tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestSprintfLiteral.cpp (1875B)


      1 #include <cstdio>
      2 
      3 void bad() {
      4  char x[100];
      5  snprintf(x, sizeof(x), "bar"); // expected-error {{Use SprintfLiteral instead of snprintf when writing into a character array.}} expected-note {{This will prevent passing in the wrong size to snprintf accidentally.}}
      6  snprintf(x, 100, "bar"); // expected-error {{Use SprintfLiteral instead of snprintf when writing into a character array.}} expected-note {{This will prevent passing in the wrong size to snprintf accidentally.}}
      7  const int hundred = 100;
      8  snprintf(x, hundred, "bar"); // expected-error {{Use SprintfLiteral instead of snprintf when writing into a character array.}} expected-note {{This will prevent passing in the wrong size to snprintf accidentally.}}
      9 }
     10 
     11 void ok() {
     12  char x[100];
     13  int y;
     14  snprintf(x, sizeof(y), "foo");
     15 
     16  snprintf(x, 50, "foo");
     17 
     18  int nothundred = 100;
     19  nothundred = 99;
     20  snprintf(x, nothundred, "foo");
     21 }
     22 
     23 void vargs_bad(va_list args) {
     24  char x[100];
     25  vsnprintf(x, sizeof(x), "bar", args); // expected-error {{Use VsprintfLiteral instead of vsnprintf when writing into a character array.}} expected-note {{This will prevent passing in the wrong size to vsnprintf accidentally.}}
     26  vsnprintf(x, 100, "bar", args); // expected-error {{Use VsprintfLiteral instead of vsnprintf when writing into a character array.}} expected-note {{This will prevent passing in the wrong size to vsnprintf accidentally.}}
     27  const int hundred = 100;
     28  vsnprintf(x, hundred, "bar", args); // expected-error {{Use VsprintfLiteral instead of vsnprintf when writing into a character array.}} expected-note {{This will prevent passing in the wrong size to vsnprintf accidentally.}}
     29 }
     30 
     31 void vargs_good(va_list args) {
     32  char x[100];
     33  int y;
     34  vsnprintf(x, sizeof(y), "foo", args);
     35 
     36  vsnprintf(x, 50, "foo", args);
     37 
     38  int nothundred = 100;
     39  nothundred = 99;
     40  vsnprintf(x, nothundred, "foo", args);
     41 }