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: e...@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.

Reply via email to