Issue 143951
Summary Incorrect warning message
Labels new issue
Assignees
Reporter 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 : 
```c
#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](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,selection:(endColumn:2,endLineNumber:15,positionColumn:1,positionLineNumber:1,selectionStartColumn:2,selectionStartLineNumber:15,startColumn:1,startLineNumber:1),source:'%23include+%3Cstdio.h%3E%0A%23include+%3Cstdlib.h%3E%0A%23include+%3Cassert.h%3E%0A%0Aint+main(+const+int+argc,+const+char+**argv)%0A%7B%0A+++char+buff%5B9%5D%3B%0A%0A+++assert(+argc+%3D%3D+2)%3B%0A+++snprintf(+buff,+sizeof(+buff),+%22%258.2f%22,+atof(+argv%5B1%5D))%3B%0A+++printf(+%22!'%25s!'%5Cn%22,+buff)%3B%0A+++snprintf(+buff,+sizeof(+buff),+%22%25%2B8.2f%22,+atof(+argv%5B1%5D))%3B%0A+++printf(+%22!'%25s!'%5Cn%22,+buff)%3B%0A+++return(+0)%3B%0A%7D%0A'),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:clang1810,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,libs:!(),options:'',overrides:!(),selection:(endColumn:37,endLineNumber:6,positionColumn:37,positionLineNumber:6,selectionStartColumn:37,selectionStartLineNumber:6,startColumn:37,startLineNumber:6),source:1),l:'5',n:'0',o:'+x86-64+clang+18.1.0+(Editor+%231)',t:'0')),k:33.333333333333336,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'x86-64+clang+17.0.1+(assertions)',editorid:1,fontScale:14,fontUsePx:'0',j:1,wrap:'1'),l:'5',n:'0',o:'Output+of+x86-64+clang+18.1.0+(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).
```
<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!
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to