chk.c revision 1.71 1 /* $NetBSD: chk.c,v 1.71 2025/05/24 07:00:32 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 * Copyright (c) 1994, 1995 Jochen Pohl
6 * All Rights Reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Jochen Pohl for
19 * The NetBSD Project.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
37 #endif
38
39 #include <sys/cdefs.h>
40 #if defined(__RCSID)
41 __RCSID("$NetBSD: chk.c,v 1.71 2025/05/24 07:00:32 rillig Exp $");
42 #endif
43
44 #include <ctype.h>
45 #include <limits.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 #include "lint2.h"
50
51 static void check_used_not_defined(const hte_t *);
52 static void check_defined_not_used(const hte_t *);
53 static void check_declared_not_used_or_defined(const hte_t *);
54 static void check_multiple_definitions(const hte_t *);
55 static void chkvtui(const hte_t *, sym_t *, sym_t *);
56 static void chkvtdi(const hte_t *, sym_t *, sym_t *);
57 static void chkfaui(const hte_t *, sym_t *, sym_t *);
58 static void chkau(const hte_t *, int, sym_t *, sym_t *, pos_t *,
59 fcall_t *, fcall_t *, type_t *, type_t *);
60 static void check_return_values(const hte_t *, sym_t *);
61 static void check_argument_declarations(const hte_t *, sym_t *, sym_t *);
62 static void printflike(const hte_t *, fcall_t *, int, const char *, type_t **);
63 static void scanflike(const hte_t *, fcall_t *, int, const char *, type_t **);
64 static void bad_format_string(const hte_t *, fcall_t *);
65 static void inconsistent_arguments(const hte_t *, fcall_t *, int);
66 static void too_few_arguments(const hte_t *, fcall_t *);
67 static void too_many_arguments(const hte_t *, fcall_t *);
68 static bool types_compatible(type_t *, type_t *, bool, bool, bool, bool *);
69 static bool prototypes_compatible(type_t *, type_t *, bool *);
70 static bool matches_no_arg_function(type_t *, bool *);
71
72
73 /*
74 * If there is a symbol named "main", mark it as used.
75 */
76 void
77 mark_main_as_used(void)
78 {
79 hte_t *hte;
80
81 if ((hte = htab_search("main", false)) != NULL)
82 hte->h_used = true;
83 }
84
85 /*
86 * Performs all tests for a single name
87 */
88 void
89 check_name(const hte_t *hte)
90 {
91 sym_t *sym, *def, *pdecl, *decl;
92
93 if (!uflag) {
94 check_used_not_defined(hte);
95 check_defined_not_used(hte);
96 if (xflag)
97 check_declared_not_used_or_defined(hte);
98 }
99 check_multiple_definitions(hte);
100
101 /* Get definition, prototype declaration and declaration */
102 def = pdecl = decl = NULL;
103 for (sym = hte->h_syms; sym != NULL; sym = sym->s_next) {
104 if (def == NULL && (sym->s_def == DEF || sym->s_def == TDEF))
105 def = sym;
106 if (pdecl == NULL && sym->s_def == DECL &&
107 TP(sym->s_type)->t_tspec == FUNC &&
108 TP(sym->s_type)->t_proto) {
109 pdecl = sym;
110 }
111 if (decl == NULL && sym->s_def == DECL)
112 decl = sym;
113 }
114
115 /* A prototype is better than an old-style declaration. */
116 if (pdecl != NULL)
117 decl = pdecl;
118
119 chkvtui(hte, def, decl);
120
121 chkvtdi(hte, def, decl);
122
123 chkfaui(hte, def, decl);
124
125 check_return_values(hte, def);
126
127 check_argument_declarations(hte, def, decl);
128 }
129
130 /*
131 * Print a warning if the name has been used, but not defined.
132 */
133 static void
134 check_used_not_defined(const hte_t *hte)
135 {
136 fcall_t *fcall;
137 usym_t *usym;
138
139 if (!hte->h_used || hte->h_def)
140 return;
141
142 if ((fcall = hte->h_calls) != NULL) {
143 /* %s is used in %s but never defined */
144 msg(0, hte->h_name, mkpos(&fcall->f_pos));
145 } else if ((usym = hte->h_usyms) != NULL) {
146 /* %s is used in %s but never defined */
147 msg(0, hte->h_name, mkpos(&usym->u_pos));
148 }
149 }
150
151 /*
152 * Print a warning if the name has been defined, but never used.
153 */
154 static void
155 check_defined_not_used(const hte_t *hte)
156 {
157 sym_t *sym;
158
159 if (!hte->h_def || hte->h_used)
160 return;
161
162 for (sym = hte->h_syms; sym != NULL; sym = sym->s_next) {
163 if (sym->s_def == DEF || sym->s_def == TDEF) {
164 /* %s is defined in %s but never used */
165 msg(1, hte->h_name, mkpos(&sym->s_pos));
166 break;
167 }
168 }
169 }
170
171 /*
172 * Print a warning if the variable has been declared, but is not used
173 * or defined.
174 */
175 static void
176 check_declared_not_used_or_defined(const hte_t *hte)
177 {
178 sym_t *sym;
179
180 if (hte->h_syms == NULL || hte->h_used || hte->h_def)
181 return;
182
183 sym = hte->h_syms;
184 if (TP(sym->s_type)->t_tspec == FUNC)
185 return;
186
187 if (sym->s_def != DECL)
188 errx(1, "internal error: check_declared_not_used_or_defined");
189 /* %s is declared in %s but never used or defined */
190 msg(2, hte->h_name, mkpos(&sym->s_pos));
191 }
192
193 /*
194 * Print a warning if there is more than one definition for
195 * this name.
196 */
197 static void
198 check_multiple_definitions(const hte_t *hte)
199 {
200 sym_t *sym, *def1;
201
202 if (!hte->h_def)
203 return;
204
205 def1 = NULL;
206 for (sym = hte->h_syms; sym != NULL; sym = sym->s_next) {
207 /*
208 * C90 allows tentative definitions of the same name in only
209 * one compilation unit.
210 */
211 if (sym->s_def != DEF && (!sflag || sym->s_def != TDEF))
212 continue;
213 if (sym->s_inline)
214 continue;
215 if (def1 == NULL) {
216 def1 = sym;
217 continue;
218 }
219 /* %s has multiple definitions in %s and %s */
220 msg(3, hte->h_name, mkpos(&def1->s_pos), mkpos(&sym->s_pos));
221 }
222 }
223
224 /*
225 * Print a warning if the return value assumed for a function call
226 * differs from the return value of the function definition or
227 * function declaration.
228 *
229 * If no definition/declaration can be found, the assumed return values
230 * are always int. So there is no need to compare with another function
231 * call as it's done for function arguments.
232 */
233 static void
234 chkvtui(const hte_t *hte, sym_t *def, sym_t *decl)
235 {
236 fcall_t *call;
237 type_t *tp1, *tp2;
238 bool dowarn, eq;
239 tspec_t t1;
240
241 if (hte->h_calls == NULL)
242 return;
243
244 if (def == NULL)
245 def = decl;
246 if (def == NULL)
247 return;
248
249 t1 = (tp1 = TP(def->s_type)->t_subt)->t_tspec;
250 for (call = hte->h_calls; call != NULL; call = call->f_next) {
251 tp2 = TP(call->f_type)->t_subt;
252 eq = types_compatible(tp1, tp2,
253 true, false, false, (dowarn = false, &dowarn));
254 if (!call->f_rused) {
255 /* no return value used */
256 if ((t1 == STRUCT || t1 == UNION) && !eq) {
257 /*
258 * If a function returns a struct or union, it
259 * must be declared to return a struct or
260 * union, even if the return value is ignored.
261 * This is necessary because the caller must
262 * allocate stack space for the return value.
263 * If it does not, the return value would
264 * overwrite other data.
265 *
266 * XXX: The following message may be confusing
267 * because it occurs also if the return value
268 * was declared inconsistently. But this
269 * behavior matches pcc-based lint, so it is
270 * accepted for now.
271 */
272 /* %s's return type in %s must be decl... */
273 msg(17, hte->h_name,
274 mkpos(&def->s_pos), mkpos(&call->f_pos));
275 }
276 continue;
277 }
278 if (!eq || (sflag && dowarn)) {
279 /* %s has its return value used inconsistently ... */
280 msg(4, hte->h_name,
281 mkpos(&def->s_pos), mkpos(&call->f_pos));
282 }
283 }
284 }
285
286 /*
287 * Print a warning if a definition/declaration does not match another
288 * definition/declaration of the same name. For functions, only the
289 * types of return values are tested.
290 */
291 static void
292 chkvtdi(const hte_t *hte, sym_t *def, sym_t *decl)
293 {
294 if (def == NULL)
295 def = decl;
296 if (def == NULL)
297 return;
298
299 type_t *tp1 = TP(def->s_type);
300 for (sym_t *sym = hte->h_syms; sym != NULL; sym = sym->s_next) {
301 if (sym == def)
302 continue;
303 type_t *tp2 = TP(sym->s_type);
304 bool dowarn = false;
305 bool is_func = tp1->t_tspec == FUNC && tp2->t_tspec == FUNC;
306 type_t *xt1 = is_func ? tp1->t_subt : tp1;
307 type_t *xt2 = is_func ? tp2->t_subt : tp2;
308 bool eq = types_compatible(xt1, xt2,
309 is_func, false, false, &dowarn);
310 if (!eq || (sflag && dowarn)) {
311 /* %s %s '%s' at %s, versus '%s' at %s */
312 msg(5, hte->h_name,
313 is_func ? "returns" : "has type",
314 type_name(xt1), mkpos(&def->s_pos),
315 type_name(xt2), mkpos(&sym->s_pos));
316 }
317 }
318 }
319
320 static int
321 total_args(int n, type_t **tpp)
322 {
323 for (; *tpp != NULL; tpp++)
324 n++;
325 return n;
326 }
327
328 /*
329 * Print a warning if a function is called with arguments which does
330 * not match the function definition, declaration or another call
331 * of the same function.
332 */
333 static void
334 chkfaui(const hte_t *hte, sym_t *def, sym_t *decl)
335 {
336 type_t *tp1, *tp2, **ap1, **ap2;
337 pos_t *pos1p = NULL;
338 fcall_t *calls, *call, *call1;
339 int n, as;
340 arginf_t *ai;
341
342 if ((calls = hte->h_calls) == NULL)
343 return;
344
345 /*
346 * If we find a function definition, we use this for comparison,
347 * otherwise the first prototype we can find. If there is no definition
348 * or prototype declaration, the first function call is used.
349 */
350 tp1 = NULL;
351 call1 = NULL;
352 if (def != NULL) {
353 if ((tp1 = TP(def->s_type))->t_tspec != FUNC)
354 return;
355 pos1p = &def->s_pos;
356 } else if (decl != NULL && TP(decl->s_type)->t_proto) {
357 if ((tp1 = TP(decl->s_type))->t_tspec != FUNC)
358 return;
359 pos1p = &decl->s_pos;
360 }
361 if (tp1 == NULL) {
362 call1 = calls;
363 calls = calls->f_next;
364 if ((tp1 = TP(call1->f_type))->t_tspec != FUNC)
365 return;
366 pos1p = &call1->f_pos;
367 }
368
369 n = 1;
370 for (call = calls; call != NULL; call = call->f_next) {
371 if ((tp2 = TP(call->f_type))->t_tspec != FUNC)
372 continue;
373 ap1 = tp1->t_args;
374 ap2 = tp2->t_args;
375 n = 0;
376 while (*ap1 != NULL && *ap2 != NULL) {
377 if (def != NULL && def->s_check_only_first_args &&
378 n >= def->s_check_num_args)
379 break;
380 n++;
381 chkau(hte, n, def, decl, pos1p, call1, call,
382 *ap1, *ap2);
383 ap1++;
384 ap2++;
385 }
386 if (*ap1 == *ap2) {
387 /* equal # of arguments */
388 } else if (def != NULL && def->s_check_only_first_args &&
389 n >= def->s_check_num_args) {
390 /*
391 * function definition with VARARGS; The # of arguments
392 * of the call must be at least as large as the
393 * parameter of VARARGS.
394 */
395 } else if (*ap2 != NULL && tp1->t_proto && tp1->t_vararg) {
396 /*
397 * prototype with ... and function call with at least
398 * the same # of arguments as declared in the
399 * prototype.
400 */
401 } else {
402 /* %s has %d parameters in %s, versus %d ... */
403 msg(7, hte->h_name, total_args(n, ap1), mkpos(pos1p),
404 total_args(n, ap2), mkpos(&call->f_pos));
405 continue;
406 }
407
408 /* perform SCANFLIKE/PRINTFLIKE tests */
409 if (def == NULL || (!def->s_printflike && !def->s_scanflike))
410 continue;
411 as = def->s_printflike
412 ? def->s_printflike_arg
413 : def->s_scanflike_arg;
414 for (ai = call->f_args; ai != NULL; ai = ai->a_next) {
415 if (ai->a_num == as)
416 break;
417 }
418 if (ai == NULL || !ai->a_fmt)
419 continue;
420 if (def->s_printflike) {
421 printflike(hte, call, n, ai->a_fstrg, ap2);
422 } else {
423 scanflike(hte, call, n, ai->a_fstrg, ap2);
424 }
425 }
426 }
427
428 /*
429 * Check a single argument in a function call.
430 *
431 * hte a pointer to the hash table entry of the function
432 * n the number of the argument (1..)
433 * def the function definition or NULL
434 * decl prototype declaration, old-style declaration or NULL
435 * pos1p position of definition, declaration of first call
436 * call1 first call, if both def and decl are old-style def/decl
437 * call checked call
438 * arg1 currently checked argument of def/decl/call1
439 * arg2 currently checked argument of call
440 */
441 static void
442 chkau(const hte_t *hte, int n, sym_t *def, sym_t *decl, pos_t *pos1p,
443 fcall_t *call1, fcall_t *call, type_t *arg1, type_t *arg2)
444 {
445 bool promote, asgn, dowarn;
446 tspec_t t1, t2;
447 arginf_t *ai, *ai1;
448
449 /*
450 * If a function definition is available (def != NULL), we compare the
451 * function call (call) with the definition. Otherwise, if a function
452 * definition is available and it is not an old-style definition (decl
453 * != NULL && TP(decl->s_type)->t_proto), we compare the call with this
454 * declaration. Otherwise we compare it with the first call we have
455 * found (call1).
456 */
457
458 /* arg1 must be promoted if it stems from an old-style definition */
459 promote = def != NULL && def->s_old_style_function;
460
461 /*
462 * If we compare with a definition or declaration, we must perform the
463 * same checks for qualifiers in indirected types as in assignments.
464 */
465 asgn = def != NULL || (decl != NULL && TP(decl->s_type)->t_proto);
466
467 dowarn = false;
468 if (types_compatible(arg1, arg2, true, promote, asgn, &dowarn) &&
469 (!sflag || !dowarn))
470 return;
471
472 /*-
473 * Other lint implementations print warnings as soon as the type of an
474 * argument does not match exactly the expected type. The result are
475 * lots of warnings which are really not necessary.
476 * We print a warning only if
477 * (0) at least one type is not an integer type and types differ
478 * (1) hflag is set and types differ
479 * (2) types differ, except in signedness
480 *
481 * If the argument is an integer constant whose msb is not set,
482 * signedness is ignored (e.g. 0 matches both signed and unsigned int).
483 * This is with and without hflag.
484 *
485 * If the argument is an integer constant with value 0 and the expected
486 * argument is of type pointer and the width of the integer constant is
487 * the same as the width of the pointer, no warning is printed.
488 */
489 t1 = arg1->t_tspec;
490 t2 = arg2->t_tspec;
491 if (is_integer(t1) && is_integer(t2) &&
492 !arg1->t_is_enum && !arg2->t_is_enum) {
493 if (promote) {
494 /*
495 * XXX Here is a problem: Although it is possible to
496 * pass an int where a char/short it expected, there
497 * may be loss in significant digits. We should first
498 * check for const arguments if they can be converted
499 * into the original parameter type.
500 */
501 if (t1 == FLOAT) {
502 t1 = DOUBLE;
503 } else if (t1 == CHAR || t1 == SCHAR) {
504 t1 = INT;
505 } else if (t1 == UCHAR) {
506 t1 = tflag ? UINT : INT;
507 } else if (t1 == SHORT) {
508 t1 = INT;
509 } else if (t1 == USHORT) {
510 t1 = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
511 }
512 }
513
514 if (signed_type(t1) == signed_type(t2)) {
515
516 /*
517 * types differ only in signedness; get information
518 * about arguments
519 */
520
521 /*
522 * treat a definition like a call with variable
523 * arguments
524 */
525 ai1 = call1 != NULL ? call1->f_args : NULL;
526
527 /*
528 * if two calls are compared, ai1 is set to the
529 * information for the n-th argument, if this was a
530 * constant, otherwise to NULL
531 */
532 for ( ; ai1 != NULL; ai1 = ai1->a_next) {
533 if (ai1->a_num == n)
534 break;
535 }
536 /*
537 * ai is set to the information of the n-th arg of the
538 * (second) call, if this was a constant, otherwise to
539 * NULL
540 */
541 for (ai = call->f_args; ai != NULL; ai = ai->a_next) {
542 if (ai->a_num == n)
543 break;
544 }
545
546 if (ai1 == NULL && ai == NULL) {
547 /* no constant at all */
548 if (!hflag)
549 return;
550 } else if (ai1 == NULL || ai == NULL) {
551 /* one constant */
552 if (ai == NULL)
553 ai = ai1;
554 if (ai->a_zero || ai->a_pcon)
555 /* same value in signed and unsigned */
556 return;
557 /* value (not representation) differently */
558 } else {
559 /*
560 * two constants, one signed, one unsigned; if
561 * the msb of one of the constants is set, the
562 * argument is used inconsistently.
563 */
564 if (!ai1->a_ncon && !ai->a_ncon)
565 return;
566 }
567 }
568
569 } else if (t1 == PTR && is_integer(t2)) {
570 for (ai = call->f_args; ai != NULL; ai = ai->a_next) {
571 if (ai->a_num == n)
572 break;
573 }
574 /*
575 * Vendor implementations of lint (e.g. HP-UX, Digital UNIX)
576 * don't care about the size of the integer argument, only
577 * whether or not it is zero. We do the same.
578 */
579 if (ai != NULL && ai->a_zero)
580 return;
581 }
582
583 /* %s has argument %d with type '%s' at %s, versus '%s' at %s */
584 msg(6, hte->h_name, n, type_name(arg1), mkpos(pos1p),
585 type_name(arg2), mkpos(&call->f_pos));
586 }
587
588 /*
589 * Compare the types in the NULL-terminated array ap with the format
590 * string fmt.
591 */
592 static void
593 printflike(const hte_t *hte, fcall_t *call, int n, const char *fmt, type_t **ap)
594 {
595 const char *fp;
596 char fc;
597 bool fwidth, prec, left, sign, space, alt, zero;
598 tspec_t sz, t1, t2 = NO_TSPEC;
599 type_t *tp;
600
601 fp = fmt;
602 fc = *fp++;
603
604 for (;;) {
605 if (fc == '\0') {
606 if (*ap != NULL)
607 too_many_arguments(hte, call);
608 break;
609 }
610 if (fc != '%') {
611 bad_format_string(hte, call);
612 break;
613 }
614 fc = *fp++;
615 fwidth = prec = left = sign = space = alt = zero = false;
616 sz = NO_TSPEC;
617
618 /* Flags */
619 for (;;) {
620 if (fc == '-') {
621 if (left)
622 break;
623 left = true;
624 } else if (fc == '+') {
625 if (sign)
626 break;
627 sign = true;
628 } else if (fc == ' ') {
629 if (space)
630 break;
631 space = true;
632 } else if (fc == '#') {
633 if (alt)
634 break;
635 alt = true;
636 } else if (fc == '0') {
637 if (zero)
638 break;
639 zero = true;
640 } else {
641 break;
642 }
643 fc = *fp++;
644 }
645
646 /* field width */
647 if (ch_isdigit(fc)) {
648 fwidth = true;
649 do { fc = *fp++; } while (ch_isdigit(fc));
650 } else if (fc == '*') {
651 fwidth = true;
652 fc = *fp++;
653 if ((tp = *ap++) == NULL) {
654 too_few_arguments(hte, call);
655 break;
656 }
657 n++;
658 if ((t1 = tp->t_tspec) != INT && (hflag || t1 != UINT))
659 inconsistent_arguments(hte, call, n);
660 }
661
662 /* precision */
663 if (fc == '.') {
664 fc = *fp++;
665 prec = true;
666 if (ch_isdigit(fc)) {
667 do {
668 fc = *fp++;
669 } while (ch_isdigit(fc));
670 } else if (fc == '*') {
671 fc = *fp++;
672 if ((tp = *ap++) == NULL) {
673 too_few_arguments(hte, call);
674 break;
675 }
676 n++;
677 if (tp->t_tspec != INT)
678 inconsistent_arguments(hte, call, n);
679 } else {
680 bad_format_string(hte, call);
681 break;
682 }
683 }
684
685 if (fc == 'h') {
686 sz = SHORT;
687 } else if (fc == 'l') {
688 sz = LONG;
689 } else if (fc == 'q') {
690 sz = LLONG;
691 } else if (fc == 'L') {
692 sz = LDOUBLE;
693 }
694 if (sz != NO_TSPEC)
695 fc = *fp++;
696
697 if (fc == '%') {
698 if (sz != NO_TSPEC || left || sign || space ||
699 alt || zero || prec || fwidth) {
700 bad_format_string(hte, call);
701 }
702 fc = *fp++;
703 continue;
704 }
705
706 if (fc == '\0') {
707 bad_format_string(hte, call);
708 break;
709 }
710
711 if ((tp = *ap++) == NULL) {
712 too_few_arguments(hte, call);
713 break;
714 }
715 n++;
716 if ((t1 = tp->t_tspec) == PTR)
717 t2 = tp->t_subt->t_tspec;
718
719 if (fc == 'd' || fc == 'i') {
720 if (alt || sz == LDOUBLE) {
721 bad_format_string(hte, call);
722 break;
723 }
724 int_conv:
725 if (sz == LONG) {
726 if (t1 != LONG && (hflag || t1 != ULONG))
727 inconsistent_arguments(hte, call, n);
728 } else if (sz == LLONG) {
729 if (t1 != LLONG && (hflag || t1 != ULLONG))
730 inconsistent_arguments(hte, call, n);
731 } else {
732 /*
733 * SHORT is always promoted to INT, USHORT to
734 * INT or UINT.
735 */
736 if (t1 != INT && (hflag || t1 != UINT))
737 inconsistent_arguments(hte, call, n);
738 }
739 } else if (fc == 'o' || fc == 'u' || fc == 'x' || fc == 'X') {
740 if ((alt && fc == 'u') || sz == LDOUBLE)
741 bad_format_string(hte, call);
742 uint_conv:
743 if (sz == LONG) {
744 if (t1 != ULONG && (hflag || t1 != LONG))
745 inconsistent_arguments(hte, call, n);
746 } else if (sz == LLONG) {
747 if (t1 != ULLONG && (hflag || t1 != LLONG))
748 inconsistent_arguments(hte, call, n);
749 } else if (sz == SHORT) {
750 /* USHORT was promoted to INT or UINT */
751 if (t1 != UINT && t1 != INT)
752 inconsistent_arguments(hte, call, n);
753 } else {
754 if (t1 != UINT && (hflag || t1 != INT))
755 inconsistent_arguments(hte, call, n);
756 }
757 } else if (fc == 'D' || fc == 'O' || fc == 'U') {
758 if ((alt && fc != 'O') || sz != NO_TSPEC || !tflag)
759 bad_format_string(hte, call);
760 sz = LONG;
761 if (fc == 'D') {
762 goto int_conv;
763 } else {
764 goto uint_conv;
765 }
766 } else if (fc == 'f' || fc == 'e' || fc == 'E' ||
767 fc == 'g' || fc == 'G') {
768 if (sz == NO_TSPEC)
769 sz = DOUBLE;
770 if (sz != DOUBLE && sz != LDOUBLE)
771 bad_format_string(hte, call);
772 if (t1 != sz)
773 inconsistent_arguments(hte, call, n);
774 } else if (fc == 'c') {
775 if (sz != NO_TSPEC || alt || zero)
776 bad_format_string(hte, call);
777 if (t1 != INT)
778 inconsistent_arguments(hte, call, n);
779 } else if (fc == 's') {
780 if (sz != NO_TSPEC || alt || zero)
781 bad_format_string(hte, call);
782 if (t1 != PTR ||
783 (t2 != CHAR && t2 != UCHAR && t2 != SCHAR)) {
784 inconsistent_arguments(hte, call, n);
785 }
786 } else if (fc == 'p') {
787 if (fwidth || prec || sz != NO_TSPEC || alt || zero)
788 bad_format_string(hte, call);
789 if (t1 != PTR || (hflag && t2 != VOID))
790 inconsistent_arguments(hte, call, n);
791 } else if (fc == 'n') {
792 if (fwidth || prec || alt || zero || sz == LDOUBLE)
793 bad_format_string(hte, call);
794 if (t1 != PTR) {
795 inconsistent_arguments(hte, call, n);
796 } else if (sz == LONG) {
797 if (t2 != LONG && t2 != ULONG)
798 inconsistent_arguments(hte, call, n);
799 } else if (sz == SHORT) {
800 if (t2 != SHORT && t2 != USHORT)
801 inconsistent_arguments(hte, call, n);
802 } else {
803 if (t2 != INT && t2 != UINT)
804 inconsistent_arguments(hte, call, n);
805 }
806 } else {
807 bad_format_string(hte, call);
808 break;
809 }
810
811 fc = *fp++;
812 }
813 }
814
815 /*
816 * Compare the types in the NULL-terminated array ap with the format
817 * string fmt.
818 */
819 static void
820 scanflike(const hte_t *hte, fcall_t *call, int n, const char *fmt, type_t **ap)
821 {
822 const char *fp;
823 char fc;
824 bool noasgn, fwidth;
825 tspec_t sz, t1 = NO_TSPEC, t2 = NO_TSPEC;
826 type_t *tp = NULL;
827
828 fp = fmt;
829 fc = *fp++;
830
831 for (;;) {
832 if (fc == '\0') {
833 if (*ap != NULL)
834 too_many_arguments(hte, call);
835 break;
836 }
837 if (fc != '%') {
838 bad_format_string(hte, call);
839 break;
840 }
841 fc = *fp++;
842
843 noasgn = fwidth = false;
844 sz = NO_TSPEC;
845
846 if (fc == '*') {
847 noasgn = true;
848 fc = *fp++;
849 }
850
851 if (ch_isdigit(fc)) {
852 fwidth = true;
853 do { fc = *fp++; } while (ch_isdigit(fc));
854 }
855
856 if (fc == 'h') {
857 sz = SHORT;
858 } else if (fc == 'l') {
859 sz = LONG;
860 } else if (fc == 'q') {
861 sz = LLONG;
862 } else if (fc == 'L') {
863 sz = LDOUBLE;
864 }
865 if (sz != NO_TSPEC)
866 fc = *fp++;
867
868 if (fc == '%') {
869 if (sz != NO_TSPEC || noasgn || fwidth)
870 bad_format_string(hte, call);
871 fc = *fp++;
872 continue;
873 }
874
875 if (!noasgn) {
876 if ((tp = *ap++) == NULL) {
877 too_few_arguments(hte, call);
878 break;
879 }
880 n++;
881 if ((t1 = tp->t_tspec) == PTR)
882 t2 = tp->t_subt->t_tspec;
883 }
884
885 if (fc == 'd' || fc == 'i' || fc == 'n') {
886 if (sz == LDOUBLE)
887 bad_format_string(hte, call);
888 if (sz != SHORT && sz != LONG && sz != LLONG)
889 sz = INT;
890 conv:
891 if (!noasgn) {
892 if (t1 != PTR) {
893 inconsistent_arguments(hte, call, n);
894 } else if (t2 != signed_type(sz)) {
895 inconsistent_arguments(hte, call, n);
896 } else if (hflag && t2 != sz) {
897 inconsistent_arguments(hte, call, n);
898 } else if (tp->t_subt->t_const) {
899 inconsistent_arguments(hte, call, n);
900 }
901 }
902 } else if (fc == 'o' || fc == 'u' || fc == 'x') {
903 if (sz == LDOUBLE)
904 bad_format_string(hte, call);
905 if (sz == SHORT) {
906 sz = USHORT;
907 } else if (sz == LONG) {
908 sz = ULONG;
909 } else if (sz == LLONG) {
910 sz = ULLONG;
911 } else {
912 sz = UINT;
913 }
914 goto conv;
915 } else if (fc == 'D') {
916 if (sz != NO_TSPEC || !tflag)
917 bad_format_string(hte, call);
918 sz = LONG;
919 goto conv;
920 } else if (fc == 'O') {
921 if (sz != NO_TSPEC || !tflag)
922 bad_format_string(hte, call);
923 sz = ULONG;
924 goto conv;
925 } else if (fc == 'X') {
926 /*
927 * XXX valid in C90, but in NetBSD's libc implemented
928 * as "lx". That's why it should be avoided.
929 */
930 if (sz != NO_TSPEC || !tflag)
931 bad_format_string(hte, call);
932 sz = ULONG;
933 goto conv;
934 } else if (fc == 'E') {
935 /*
936 * XXX valid in C90, but in NetBSD's libc implemented
937 * as "lf". That's why it should be avoided.
938 */
939 if (sz != NO_TSPEC || !tflag)
940 bad_format_string(hte, call);
941 sz = DOUBLE;
942 goto conv;
943 } else if (fc == 'F') {
944 /* XXX only for backward compatibility */
945 if (sz != NO_TSPEC || !tflag)
946 bad_format_string(hte, call);
947 sz = DOUBLE;
948 goto conv;
949 } else if (fc == 'G') {
950 /*
951 * XXX valid in C90, but in NetBSD's libc not
952 * implemented
953 */
954 if (sz != NO_TSPEC && sz != LONG && sz != LDOUBLE)
955 bad_format_string(hte, call);
956 goto fconv;
957 } else if (fc == 'e' || fc == 'f' || fc == 'g') {
958 fconv:
959 if (sz == NO_TSPEC) {
960 sz = FLOAT;
961 } else if (sz == LONG) {
962 sz = DOUBLE;
963 } else if (sz != LDOUBLE) {
964 bad_format_string(hte, call);
965 sz = FLOAT;
966 }
967 goto conv;
968 } else if (fc == 's' || fc == '[' || fc == 'c') {
969 if (sz != NO_TSPEC)
970 bad_format_string(hte, call);
971 if (fc == '[') {
972 if ((fc = *fp++) == '-') {
973 bad_format_string(hte, call);
974 fc = *fp++;
975 }
976 if (fc != ']') {
977 bad_format_string(hte, call);
978 if (fc == '\0')
979 break;
980 }
981 }
982 if (!noasgn) {
983 if (t1 != PTR) {
984 inconsistent_arguments(hte, call, n);
985 } else if (t2 != CHAR && t2 != UCHAR &&
986 t2 != SCHAR) {
987 inconsistent_arguments(hte, call, n);
988 }
989 }
990 } else if (fc == 'p') {
991 if (sz != NO_TSPEC)
992 bad_format_string(hte, call);
993 if (!noasgn) {
994 if (t1 != PTR || t2 != PTR) {
995 inconsistent_arguments(hte, call, n);
996 } else if (tp->t_subt->t_subt->t_tspec!=VOID) {
997 if (hflag)
998 inconsistent_arguments(hte, call, n);
999 }
1000 }
1001 } else {
1002 bad_format_string(hte, call);
1003 break;
1004 }
1005
1006 fc = *fp++;
1007 }
1008 }
1009
1010 static void
1011 bad_format_string(const hte_t *hte, fcall_t *call)
1012 {
1013
1014 /* %s is called with a malformed format string in %s */
1015 msg(13, hte->h_name, mkpos(&call->f_pos));
1016 }
1017
1018 static void
1019 inconsistent_arguments(const hte_t *hte, fcall_t *call, int n)
1020 {
1021
1022 /* %s is called in %s with argument %d being incompatible with ... */
1023 msg(14, hte->h_name, mkpos(&call->f_pos), n);
1024 }
1025
1026 static void
1027 too_few_arguments(const hte_t *hte, fcall_t *call)
1028 {
1029
1030 /* %s is called in %s with too few arguments for format string */
1031 msg(15, hte->h_name, mkpos(&call->f_pos));
1032 }
1033
1034 static void
1035 too_many_arguments(const hte_t *hte, fcall_t *call)
1036 {
1037
1038 /* %s is called in %s with too many arguments for format string */
1039 msg(16, hte->h_name, mkpos(&call->f_pos));
1040 }
1041
1042 /*
1043 * List of functions where we usually don't care about their result.
1044 * NB: Must be sorted.
1045 */
1046 static const char ignorelist[][8] = {
1047 "memcpy",
1048 "memmove",
1049 "memset",
1050 "printf",
1051 "strcat",
1052 "strcpy",
1053 "vprintf",
1054 };
1055
1056 /*
1057 * Print warnings for return values which are used but not returned,
1058 * or return values which are always or sometimes ignored.
1059 */
1060 static void
1061 check_return_values(const hte_t *hte, sym_t *def)
1062 {
1063 fcall_t *call;
1064 bool used, ignored;
1065
1066 if (def == NULL)
1067 /* don't know whether or not the functions returns a value */
1068 return;
1069
1070 if (hte->h_calls == NULL)
1071 return;
1072
1073 if (def->s_function_has_return_value) {
1074 /*
1075 * XXX as soon as we are able to disable single warnings, the
1076 * following dependencies from hflag should be removed. But for
1077 * now I don't want to be bothered by these warnings which are
1078 * almost always useless.
1079 */
1080 if (!hflag)
1081 return;
1082 if (hflag && bsearch(hte->h_name, ignorelist,
1083 sizeof(ignorelist) / sizeof(ignorelist[0]),
1084 sizeof(ignorelist[0]),
1085 (int (*)(const void *, const void *))strcmp) != NULL)
1086 return;
1087
1088 /* function has return value */
1089 used = ignored = false;
1090 for (call = hte->h_calls; call != NULL; call = call->f_next) {
1091 used |= call->f_rused || call->f_rdisc;
1092 ignored |= !call->f_rused && !call->f_rdisc;
1093 }
1094 if (!used && ignored) {
1095 /* %s returns a value that is always ignored */
1096 msg(8, hte->h_name);
1097 } else if (used && ignored) {
1098 /* %s returns a value that is sometimes ignored */
1099 msg(9, hte->h_name);
1100 }
1101 } else {
1102 /* function has no return value */
1103 for (call = hte->h_calls; call != NULL; call = call->f_next) {
1104 if (call->f_rused)
1105 /* %s has its return value used in %s but doesn't return one */
1106 msg(10, hte->h_name, mkpos(&call->f_pos));
1107 }
1108 }
1109 }
1110
1111 /*
1112 * Print warnings for inconsistent argument declarations.
1113 */
1114 static void
1115 check_argument_declarations(const hte_t *hte, sym_t *def, sym_t *decl)
1116 {
1117 bool osdef, eq, dowarn;
1118 int n;
1119 sym_t *sym1, *sym;
1120 type_t **ap1, **ap2, *tp1, *tp2;
1121
1122 osdef = false;
1123 if (def != NULL) {
1124 osdef = def->s_old_style_function;
1125 sym1 = def;
1126 } else if (decl != NULL && TP(decl->s_type)->t_proto) {
1127 sym1 = decl;
1128 } else {
1129 return;
1130 }
1131 if (TP(sym1->s_type)->t_tspec != FUNC)
1132 return;
1133
1134 /*
1135 * XXX Prototypes should also be compared with old-style function
1136 * declarations.
1137 */
1138
1139 for (sym = hte->h_syms; sym != NULL; sym = sym->s_next) {
1140 if (sym == sym1 || !TP(sym->s_type)->t_proto)
1141 continue;
1142 ap1 = TP(sym1->s_type)->t_args;
1143 ap2 = TP(sym->s_type)->t_args;
1144 n = 0;
1145 while (*ap1 != NULL && *ap2 != NULL) {
1146 type_t *xt1, *xt2;
1147 dowarn = false;
1148 eq = types_compatible(xt1 = *ap1, xt2 = *ap2,
1149 true, osdef, false, &dowarn);
1150 if (!eq || dowarn) {
1151 /* %s has parameter %d declared as '%s' ... */
1152 msg(11, hte->h_name, n + 1,
1153 type_name(xt1), mkpos(&sym1->s_pos),
1154 type_name(xt2), mkpos(&sym->s_pos));
1155 }
1156 n++;
1157 ap1++;
1158 ap2++;
1159 }
1160 if (*ap1 == *ap2) {
1161 tp1 = TP(sym1->s_type);
1162 tp2 = TP(sym->s_type);
1163 if (tp1->t_vararg == tp2->t_vararg)
1164 continue;
1165 if (tp2->t_vararg && sym1->s_check_only_first_args &&
1166 sym1->s_check_num_args == n && !sflag) {
1167 continue;
1168 }
1169 }
1170 /* %s has %d parameters in %s, versus %d in %s */
1171 msg(12, hte->h_name,
1172 total_args(n, ap1), mkpos(&sym1->s_pos),
1173 total_args(n, ap2), mkpos(&sym->s_pos));
1174 }
1175 }
1176
1177
1178 /*
1179 * Check compatibility of two types. Returns whether types are compatible.
1180 *
1181 * ignqual if set, ignore qualifiers of outermost type; used for
1182 * function arguments
1183 * promote if set, promote left type before comparison; used for
1184 * comparisons of arguments with parameters of old-style
1185 * definitions
1186 * asgn left indirected type must have at least the same qualifiers
1187 * like right indirected type (for assignments and function
1188 * arguments)
1189 * *dowarn set to true if an old-style declaration was compared with
1190 * an incompatible prototype declaration
1191 */
1192 static bool
1193 types_compatible(type_t *tp1, type_t *tp2,
1194 bool ignqual, bool promot, bool asgn, bool *dowarn)
1195 {
1196 tspec_t t, to;
1197 int indir;
1198
1199 to = NO_TSPEC;
1200 indir = 0;
1201
1202 while (tp1 != NULL && tp2 != NULL) {
1203
1204 t = tp1->t_tspec;
1205 if (promot) {
1206 if (t == FLOAT) {
1207 t = DOUBLE;
1208 } else if (t == CHAR || t == SCHAR) {
1209 t = INT;
1210 } else if (t == UCHAR) {
1211 t = tflag ? UINT : INT;
1212 } else if (t == SHORT) {
1213 t = INT;
1214 } else if (t == USHORT) {
1215 t = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
1216 }
1217 }
1218
1219 if (asgn && to == PTR) {
1220 if (indir == 1 && (t == VOID || tp2->t_tspec == VOID))
1221 return true;
1222 }
1223
1224 if (t != tp2->t_tspec) {
1225 /*
1226 * Give pointer to types which differ only in
1227 * signedness a chance if not sflag and not hflag.
1228 */
1229 if (sflag || hflag || to != PTR)
1230 return false;
1231 if (signed_type(t) != signed_type(tp2->t_tspec))
1232 return false;
1233 }
1234
1235 if (tp1->t_is_enum && tp2->t_is_enum) {
1236 if (tp1->t_istag && tp2->t_istag) {
1237 return tp1->t_tag == tp2->t_tag;
1238 } else if (tp1->t_istynam && tp2->t_istynam) {
1239 return tp1->t_tynam == tp2->t_tynam;
1240 } else if (tp1->t_isuniqpos && tp2->t_isuniqpos) {
1241 return (tp1->t_uniqpos.p_line ==
1242 tp2->t_uniqpos.p_line &&
1243 tp1->t_uniqpos.p_file ==
1244 tp2->t_uniqpos.p_file &&
1245 tp1->t_uniqpos.p_uniq ==
1246 tp2->t_uniqpos.p_uniq);
1247 } else {
1248 return false;
1249 }
1250 }
1251
1252 /*
1253 * XXX Handle combinations of enum and int if eflag is set. But
1254 * note: enum and 0 should be allowed.
1255 */
1256
1257 if (asgn && indir == 1) {
1258 if (!tp1->t_const && tp2->t_const)
1259 return false;
1260 if (!tp1->t_volatile && tp2->t_volatile)
1261 return false;
1262 } else if (!ignqual && !tflag) {
1263 if (tp1->t_const != tp2->t_const)
1264 return false;
1265 if (tp1->t_const != tp2->t_const)
1266 return false;
1267 }
1268
1269 if (t == STRUCT || t == UNION) {
1270 if (tp1->t_istag && tp2->t_istag) {
1271 return tp1->t_tag == tp2->t_tag;
1272 } else if (tp1->t_istynam && tp2->t_istynam) {
1273 return tp1->t_tynam == tp2->t_tynam;
1274 } else if (tp1->t_isuniqpos && tp2->t_isuniqpos) {
1275 return (tp1->t_uniqpos.p_line ==
1276 tp2->t_uniqpos.p_line &&
1277 tp1->t_uniqpos.p_file ==
1278 tp2->t_uniqpos.p_file &&
1279 tp1->t_uniqpos.p_uniq ==
1280 tp2->t_uniqpos.p_uniq);
1281 } else {
1282 return false;
1283 }
1284 }
1285
1286 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
1287 if (tp1->t_dim != 0 && tp2->t_dim != 0)
1288 return false;
1289 }
1290
1291 if (t == FUNC) {
1292 if (tp1->t_proto && tp2->t_proto) {
1293 if (!prototypes_compatible(tp1, tp2, dowarn))
1294 return false;
1295 } else if (tp1->t_proto) {
1296 if (!matches_no_arg_function(tp1, dowarn))
1297 return false;
1298 } else if (tp2->t_proto) {
1299 if (!matches_no_arg_function(tp2, dowarn))
1300 return false;
1301 }
1302 }
1303
1304 tp1 = tp1->t_subt;
1305 tp2 = tp2->t_subt;
1306 ignqual = promot = false;
1307 to = t;
1308 indir++;
1309 }
1310
1311 return tp1 == tp2;
1312 }
1313
1314 /*
1315 * Compares arguments of two prototypes
1316 */
1317 static bool
1318 prototypes_compatible(type_t *tp1, type_t *tp2, bool *dowarn)
1319 {
1320 type_t **a1, **a2;
1321
1322 if (tp1->t_vararg != tp2->t_vararg)
1323 return false;
1324
1325 a1 = tp1->t_args;
1326 a2 = tp2->t_args;
1327
1328 while (*a1 != NULL && *a2 != NULL) {
1329
1330 if (!types_compatible(*a1, *a2, true, false, false, dowarn))
1331 return false;
1332
1333 a1++;
1334 a2++;
1335 }
1336
1337 return *a1 == *a2;
1338 }
1339
1340 /*
1341 * Returns whether all parameters of a prototype are compatible with an
1342 * old-style function declaration.
1343 *
1344 * This is the case if the following conditions are met:
1345 * 1. the prototype must have a fixed number of parameters
1346 * 2. no parameter is of type float
1347 * 3. no parameter is converted to another type if integer promotion
1348 * is applied on it
1349 */
1350 static bool
1351 matches_no_arg_function(type_t *tp, bool *dowarn)
1352 {
1353 type_t **arg;
1354 tspec_t t;
1355
1356 if (tp->t_vararg && dowarn != NULL)
1357 *dowarn = true;
1358 for (arg = tp->t_args; *arg != NULL; arg++) {
1359 if ((t = (*arg)->t_tspec) == FLOAT)
1360 return false;
1361 if (t == CHAR || t == SCHAR || t == UCHAR)
1362 return false;
1363 if (t == SHORT || t == USHORT)
1364 return false;
1365 }
1366 return true;
1367 }
1368