public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/111102] New: illegal pointer arithmetic invoked by std::format("L{:65536}",1)
@ 2023-08-22 17:20 gcc at pauldreik dot se
  2023-08-22 20:18 ` [Bug libstdc++/111102] " redi at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: gcc at pauldreik dot se @ 2023-08-22 17:20 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111102

            Bug ID: 111102
           Summary: illegal pointer arithmetic invoked by
                    std::format("L{:65536}",1)
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at pauldreik dot se
  Target Milestone: ---

The following program:

#include <format>

constexpr auto blah = std::format(L"{:65536}", 1);

is problematic when compiled with gcc 13 as well as the current trunk.

It triggers a pointer arithmetic error inside
https://gcc.gnu.org/git/?p=gcc.git;a=blame;f=libstdc%2B%2B-v3/include/std/format;hb=1d9454aba615eadd0d85c93713dd848227345f67#l295

it seems like the width is parsed into a unsigned short, but the return value
is not checked in the case the format string was not a char string (as above,
which is a wide char string).

The following patch fixes the problem:
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] 7+ messages in thread

* [Bug libstdc++/111102] illegal pointer arithmetic invoked by std::format("L{:65536}",1)
  2023-08-22 17:20 [Bug libstdc++/111102] New: illegal pointer arithmetic invoked by std::format("L{:65536}",1) gcc at pauldreik dot se
@ 2023-08-22 20:18 ` redi at gcc dot gnu.org
  2023-08-24 12:45 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2023-08-22 20:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111102

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Target Milestone|---                         |13.3
   Last reconfirmed|                            |2023-08-22

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

* [Bug libstdc++/111102] illegal pointer arithmetic invoked by std::format("L{:65536}",1)
  2023-08-22 17:20 [Bug libstdc++/111102] New: illegal pointer arithmetic invoked by std::format("L{:65536}",1) gcc at pauldreik dot se
  2023-08-22 20:18 ` [Bug libstdc++/111102] " redi at gcc dot gnu.org
@ 2023-08-24 12:45 ` cvs-commit at gcc dot gnu.org
  2023-08-24 12:45 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-08-24 12:45 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111102

--- Comment #1 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:dd4bdb9eea436bf06f175d8dbfc2190377455be4

commit r14-3457-gdd4bdb9eea436bf06f175d8dbfc2190377455be4
Author: Paul Dreik <gccpatches@pauldreik.se>
Date:   Thu Aug 24 11:43:43 2023 +0100

    libstdc++: fix illegal pointer arithmetic in format [PR111102]

    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>

    libstdc++-v3/ChangeLog:

            PR libstdc++/111102
            * include/std/format (__format::__parse_integer): Check for
            non-null pointer.

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

* [Bug libstdc++/111102] illegal pointer arithmetic invoked by std::format("L{:65536}",1)
  2023-08-22 17:20 [Bug libstdc++/111102] New: illegal pointer arithmetic invoked by std::format("L{:65536}",1) gcc at pauldreik dot se
  2023-08-22 20:18 ` [Bug libstdc++/111102] " redi at gcc dot gnu.org
  2023-08-24 12:45 ` cvs-commit at gcc dot gnu.org
@ 2023-08-24 12:45 ` cvs-commit at gcc dot gnu.org
  2023-09-27 16:17 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-08-24 12:45 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111102

--- Comment #2 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:7564fe98657ad5ede34bd08f5279778fa8698865

commit r14-3458-g7564fe98657ad5ede34bd08f5279778fa8698865
Author: Paul Dreik <gccpatches@pauldreik.se>
Date:   Thu Aug 24 11:43:43 2023 +0100

    libstdc++: Add test for illegal pointer arithmetic in format [PR111102]

    libstdc++-v3/ChangeLog:

            PR libstdc++/111102
            * testsuite/std/format/string.cc: Check wide character format
            strings with out-of-range widths.

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

* [Bug libstdc++/111102] illegal pointer arithmetic invoked by std::format("L{:65536}",1)
  2023-08-22 17:20 [Bug libstdc++/111102] New: illegal pointer arithmetic invoked by std::format("L{:65536}",1) gcc at pauldreik dot se
                   ` (2 preceding siblings ...)
  2023-08-24 12:45 ` cvs-commit at gcc dot gnu.org
@ 2023-09-27 16:17 ` cvs-commit at gcc dot gnu.org
  2023-09-27 16:19 ` cvs-commit at gcc dot gnu.org
  2023-09-27 16:20 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-09-27 16:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111102

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-13 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:183eea6029be2f6c9f416d6ffe751c469237ff2d

commit r13-7916-g183eea6029be2f6c9f416d6ffe751c469237ff2d
Author: Paul Dreik <gccpatches@pauldreik.se>
Date:   Thu Aug 24 11:43:43 2023 +0100

    libstdc++: fix illegal pointer arithmetic in format [PR111102]

    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>

    libstdc++-v3/ChangeLog:

            PR libstdc++/111102
            * include/std/format (__format::__parse_integer): Check for
            non-null pointer.

    (cherry picked from commit dd4bdb9eea436bf06f175d8dbfc2190377455be4)

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

* [Bug libstdc++/111102] illegal pointer arithmetic invoked by std::format("L{:65536}",1)
  2023-08-22 17:20 [Bug libstdc++/111102] New: illegal pointer arithmetic invoked by std::format("L{:65536}",1) gcc at pauldreik dot se
                   ` (3 preceding siblings ...)
  2023-09-27 16:17 ` cvs-commit at gcc dot gnu.org
@ 2023-09-27 16:19 ` cvs-commit at gcc dot gnu.org
  2023-09-27 16:20 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-09-27 16:19 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111102

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-13 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:9853ad876bd3d9d4685126466f74402e567664b3

commit r13-7918-g9853ad876bd3d9d4685126466f74402e567664b3
Author: Paul Dreik <gccpatches@pauldreik.se>
Date:   Thu Aug 24 11:43:43 2023 +0100

    libstdc++: Add test for illegal pointer arithmetic in format [PR111102]

    libstdc++-v3/ChangeLog:

            PR libstdc++/111102
            * testsuite/std/format/string.cc: Check wide character format
            strings with out-of-range widths.

    (cherry picked from commit 7564fe98657ad5ede34bd08f5279778fa8698865)

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

* [Bug libstdc++/111102] illegal pointer arithmetic invoked by std::format("L{:65536}",1)
  2023-08-22 17:20 [Bug libstdc++/111102] New: illegal pointer arithmetic invoked by std::format("L{:65536}",1) gcc at pauldreik dot se
                   ` (4 preceding siblings ...)
  2023-09-27 16:19 ` cvs-commit at gcc dot gnu.org
@ 2023-09-27 16:20 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2023-09-27 16:20 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111102

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|NEW                         |RESOLVED

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Fixed for 13.3, thanks for the report and patches.

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

end of thread, other threads:[~2023-09-27 16:20 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-22 17:20 [Bug libstdc++/111102] New: illegal pointer arithmetic invoked by std::format("L{:65536}",1) gcc at pauldreik dot se
2023-08-22 20:18 ` [Bug libstdc++/111102] " redi at gcc dot gnu.org
2023-08-24 12:45 ` cvs-commit at gcc dot gnu.org
2023-08-24 12:45 ` cvs-commit at gcc dot gnu.org
2023-09-27 16:17 ` cvs-commit at gcc dot gnu.org
2023-09-27 16:19 ` cvs-commit at gcc dot gnu.org
2023-09-27 16:20 ` redi at gcc dot gnu.org

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).