public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "msebor at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug other/103736] snprintf bogus format-truncation, disregarding modulo on argument Date: Wed, 15 Dec 2021 17:12:37 +0000 [thread overview] Message-ID: <bug-103736-4-BCoIh642Jy@http.gcc.gnu.org/bugzilla/> (raw) In-Reply-To: <bug-103736-4@http.gcc.gnu.org/bugzilla/> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103736 Martin Sebor <msebor at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |msebor at gcc dot gnu.org --- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> --- The warning is based on the annotated IL below. The duplicate modulo computation is optimized to take place just once (in PRE), before the negativity test, the range of the result in the else branch is greater than the code permits. void func () { char timezone[4]; signed char timezoneval.0_1; int _4; int _12; signed char _13; <bb 2> [local count: 1073741824]: timezoneval.0_1 = timezoneval; _13 = timezoneval.0_1 % 100; _12 = (int) _13; >>> _12's range is [-99, 99] if (timezoneval.0_1 < 0) goto <bb 3>; [41.00%] else goto <bb 4>; [59.00%] <bb 3> [local count: 440234144]: _4 = -_12; _4's range is [0, 99] snprintf (&timezone, 4, "-%02d", _4); so no warning here goto <bb 5>; [100.00%] <bb 4> [local count: 633507681]: snprintf (&timezone, 4, "+%02d", _12); << -Wformat-truncation <bb 5> [local count: 1073741824]: timezone ={v} {CLOBBER}; return; } The warning can be avoided by using a local temporary copy of the extern variable, like so int x = timezoneval; if(x < 0) { snprintf(timezone, sizeof(timezone),"-%02d",-(x % 100)); } else { snprintf(timezone, sizeof(timezone),"+%02d", x % 100); } or simply: int x = timezoneval % 100; if(x < 0) { snprintf(timezone, sizeof(timezone),"-%02d",-x); } else { snprintf(timezone, sizeof(timezone),"+%02d", x); }
prev parent reply other threads:[~2021-12-15 17:12 UTC|newest] Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-12-15 14:39 [Bug other/103736] New: " patrickdepinguin at gmail dot com 2021-12-15 17:12 ` msebor at gcc dot gnu.org [this message]
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-103736-4-BCoIh642Jy@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: linkBe 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).