public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fix for bug libstdc++/111102 pointer arithmetic on nullptr
@ 2023-08-23 18:48 Paul Dreik
  2023-08-24 12:47 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Paul Dreik @ 2023-08-23 18:48 UTC (permalink / raw)
  To: gcc-patches; +Cc: libstdc++


[-- Attachment #1.1: Type: text/plain, Size: 1437 bytes --]

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

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 840 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Fix for bug libstdc++/111102 pointer arithmetic on nullptr
  2023-08-23 18:48 [PATCH] Fix for bug libstdc++/111102 pointer arithmetic on nullptr Paul Dreik
@ 2023-08-24 12:47 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2023-08-24 12:47 UTC (permalink / raw)
  To: Paul Dreik; +Cc: gcc-patches, libstdc++

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-08-24 12:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-23 18:48 [PATCH] Fix for bug libstdc++/111102 pointer arithmetic on nullptr Paul Dreik
2023-08-24 12:47 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).