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