Home | History | Annotate | Line # | Download | only in lint1
msg_366.c revision 1.5
      1 /*	$NetBSD: msg_366.c,v 1.5 2024/08/31 06:57:31 rillig Exp $	*/
      2 # 3 "msg_366.c"
      3 
      4 // Test for message: missing '\0' at the end of '%.*s' [366]
      5 
      6 /*
      7  * In the new-style format, each conversion ends with a '\0'.  If that's not
      8  * the case, snprintb will read beyond the end of the format argument, looking
      9  * for the terminating '\0'.  In the most common case where the format comes
     10  * from a string literal, the '\0' from the conversion needs to be spelled
     11  * out, while the '\0' that terminates the sequence of conversions is provided
     12  * by the C compiler.
     13  */
     14 
     15 /* lint1-extra-flags: -X 351 */
     16 
     17 typedef typeof(sizeof(0)) size_t;
     18 typedef unsigned long long uint64_t;
     19 
     20 int snprintb(char *, size_t, const char *, uint64_t);
     21 
     22 void
     23 example(unsigned u32)
     24 {
     25 	char buf[64];
     26 
     27 	/* expect+4: warning: redundant '\0' at the end of the format [377] */
     28 	snprintb(buf, sizeof(buf),
     29 	    "\177\020"
     30 	    "\0",
     31 	    u32);
     32 
     33 	/* expect+5: warning: empty description in 'b\007' [367] */
     34 	/* expect+4: warning: missing '\0' at the end of 'b\007' [366] */
     35 	snprintb(buf, sizeof(buf),
     36 	    "\177\020"
     37 	    "b\007",
     38 	    u32);
     39 
     40 	/* expect+5: warning: empty description in 'f\007\000' [367] */
     41 	/* expect+4: warning: missing '\0' at the end of 'f\007\000' [366] */
     42 	snprintb(buf, sizeof(buf),
     43 	    "\177\020"
     44 	    "f\007\000",
     45 	    u32);
     46 
     47 	/* expect+4: warning: missing '\0' at the end of 'F\007\000' [366] */
     48 	snprintb(buf, sizeof(buf),
     49 	    "\177\020"
     50 	    "F\007\000",
     51 	    u32);
     52 
     53 	/* expect+4: warning: missing '\0' at the end of '=\007value' [366] */
     54 	snprintb(buf, sizeof(buf),
     55 	    "\177\020"
     56 	    "=\007value",
     57 	    u32);
     58 
     59 	/* expect+4: warning: missing '\0' at the end of ':\007value' [366] */
     60 	snprintb(buf, sizeof(buf),
     61 	    "\177\020"
     62 	    ":\007value",
     63 	    u32);
     64 
     65 	/* expect+4: warning: missing '\0' at the end of '*default' [366] */
     66 	snprintb(buf, sizeof(buf),
     67 	    "\177\020"
     68 	    "*default",
     69 	    u32);
     70 }
     71