public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/104012] New: -Wformat-truncation warnings not taking previous length check into account
@ 2022-01-13 16:01 eike@sf-mail.de
  2022-01-13 16:04 ` [Bug tree-optimization/104012] [12 regression] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: eike@sf-mail.de @ 2022-01-13 16:01 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 104012
           Summary: -Wformat-truncation warnings not taking previous
                    length check into account
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: eike@sf-mail.de
  Target Milestone: ---

This code is from CMake's Source/cmLocalUnixMakefileGenerator3.cxx:

std::string cmLocalUnixMakefileGenerator3::CreateMakeVariable(
  std::string const& s, std::string const& s2)
{
[…]
    char buffer[5];
    int ni = 0;
    snprintf(buffer, sizeof(buffer), "%04d", ni);
    ret = str1 + str2 + buffer;
    while (this->ShortMakeVariableMap.count(ret) && ni < 1000) {
      ++ni;
      snprintf(buffer, sizeof(buffer), "%04d", ni);
      ret = str1 + str2 + buffer;
    }


The second snprintf() causes this warning:

…/CMake/Source/cmLocalUnixMakefileGenerator3.cxx:1311:41: warning: '%04d'
directive output may be truncated writing between 4 and 11 bytes into a region
of size 5 [-Wformat-truncation=]
 1311 |       snprintf(buffer, sizeof(buffer), "%04d", ni);
      |                                         ^~~~
…/CMake/Source/cmLocalUnixMakefileGenerator3.cxx:1311:40: note: directive
argument in the range [-2147483647, 2147483647]
 1311 |       snprintf(buffer, sizeof(buffer), "%04d", ni);
      |                                        ^~~~~~

The second warning line shows that the argument range is not correctly limited
to [0, 1000], which would have avoided the warning. A similar warning is shown
~30 lines earlier in the same file for basically the same code.

My current version is:

gcc-12.0.0 (Gentoo 12.0.0_pre9999 p2, commit
8b35f02ed599a70cce752e3cb544a7c9f808fce8) 12.0.0 20220111 (experimental)

The version used previously has been built on 2021-08-14T20:47:39 and didn't
show that warning.

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

end of thread, other threads:[~2022-01-14 17:29 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-13 16:01 [Bug c++/104012] New: -Wformat-truncation warnings not taking previous length check into account eike@sf-mail.de
2022-01-13 16:04 ` [Bug tree-optimization/104012] [12 regression] " pinskia at gcc dot gnu.org
2022-01-13 19:58 ` eike@sf-mail.de
2022-01-13 21:49 ` msebor at gcc dot gnu.org
2022-01-14 14:14 ` eike@sf-mail.de
2022-01-14 16:20 ` msebor at gcc dot gnu.org
2022-01-14 17:29 ` msebor 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).