Skip to content

Incorrect warning message #143951

@Bill-Gray

Description

@Bill-Gray

(Compiles correctly, no security ramifications, so prioritize accordingly. May be an easy fix, though.)

In clang 18.1 and later (but not in 17.0.1 or earlier), compiling the following :

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int main( const int argc, const char **argv)
{
   char buff[9];

   assert( argc == 2);
   snprintf( buff, sizeof( buff), "%8.2f", atof( argv[1]));
   printf( "'%s'\n", buff);
   snprintf( buff, sizeof( buff), "%+8.2f", atof( argv[1]));
   printf( "'%s'\n", buff);
   return( 0);
}

gets me the following warning for the second snprintf(). I think this is due to an assumption that the '+' will be added in, resulting in a nine-byte (plus \0 terminator) output. Tested via the godbolt.org Compiler Explorer.

<source>:12:4: warning: 'snprintf' will always be truncated; specified size is 9, but format string expands to at least 10 [-Wformat-truncation]
   12 |    snprintf( buff, sizeof( buff), "%+8.2f", atof( argv[1]));
      |    ^

Thank you. I'm quite impressed with this compiler!

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerconfirmedVerified by a second party

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions