[PATCH] D111833: [clang] Fortify warning for scanf calls with field width too big.

Nico Weber via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 28 07:41:46 PDT 2021


thakis added a comment.

This doesn't seem to be working very well:

  thakis at thakis:~/src/llvm-project$ cat test.cc
  #include <inttypes.h>
  #include <stdio.h>
  #include <stdint.h>
  
  int main() {
    uint16_t hextets[8];
    int chars_scanned;
    char buf[] = "1234:5678:9abc:def0:1234:5678:9abc:def0";
    sscanf(buf,
  	 "%4" SCNx16 ":%4" SCNx16 ":%4" SCNx16 ":%4" SCNx16 ":%4" SCNx16
  	 ":%4" SCNx16 ":%4" SCNx16 ":%4" SCNx16 "%n",
  	 &hextets[0], &hextets[1], &hextets[2], &hextets[3], &hextets[4],
  	 &hextets[5], &hextets[6], &hextets[7], &chars_scanned);
  
    for (int i = 0; i < 8; ++i)
      printf("%x ", hextets[i]);
    printf("%d\n", chars_scanned);
  }
  thakis at thakis:~/src/llvm-project$ out/gn/bin/clang test.cc -Wall
  test.cc:9:3: warning: 'sscanf' may overflow; destination buffer in argument 9 has size 4, but the corresponding field width plus NUL byte is 5 [-Wfortify-source]
    sscanf(buf,
    ^
  test.cc:9:3: warning: 'sscanf' may overflow; destination buffer in argument 10 has size 2, but the corresponding field width plus NUL byte is 5 [-Wfortify-source]
  2 warnings generated.
  thakis at thakis:~/src/llvm-project$ ./a.out 
  1234 5678 9abc def0 1234 5678 9abc def0 39



1. The warning is emitted twice, but doesn't point at code the 2nd time round
2. That code looks correct to me (ie there shouldn't be any warnings), maybe `%n` isn't handled correctly?
3. The diag points at the start of the scanf instead of at the faulty arg.

Especially 2 is breaking builds, so I'll revert this for now. Looks like a cool warning though, looking forward to the relanding :)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D111833/new/

https://reviews.llvm.org/D111833



More information about the cfe-commits mailing list