Fix for bug libstdc++/111102 pointer arithmetic on nullptr

Message ID 3619b32b-e7cb-7e67-2fea-67e3d9c5377a@pauldreik.se
State Not Applicable
Headers
Series Fix for bug libstdc++/111102 pointer arithmetic on nullptr |

Checks

Context Check Description
snail/gcc-patch-check fail Git am fail log

Commit Message

Paul Dreik Aug. 23, 2023, 6:48 p.m. UTC
  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>

      }
  

Comments

Jonathan Wakely Aug. 24, 2023, 12:47 p.m. UTC | #1
On Wed, 23 Aug 2023 at 19:48, Paul Dreik via Libstdc++
<libstdc++@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 <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};
>       }
  

Patch

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};