public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "gcc at pauldreik dot se" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/110860] New: std::format("{:f}",2e304) invokes undefined behaviour
Date: Mon, 31 Jul 2023 14:49:48 +0000	[thread overview]
Message-ID: <bug-110860-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 110860
           Summary: std::format("{:f}",2e304) invokes undefined behaviour
           Product: gcc
           Version: 13.2.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, compiled with gcc 13.2:

#include <format>
#include <cstdio>

int main() {
std::puts(std::format("{:f}",2e304).c_str());
}

causes ubsan to warn:
/opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/format:1489:52: runtime
error: negation of -2147483648 cannot be represented in type 'int'; cast to an
unsigned type to negate this value to itself

I believe the problem is using __builtin_abs() which uses integer, but is fed a
double.


Link to reproducer:

https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,selection:(endColumn:1,endLineNumber:6,positionColumn:1,positionLineNumber:6,selectionStartColumn:1,selectionStartLineNumber:6,startColumn:1,startLineNumber:6),source:'%23include+%3Cformat%3E%0A%23include+%3Ccstdio%3E%0A%0Aint+main()+%7B%0Astd::puts(std::format(%22%7B:f%7D%22,2e304).c_str())%3B%0A%7D'),l:'5',n:'0',o:'C%2B%2B+source+%231',t:'0')),k:33.333333333333336,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:g132,deviceViewOpen:'1',filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'0',intel:'0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,libs:!(),options:'-std%3Dgnu%2B%2B2b+-fsanitize%3Daddress,undefined,leak+-g',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+gcc+13.2+(Editor+%231)',t:'0')),k:33.333333333333336,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(editorid:1,fontScale:14,fontUsePx:'0',j:1,wrap:'1'),l:'5',n:'0',o:'Output+of+x86-64+gcc+13.2+(Compiler+%231)',t:'0')),k:33.33333333333333,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4

             reply	other threads:[~2023-07-31 14:49 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-31 14:49 gcc at pauldreik dot se [this message]
2023-07-31 21:41 ` [Bug libstdc++/110860] " pinskia at gcc dot gnu.org
2023-08-01 12:38 ` redi at gcc dot gnu.org
2023-08-01 12:38 ` redi at gcc dot gnu.org
2023-08-03  9:54 ` redi at gcc dot gnu.org
2023-08-07 21:14 ` cvs-commit at gcc dot gnu.org
2023-08-08 16:13 ` cvs-commit at gcc dot gnu.org
2023-08-08 16:14 ` redi at gcc dot gnu.org
2023-08-11 11:02 ` gcc at pauldreik dot se
2023-08-11 11:40 ` redi at gcc dot gnu.org
2023-08-11 17:17 ` redi at gcc dot gnu.org
2023-08-11 17:21 ` cvs-commit at gcc dot gnu.org
2023-08-12 11:38 ` gcc at pauldreik dot se
2023-08-12 11:56 ` redi at gcc dot gnu.org
2023-08-12 11:58 ` redi at gcc dot gnu.org
2023-08-14  8:35 ` redi at gcc dot gnu.org
2023-08-14 17:10 ` cvs-commit at gcc dot gnu.org
2023-08-14 17:48 ` cvs-commit at gcc dot gnu.org
2023-08-14 17:50 ` redi at gcc dot gnu.org
2023-08-15 18:53 ` gcc at pauldreik dot se

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-110860-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).