55f6e7
commit 211a30a92b72a18ea4caa35ed503b70bc644923e
55f6e7
Author: Joseph Myers <joseph@codesourcery.com>
55f6e7
Date:   Mon Nov 8 19:11:51 2021 +0000
55f6e7
55f6e7
    Fix memmove call in vfprintf-internal.c:group_number
55f6e7
    
55f6e7
    A recent GCC mainline change introduces errors of the form:
55f6e7
    
55f6e7
    vfprintf-internal.c: In function 'group_number':
55f6e7
    vfprintf-internal.c:2093:15: error: 'memmove' specified bound between 9223372036854775808 and 18446744073709551615 exceeds maximum object size 9223372036854775807 [-Werror=stringop-overflow=]
55f6e7
     2093 |               memmove (w, s, (front_ptr -s) * sizeof (CHAR_T));
55f6e7
          |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55f6e7
    
55f6e7
    This is a genuine bug in the glibc code: s > front_ptr is always true
55f6e7
    at this point in the code, and the intent is clearly for the
55f6e7
    subtraction to be the other way round.  The other arguments to the
55f6e7
    memmove call here also appear to be wrong; w and s point just *after*
55f6e7
    the destination and source for copying the rest of the number, so the
55f6e7
    size needs to be subtracted to get appropriate pointers for the
55f6e7
    copying.  Adjust the memmove call to conform to the apparent intent of
55f6e7
    the code, so fixing the -Wstringop-overflow error.
55f6e7
    
55f6e7
    Now, if the original code were ever executed, a buffer overrun would
55f6e7
    result.  However, I believe this code (introduced in commit
55f6e7
    edc1686af0c0fc2eb535f1d38cdf63c1a5a03675, "vfprintf: Reuse work_buffer
55f6e7
    in group_number", so in glibc 2.26) is unreachable in prior glibc
55f6e7
    releases (so there is no need for a bug in Bugzilla, no need to
55f6e7
    consider any backports unless someone wants to build older glibc
55f6e7
    releases with GCC 12 and no possibility of this buffer overrun
55f6e7
    resulting in a security issue).
55f6e7
    
55f6e7
    work_buffer is 1000 bytes / 250 wide characters.  This case is only
55f6e7
    reachable if an initial part of the number, plus a grouped copy of the
55f6e7
    rest of the number, fail to fit in that space; that is, if the grouped
55f6e7
    number fails to fit in the space.  In the wide character case,
55f6e7
    grouping is always one wide character, so even with a locale (of which
55f6e7
    there aren't any in glibc) grouping every digit, a number would need
55f6e7
    to occupy at least 125 wide characters to overflow, and a 64-bit
55f6e7
    integer occupies at most 23 characters in octal including a leading 0.
55f6e7
    In the narrow character case, the multibyte encoding of the grouping
55f6e7
    separator would need to be at least 42 bytes to overflow, again
55f6e7
    supposing grouping every digit, but MB_LEN_MAX is 16.  So even if we
55f6e7
    admit the case of artificially constructed locales not shipped with
55f6e7
    glibc, given that such a locale would need to use one of the character
55f6e7
    sets supported by glibc, this code cannot be reached at present.  (And
55f6e7
    POSIX only actually specifies the ' flag for grouping for decimal
55f6e7
    output, though glibc acts on it for other bases as well.)
55f6e7
    
55f6e7
    With binary output (if you consider use of grouping there to be
55f6e7
    valid), you'd need a 15-byte multibyte character for overflow; I don't
55f6e7
    know if any supported character set has such a character (if, again,
55f6e7
    we admit constructed locales using grouping every digit and a grouping
55f6e7
    separator chosen to have a multibyte encoding as long as possible, as
55f6e7
    well as accepting use of grouping with binary), but given that we have
55f6e7
    this code at all (clearly it's not *correct*, or in accordance with
55f6e7
    the principle of avoiding arbitrary limits, to skip grouping on
55f6e7
    running out of internal space like that), I don't think it should need
55f6e7
    any further changes for binary printf support to go in.
55f6e7
    
55f6e7
    On the other hand, support for large sizes of _BitInt in printf (see
55f6e7
    the N2858 proposal) *would* require something to be done about such
55f6e7
    arbitrary limits (presumably using dynamic allocation in printf again,
55f6e7
    for sufficiently large _BitInt arguments only - currently only
55f6e7
    floating-point uses dynamic allocation, and, as previously discussed,
55f6e7
    that could actually be replaced by bounded allocation given smarter
55f6e7
    code).
55f6e7
    
55f6e7
    Tested with build-many-glibcs.py for aarch64-linux-gnu (GCC mainline).
55f6e7
    Also tested natively for x86_64.
55f6e7
    
55f6e7
    (cherry picked from commit db6c4935fae6005d46af413b32aa92f4f6059dce)
55f6e7
55f6e7
diff --git a/stdio-common/vfprintf.c b/stdio-common/vfprintf.c
55f6e7
index 6b83ba91a12cdcd5..2d434ba45a67911e 100644
55f6e7
--- a/stdio-common/vfprintf.c
55f6e7
+++ b/stdio-common/vfprintf.c
55f6e7
@@ -2101,7 +2101,8 @@ group_number (CHAR_T *front_ptr, CHAR_T *w, CHAR_T *rear_ptr,
55f6e7
 	    copy_rest:
55f6e7
 	      /* No further grouping to be done.  Copy the rest of the
55f6e7
 		 number.  */
55f6e7
-	      memmove (w, s, (front_ptr -s) * sizeof (CHAR_T));
55f6e7
+	      w -= s - front_ptr;
55f6e7
+	      memmove (w, front_ptr, (s - front_ptr) * sizeof (CHAR_T));
55f6e7
 	      break;
55f6e7
 	    }
55f6e7
 	  else if (*grouping != '\0')