On Wed, 23 Aug 2023 at 19:48, Paul Dreik via Libstdc++ <libstd...@gcc.gnu.org> wrote: > > This fixes pointer arithmetic made on a null pointer, which I found > through fuzzing. > Tested on debian/amd64. > > Thanks, Paul
Thanks. Pushed to trunk, backport to gcc-13 to follow. I also added your testcase from the bug report to the testsuite. > > ------------------------------------------------------------------------ > commit 78ac41590432f4f01036797fd9d661f6ed80cf37 (HEAD -> master) > Author: Paul Dreik <gccpatc...@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 <gccpatc...@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}; > }