[PATCH] Fix for bug libstdc++/111102 pointer arithmetic on nullptr

Paul Dreik gccpatches@pauldreik.se
Wed Aug 23 18:48:25 GMT 2023


This fixes pointer arithmetic made on a null pointer, which I found 
through fuzzing.
Tested on debian/amd64.

Thanks, Paul

------------------------------------------------------------------------
commit 78ac41590432f4f01036797fd9d661f6ed80cf37 (HEAD -> master)
Author: Paul Dreik <gccpatches@pauldreik.se>
Date:   Tue Aug 22 19:16:57 2023 +0200

     libstdc++: fix illegal pointer arithmetic in format

     when parsing a format string, the width is parsed into an unsigned 
short
     but the result is not checked in the case the format string is not a
     char string (such as a wide string). in case the parse fails,
     a null pointer is returned which is used for pointer arithmetic
     which is undefined behaviour.

     Signed-off-by: Paul Dreik <gccpatches@pauldreik.se>

diff --git a/libstdc++-v3/include/std/format 
b/libstdc++-v3/include/std/format
index f3d9ae152f..fe2caa5868 100644
--- a/libstdc++-v3/include/std/format
+++ b/libstdc++-v3/include/std/format
@@ -285,7 +285,8 @@ namespace __format
           for (int __i = 0; __i < __n && (__first + __i) != __last; ++__i)
             __buf[__i] = __first[__i];
           auto [__v, __ptr] = __format::__parse_integer(__buf, __buf + 
__n);
-         return {__v, __first + (__ptr - __buf)};
+         if (__ptr) [[likely]]
+           return {__v, __first + (__ptr - __buf)};
         }
        return {0, nullptr};
      }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 840 bytes
Desc: OpenPGP digital signature
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20230823/35aaa373/attachment.sig>


More information about the Libstdc++ mailing list