chk.c revision 1.70 1 /* $NetBSD: chk.c,v 1.70 2025/04/10 20:37:48 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.70 2025/04/10 20:37:48 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 sym_t *sym;
295 type_t *tp1, *tp2;
296 bool eq, dowarn;
297
298 if (def == NULL)
299 def = decl;
300 if (def == NULL)
301 return;
302
303 tp1 = TP(def->s_type);
304 for (sym = hte->h_syms; sym != NULL; sym = sym->s_next) {
305 type_t *xt1, *xt2;
306 if (sym == def)
307 continue;
308 tp2 = TP(sym->s_type);
309 dowarn = false;
310 if (tp1->t_tspec == FUNC && tp2->t_tspec == FUNC) {
311 eq = types_compatible(xt1 = tp1->t_subt,
312 xt2 = tp2->t_subt, true, false, false, &dowarn);
313 } else {
314 eq = types_compatible(xt1 = tp1, xt2 = tp2,
315 false, false, false, &dowarn);
316 }
317 if (!eq || (sflag && dowarn)) {
318 /* %s returns '%s' at %s, versus '%s' at %s */
319 msg(5, hte->h_name, type_name(xt1), mkpos(&def->s_pos),
320 type_name(xt2), mkpos(&sym->s_pos));
321 }
322 }
323 }
324
325 static int
326 total_args(int n, type_t **tpp)
327 {
328 for (; *tpp != NULL; tpp++)
329 n++;
330 return n;
331 }
332
333 /*
334 * Print a warning if a function is called with arguments which does
335 * not match the function definition, declaration or another call
336 * of the same function.
337 */
338 static void
339 chkfaui(const hte_t *hte, sym_t *def, sym_t *decl)
340 {
341 type_t *tp1, *tp2, **ap1, **ap2;
342 pos_t *pos1p = NULL;
343 fcall_t *calls, *call, *call1;
344 int n, as;
345 arginf_t *ai;
346
347 if ((calls = hte->h_calls) == NULL)
348 return;
349
350 /*
351 * If we find a function definition, we use this for comparison,
352 * otherwise the first prototype we can find. If there is no definition
353 * or prototype declaration, the first function call is used.
354 */
355 tp1 = NULL;
356 call1 = NULL;
357 if (def != NULL) {
358 if ((tp1 = TP(def->s_type))->t_tspec != FUNC)
359 return;
360 pos1p = &def->s_pos;
361 } else if (decl != NULL && TP(decl->s_type)->t_proto) {
362 if ((tp1 = TP(decl->s_type))->t_tspec != FUNC)
363 return;
364 pos1p = &decl->s_pos;
365 }
366 if (tp1 == NULL) {
367 call1 = calls;
368 calls = calls->f_next;
369 if ((tp1 = TP(call1->f_type))->t_tspec != FUNC)
370 return;
371 pos1p = &call1->f_pos;
372 }
373
374 n = 1;
375 for (call = calls; call != NULL; call = call->f_next) {
376 if ((tp2 = TP(call->f_type))->t_tspec != FUNC)
377 continue;
378 ap1 = tp1->t_args;
379 ap2 = tp2->t_args;
380 n = 0;
381 while (*ap1 != NULL && *ap2 != NULL) {
382 if (def != NULL && def->s_check_only_first_args &&
383 n >= def->s_check_num_args)
384 break;
385 n++;
386 chkau(hte, n, def, decl, pos1p, call1, call,
387 *ap1, *ap2);
388 ap1++;
389 ap2++;
390 }
391 if (*ap1 == *ap2) {
392 /* equal # of arguments */
393 } else if (def != NULL && def->s_check_only_first_args &&
394 n >= def->s_check_num_args) {
395 /*
396 * function definition with VARARGS; The # of arguments
397 * of the call must be at least as large as the
398 * parameter of VARARGS.
399 */
400 } else if (*ap2 != NULL && tp1->t_proto && tp1->t_vararg) {
401 /*
402 * prototype with ... and function call with at least
403 * the same # of arguments as declared in the
404 * prototype.
405 */
406 } else {
407 /* %s has %d parameters in %s, versus %d ... */
408 msg(7, hte->h_name, total_args(n, ap1), mkpos(pos1p),
409 total_args(n, ap2), mkpos(&call->f_pos));
410 continue;
411 }
412
413 /* perform SCANFLIKE/PRINTFLIKE tests */
414 if (def == NULL || (!def->s_printflike && !def->s_scanflike))
415 continue;
416 as = def->s_printflike
417 ? def->s_printflike_arg
418 : def->s_scanflike_arg;
419 for (ai = call->f_args; ai != NULL; ai = ai->a_next) {
420 if (ai->a_num == as)
421 break;
422 }
423 if (ai == NULL || !ai->a_fmt)
424 continue;
425 if (def->s_printflike) {
426 printflike(hte, call, n, ai->a_fstrg, ap2);
427 } else {
428 scanflike(hte, call, n, ai->a_fstrg, ap2);
429 }
430 }
431 }
432
433 /*
434 * Check a single argument in a function call.
435 *
436 * hte a pointer to the hash table entry of the function
437 * n the number of the argument (1..)
438 * def the function definition or NULL
439 * decl prototype declaration, old-style declaration or NULL
440 * pos1p position of definition, declaration of first call
441 * call1 first call, if both def and decl are old-style def/decl
442 * call checked call
443 * arg1 currently checked argument of def/decl/call1
444 * arg2 currently checked argument of call
445 */
446 static void
447 chkau(const hte_t *hte, int n, sym_t *def, sym_t *decl, pos_t *pos1p,
448 fcall_t *call1, fcall_t *call, type_t *arg1, type_t *arg2)
449 {
450 bool promote, asgn, dowarn;
451 tspec_t t1, t2;
452 arginf_t *ai, *ai1;
453
454 /*
455 * If a function definition is available (def != NULL), we compare the
456 * function call (call) with the definition. Otherwise, if a function
457 * definition is available and it is not an old-style definition (decl
458 * != NULL && TP(decl->s_type)->t_proto), we compare the call with this
459 * declaration. Otherwise we compare it with the first call we have
460 * found (call1).
461 */
462
463 /* arg1 must be promoted if it stems from an old-style definition */
464 promote = def != NULL && def->s_old_style_function;
465
466 /*
467 * If we compare with a definition or declaration, we must perform the
468 * same checks for qualifiers in indirected types as in assignments.
469 */
470 asgn = def != NULL || (decl != NULL && TP(decl->s_type)->t_proto);
471
472 dowarn = false;
473 if (types_compatible(arg1, arg2, true, promote, asgn, &dowarn) &&
474 (!sflag || !dowarn))
475 return;
476
477 /*-
478 * Other lint implementations print warnings as soon as the type of an
479 * argument does not match exactly the expected type. The result are
480 * lots of warnings which are really not necessary.
481 * We print a warning only if
482 * (0) at least one type is not an integer type and types differ
483 * (1) hflag is set and types differ
484 * (2) types differ, except in signedness
485 *
486 * If the argument is an integer constant whose msb is not set,
487 * signedness is ignored (e.g. 0 matches both signed and unsigned int).
488 * This is with and without hflag.
489 *
490 * If the argument is an integer constant with value 0 and the expected
491 * argument is of type pointer and the width of the integer constant is
492 * the same as the width of the pointer, no warning is printed.
493 */
494 t1 = arg1->t_tspec;
495 t2 = arg2->t_tspec;
496 if (is_integer(t1) && is_integer(t2) &&
497 !arg1->t_is_enum && !arg2->t_is_enum) {
498 if (promote) {
499 /*
500 * XXX Here is a problem: Although it is possible to
501 * pass an int where a char/short it expected, there
502 * may be loss in significant digits. We should first
503 * check for const arguments if they can be converted
504 * into the original parameter type.
505 */
506 if (t1 == FLOAT) {
507 t1 = DOUBLE;
508 } else if (t1 == CHAR || t1 == SCHAR) {
509 t1 = INT;
510 } else if (t1 == UCHAR) {
511 t1 = tflag ? UINT : INT;
512 } else if (t1 == SHORT) {
513 t1 = INT;
514 } else if (t1 == USHORT) {
515 t1 = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
516 }
517 }
518
519 if (signed_type(t1) == signed_type(t2)) {
520
521 /*
522 * types differ only in signedness; get information
523 * about arguments
524 */
525
526 /*
527 * treat a definition like a call with variable
528 * arguments
529 */
530 ai1 = call1 != NULL ? call1->f_args : NULL;
531
532 /*
533 * if two calls are compared, ai1 is set to the
534 * information for the n-th argument, if this was a
535 * constant, otherwise to NULL
536 */
537 for ( ; ai1 != NULL; ai1 = ai1->a_next) {
538 if (ai1->a_num == n)
539 break;
540 }
541 /*
542 * ai is set to the information of the n-th arg of the
543 * (second) call, if this was a constant, otherwise to
544 * NULL
545 */
546 for (ai = call->f_args; ai != NULL; ai = ai->a_next) {
547 if (ai->a_num == n)
548 break;
549 }
550
551 if (ai1 == NULL && ai == NULL) {
552 /* no constant at all */
553 if (!hflag)
554 return;
555 } else if (ai1 == NULL || ai == NULL) {
556 /* one constant */
557 if (ai == NULL)
558 ai = ai1;
559 if (ai->a_zero || ai->a_pcon)
560 /* same value in signed and unsigned */
561 return;
562 /* value (not representation) differently */
563 } else {
564 /*
565 * two constants, one signed, one unsigned; if
566 * the msb of one of the constants is set, the
567 * argument is used inconsistently.
568 */
569 if (!ai1->a_ncon && !ai->a_ncon)
570 return;
571 }
572 }
573
574 } else if (t1 == PTR && is_integer(t2)) {
575 for (ai = call->f_args; ai != NULL; ai = ai->a_next) {
576 if (ai->a_num == n)
577 break;
578 }
579 /*
580 * Vendor implementations of lint (e.g. HP-UX, Digital UNIX)
581 * don't care about the size of the integer argument, only
582 * whether or not it is zero. We do the same.
583 */
584 if (ai != NULL && ai->a_zero)
585 return;
586 }
587
588 /* %s has argument %d with type '%s' at %s, versus '%s' at %s */
589 msg(6, hte->h_name, n, type_name(arg1), mkpos(pos1p),
590 type_name(arg2), mkpos(&call->f_pos));
591 }
592
593 /*
594 * Compare the types in the NULL-terminated array ap with the format
595 * string fmt.
596 */
597 static void
598 printflike(const hte_t *hte, fcall_t *call, int n, const char *fmt, type_t **ap)
599 {
600 const char *fp;
601 char fc;
602 bool fwidth, prec, left, sign, space, alt, zero;
603 tspec_t sz, t1, t2 = NO_TSPEC;
604 type_t *tp;
605
606 fp = fmt;
607 fc = *fp++;
608
609 for (;;) {
610 if (fc == '\0') {
611 if (*ap != NULL)
612 too_many_arguments(hte, call);
613 break;
614 }
615 if (fc != '%') {
616 bad_format_string(hte, call);
617 break;
618 }
619 fc = *fp++;
620 fwidth = prec = left = sign = space = alt = zero = false;
621 sz = NO_TSPEC;
622
623 /* Flags */
624 for (;;) {
625 if (fc == '-') {
626 if (left)
627 break;
628 left = true;
629 } else if (fc == '+') {
630 if (sign)
631 break;
632 sign = true;
633 } else if (fc == ' ') {
634 if (space)
635 break;
636 space = true;
637 } else if (fc == '#') {
638 if (alt)
639 break;
640 alt = true;
641 } else if (fc == '0') {
642 if (zero)
643 break;
644 zero = true;
645 } else {
646 break;
647 }
648 fc = *fp++;
649 }
650
651 /* field width */
652 if (ch_isdigit(fc)) {
653 fwidth = true;
654 do { fc = *fp++; } while (ch_isdigit(fc));
655 } else if (fc == '*') {
656 fwidth = true;
657 fc = *fp++;
658 if ((tp = *ap++) == NULL) {
659 too_few_arguments(hte, call);
660 break;
661 }
662 n++;
663 if ((t1 = tp->t_tspec) != INT && (hflag || t1 != UINT))
664 inconsistent_arguments(hte, call, n);
665 }
666
667 /* precision */
668 if (fc == '.') {
669 fc = *fp++;
670 prec = true;
671 if (ch_isdigit(fc)) {
672 do {
673 fc = *fp++;
674 } while (ch_isdigit(fc));
675 } else if (fc == '*') {
676 fc = *fp++;
677 if ((tp = *ap++) == NULL) {
678 too_few_arguments(hte, call);
679 break;
680 }
681 n++;
682 if (tp->t_tspec != INT)
683 inconsistent_arguments(hte, call, n);
684 } else {
685 bad_format_string(hte, call);
686 break;
687 }
688 }
689
690 if (fc == 'h') {
691 sz = SHORT;
692 } else if (fc == 'l') {
693 sz = LONG;
694 } else if (fc == 'q') {
695 sz = LLONG;
696 } else if (fc == 'L') {
697 sz = LDOUBLE;
698 }
699 if (sz != NO_TSPEC)
700 fc = *fp++;
701
702 if (fc == '%') {
703 if (sz != NO_TSPEC || left || sign || space ||
704 alt || zero || prec || fwidth) {
705 bad_format_string(hte, call);
706 }
707 fc = *fp++;
708 continue;
709 }
710
711 if (fc == '\0') {
712 bad_format_string(hte, call);
713 break;
714 }
715
716 if ((tp = *ap++) == NULL) {
717 too_few_arguments(hte, call);
718 break;
719 }
720 n++;
721 if ((t1 = tp->t_tspec) == PTR)
722 t2 = tp->t_subt->t_tspec;
723
724 if (fc == 'd' || fc == 'i') {
725 if (alt || sz == LDOUBLE) {
726 bad_format_string(hte, call);
727 break;
728 }
729 int_conv:
730 if (sz == LONG) {
731 if (t1 != LONG && (hflag || t1 != ULONG))
732 inconsistent_arguments(hte, call, n);
733 } else if (sz == LLONG) {
734 if (t1 != LLONG && (hflag || t1 != ULLONG))
735 inconsistent_arguments(hte, call, n);
736 } else {
737 /*
738 * SHORT is always promoted to INT, USHORT to
739 * INT or UINT.
740 */
741 if (t1 != INT && (hflag || t1 != UINT))
742 inconsistent_arguments(hte, call, n);
743 }
744 } else if (fc == 'o' || fc == 'u' || fc == 'x' || fc == 'X') {
745 if ((alt && fc == 'u') || sz == LDOUBLE)
746 bad_format_string(hte, call);
747 uint_conv:
748 if (sz == LONG) {
749 if (t1 != ULONG && (hflag || t1 != LONG))
750 inconsistent_arguments(hte, call, n);
751 } else if (sz == LLONG) {
752 if (t1 != ULLONG && (hflag || t1 != LLONG))
753 inconsistent_arguments(hte, call, n);
754 } else if (sz == SHORT) {
755 /* USHORT was promoted to INT or UINT */
756 if (t1 != UINT && t1 != INT)
757 inconsistent_arguments(hte, call, n);
758 } else {
759 if (t1 != UINT && (hflag || t1 != INT))
760 inconsistent_arguments(hte, call, n);
761 }
762 } else if (fc == 'D' || fc == 'O' || fc == 'U') {
763 if ((alt && fc != 'O') || sz != NO_TSPEC || !tflag)
764 bad_format_string(hte, call);
765 sz = LONG;
766 if (fc == 'D') {
767 goto int_conv;
768 } else {
769 goto uint_conv;
770 }
771 } else if (fc == 'f' || fc == 'e' || fc == 'E' ||
772 fc == 'g' || fc == 'G') {
773 if (sz == NO_TSPEC)
774 sz = DOUBLE;
775 if (sz != DOUBLE && sz != LDOUBLE)
776 bad_format_string(hte, call);
777 if (t1 != sz)
778 inconsistent_arguments(hte, call, n);
779 } else if (fc == 'c') {
780 if (sz != NO_TSPEC || alt || zero)
781 bad_format_string(hte, call);
782 if (t1 != INT)
783 inconsistent_arguments(hte, call, n);
784 } else if (fc == 's') {
785 if (sz != NO_TSPEC || alt || zero)
786 bad_format_string(hte, call);
787 if (t1 != PTR ||
788 (t2 != CHAR && t2 != UCHAR && t2 != SCHAR)) {
789 inconsistent_arguments(hte, call, n);
790 }
791 } else if (fc == 'p') {
792 if (fwidth || prec || sz != NO_TSPEC || alt || zero)
793 bad_format_string(hte, call);
794 if (t1 != PTR || (hflag && t2 != VOID))
795 inconsistent_arguments(hte, call, n);
796 } else if (fc == 'n') {
797 if (fwidth || prec || alt || zero || sz == LDOUBLE)
798 bad_format_string(hte, call);
799 if (t1 != PTR) {
800 inconsistent_arguments(hte, call, n);
801 } else if (sz == LONG) {
802 if (t2 != LONG && t2 != ULONG)
803 inconsistent_arguments(hte, call, n);
804 } else if (sz == SHORT) {
805 if (t2 != SHORT && t2 != USHORT)
806 inconsistent_arguments(hte, call, n);
807 } else {
808 if (t2 != INT && t2 != UINT)
809 inconsistent_arguments(hte, call, n);
810 }
811 } else {
812 bad_format_string(hte, call);
813 break;
814 }
815
816 fc = *fp++;
817 }
818 }
819
820 /*
821 * Compare the types in the NULL-terminated array ap with the format
822 * string fmt.
823 */
824 static void
825 scanflike(const hte_t *hte, fcall_t *call, int n, const char *fmt, type_t **ap)
826 {
827 const char *fp;
828 char fc;
829 bool noasgn, fwidth;
830 tspec_t sz, t1 = NO_TSPEC, t2 = NO_TSPEC;
831 type_t *tp = NULL;
832
833 fp = fmt;
834 fc = *fp++;
835
836 for (;;) {
837 if (fc == '\0') {
838 if (*ap != NULL)
839 too_many_arguments(hte, call);
840 break;
841 }
842 if (fc != '%') {
843 bad_format_string(hte, call);
844 break;
845 }
846 fc = *fp++;
847
848 noasgn = fwidth = false;
849 sz = NO_TSPEC;
850
851 if (fc == '*') {
852 noasgn = true;
853 fc = *fp++;
854 }
855
856 if (ch_isdigit(fc)) {
857 fwidth = true;
858 do { fc = *fp++; } while (ch_isdigit(fc));
859 }
860
861 if (fc == 'h') {
862 sz = SHORT;
863 } else if (fc == 'l') {
864 sz = LONG;
865 } else if (fc == 'q') {
866 sz = LLONG;
867 } else if (fc == 'L') {
868 sz = LDOUBLE;
869 }
870 if (sz != NO_TSPEC)
871 fc = *fp++;
872
873 if (fc == '%') {
874 if (sz != NO_TSPEC || noasgn || fwidth)
875 bad_format_string(hte, call);
876 fc = *fp++;
877 continue;
878 }
879
880 if (!noasgn) {
881 if ((tp = *ap++) == NULL) {
882 too_few_arguments(hte, call);
883 break;
884 }
885 n++;
886 if ((t1 = tp->t_tspec) == PTR)
887 t2 = tp->t_subt->t_tspec;
888 }
889
890 if (fc == 'd' || fc == 'i' || fc == 'n') {
891 if (sz == LDOUBLE)
892 bad_format_string(hte, call);
893 if (sz != SHORT && sz != LONG && sz != LLONG)
894 sz = INT;
895 conv:
896 if (!noasgn) {
897 if (t1 != PTR) {
898 inconsistent_arguments(hte, call, n);
899 } else if (t2 != signed_type(sz)) {
900 inconsistent_arguments(hte, call, n);
901 } else if (hflag && t2 != sz) {
902 inconsistent_arguments(hte, call, n);
903 } else if (tp->t_subt->t_const) {
904 inconsistent_arguments(hte, call, n);
905 }
906 }
907 } else if (fc == 'o' || fc == 'u' || fc == 'x') {
908 if (sz == LDOUBLE)
909 bad_format_string(hte, call);
910 if (sz == SHORT) {
911 sz = USHORT;
912 } else if (sz == LONG) {
913 sz = ULONG;
914 } else if (sz == LLONG) {
915 sz = ULLONG;
916 } else {
917 sz = UINT;
918 }
919 goto conv;
920 } else if (fc == 'D') {
921 if (sz != NO_TSPEC || !tflag)
922 bad_format_string(hte, call);
923 sz = LONG;
924 goto conv;
925 } else if (fc == 'O') {
926 if (sz != NO_TSPEC || !tflag)
927 bad_format_string(hte, call);
928 sz = ULONG;
929 goto conv;
930 } else if (fc == 'X') {
931 /*
932 * XXX valid in C90, but in NetBSD's libc implemented
933 * as "lx". That's why it should be avoided.
934 */
935 if (sz != NO_TSPEC || !tflag)
936 bad_format_string(hte, call);
937 sz = ULONG;
938 goto conv;
939 } else if (fc == 'E') {
940 /*
941 * XXX valid in C90, but in NetBSD's libc implemented
942 * as "lf". That's why it should be avoided.
943 */
944 if (sz != NO_TSPEC || !tflag)
945 bad_format_string(hte, call);
946 sz = DOUBLE;
947 goto conv;
948 } else if (fc == 'F') {
949 /* XXX only for backward compatibility */
950 if (sz != NO_TSPEC || !tflag)
951 bad_format_string(hte, call);
952 sz = DOUBLE;
953 goto conv;
954 } else if (fc == 'G') {
955 /*
956 * XXX valid in C90, but in NetBSD's libc not
957 * implemented
958 */
959 if (sz != NO_TSPEC && sz != LONG && sz != LDOUBLE)
960 bad_format_string(hte, call);
961 goto fconv;
962 } else if (fc == 'e' || fc == 'f' || fc == 'g') {
963 fconv:
964 if (sz == NO_TSPEC) {
965 sz = FLOAT;
966 } else if (sz == LONG) {
967 sz = DOUBLE;
968 } else if (sz != LDOUBLE) {
969 bad_format_string(hte, call);
970 sz = FLOAT;
971 }
972 goto conv;
973 } else if (fc == 's' || fc == '[' || fc == 'c') {
974 if (sz != NO_TSPEC)
975 bad_format_string(hte, call);
976 if (fc == '[') {
977 if ((fc = *fp++) == '-') {
978 bad_format_string(hte, call);
979 fc = *fp++;
980 }
981 if (fc != ']') {
982 bad_format_string(hte, call);
983 if (fc == '\0')
984 break;
985 }
986 }
987 if (!noasgn) {
988 if (t1 != PTR) {
989 inconsistent_arguments(hte, call, n);
990 } else if (t2 != CHAR && t2 != UCHAR &&
991 t2 != SCHAR) {
992 inconsistent_arguments(hte, call, n);
993 }
994 }
995 } else if (fc == 'p') {
996 if (sz != NO_TSPEC)
997 bad_format_string(hte, call);
998 if (!noasgn) {
999 if (t1 != PTR || t2 != PTR) {
1000 inconsistent_arguments(hte, call, n);
1001 } else if (tp->t_subt->t_subt->t_tspec!=VOID) {
1002 if (hflag)
1003 inconsistent_arguments(hte, call, n);
1004 }
1005 }
1006 } else {
1007 bad_format_string(hte, call);
1008 break;
1009 }
1010
1011 fc = *fp++;
1012 }
1013 }
1014
1015 static void
1016 bad_format_string(const hte_t *hte, fcall_t *call)
1017 {
1018
1019 /* %s is called with a malformed format string in %s */
1020 msg(13, hte->h_name, mkpos(&call->f_pos));
1021 }
1022
1023 static void
1024 inconsistent_arguments(const hte_t *hte, fcall_t *call, int n)
1025 {
1026
1027 /* %s is called in %s with argument %d being incompatible with ... */
1028 msg(14, hte->h_name, mkpos(&call->f_pos), n);
1029 }
1030
1031 static void
1032 too_few_arguments(const hte_t *hte, fcall_t *call)
1033 {
1034
1035 /* %s is called in %s with too few arguments for format string */
1036 msg(15, hte->h_name, mkpos(&call->f_pos));
1037 }
1038
1039 static void
1040 too_many_arguments(const hte_t *hte, fcall_t *call)
1041 {
1042
1043 /* %s is called in %s with too many arguments for format string */
1044 msg(16, hte->h_name, mkpos(&call->f_pos));
1045 }
1046
1047 /*
1048 * List of functions where we usually don't care about their result.
1049 * NB: Must be sorted.
1050 */
1051 static const char ignorelist[][8] = {
1052 "memcpy",
1053 "memmove",
1054 "memset",
1055 "printf",
1056 "strcat",
1057 "strcpy",
1058 "vprintf",
1059 };
1060
1061 /*
1062 * Print warnings for return values which are used but not returned,
1063 * or return values which are always or sometimes ignored.
1064 */
1065 static void
1066 check_return_values(const hte_t *hte, sym_t *def)
1067 {
1068 fcall_t *call;
1069 bool used, ignored;
1070
1071 if (def == NULL)
1072 /* don't know whether or not the functions returns a value */
1073 return;
1074
1075 if (hte->h_calls == NULL)
1076 return;
1077
1078 if (def->s_function_has_return_value) {
1079 /*
1080 * XXX as soon as we are able to disable single warnings, the
1081 * following dependencies from hflag should be removed. But for
1082 * now I don't want to be bothered by these warnings which are
1083 * almost always useless.
1084 */
1085 if (!hflag)
1086 return;
1087 if (hflag && bsearch(hte->h_name, ignorelist,
1088 sizeof(ignorelist) / sizeof(ignorelist[0]),
1089 sizeof(ignorelist[0]),
1090 (int (*)(const void *, const void *))strcmp) != NULL)
1091 return;
1092
1093 /* function has return value */
1094 used = ignored = false;
1095 for (call = hte->h_calls; call != NULL; call = call->f_next) {
1096 used |= call->f_rused || call->f_rdisc;
1097 ignored |= !call->f_rused && !call->f_rdisc;
1098 }
1099 if (!used && ignored) {
1100 /* %s returns a value that is always ignored */
1101 msg(8, hte->h_name);
1102 } else if (used && ignored) {
1103 /* %s returns a value that is sometimes ignored */
1104 msg(9, hte->h_name);
1105 }
1106 } else {
1107 /* function has no return value */
1108 for (call = hte->h_calls; call != NULL; call = call->f_next) {
1109 if (call->f_rused)
1110 /* %s has its return value used in %s but doesn't return one */
1111 msg(10, hte->h_name, mkpos(&call->f_pos));
1112 }
1113 }
1114 }
1115
1116 /*
1117 * Print warnings for inconsistent argument declarations.
1118 */
1119 static void
1120 check_argument_declarations(const hte_t *hte, sym_t *def, sym_t *decl)
1121 {
1122 bool osdef, eq, dowarn;
1123 int n;
1124 sym_t *sym1, *sym;
1125 type_t **ap1, **ap2, *tp1, *tp2;
1126
1127 osdef = false;
1128 if (def != NULL) {
1129 osdef = def->s_old_style_function;
1130 sym1 = def;
1131 } else if (decl != NULL && TP(decl->s_type)->t_proto) {
1132 sym1 = decl;
1133 } else {
1134 return;
1135 }
1136 if (TP(sym1->s_type)->t_tspec != FUNC)
1137 return;
1138
1139 /*
1140 * XXX Prototypes should also be compared with old-style function
1141 * declarations.
1142 */
1143
1144 for (sym = hte->h_syms; sym != NULL; sym = sym->s_next) {
1145 if (sym == sym1 || !TP(sym->s_type)->t_proto)
1146 continue;
1147 ap1 = TP(sym1->s_type)->t_args;
1148 ap2 = TP(sym->s_type)->t_args;
1149 n = 0;
1150 while (*ap1 != NULL && *ap2 != NULL) {
1151 type_t *xt1, *xt2;
1152 dowarn = false;
1153 eq = types_compatible(xt1 = *ap1, xt2 = *ap2,
1154 true, osdef, false, &dowarn);
1155 if (!eq || dowarn) {
1156 /* %s has parameter %d declared as '%s' ... */
1157 msg(11, hte->h_name, n + 1,
1158 type_name(xt1), mkpos(&sym1->s_pos),
1159 type_name(xt2), mkpos(&sym->s_pos));
1160 }
1161 n++;
1162 ap1++;
1163 ap2++;
1164 }
1165 if (*ap1 == *ap2) {
1166 tp1 = TP(sym1->s_type);
1167 tp2 = TP(sym->s_type);
1168 if (tp1->t_vararg == tp2->t_vararg)
1169 continue;
1170 if (tp2->t_vararg && sym1->s_check_only_first_args &&
1171 sym1->s_check_num_args == n && !sflag) {
1172 continue;
1173 }
1174 }
1175 /* %s has %d parameters in %s, versus %d in %s */
1176 msg(12, hte->h_name,
1177 total_args(n, ap1), mkpos(&sym1->s_pos),
1178 total_args(n, ap2), mkpos(&sym->s_pos));
1179 }
1180 }
1181
1182
1183 /*
1184 * Check compatibility of two types. Returns whether types are compatible.
1185 *
1186 * ignqual if set, ignore qualifiers of outermost type; used for
1187 * function arguments
1188 * promote if set, promote left type before comparison; used for
1189 * comparisons of arguments with parameters of old-style
1190 * definitions
1191 * asgn left indirected type must have at least the same qualifiers
1192 * like right indirected type (for assignments and function
1193 * arguments)
1194 * *dowarn set to true if an old-style declaration was compared with
1195 * an incompatible prototype declaration
1196 */
1197 static bool
1198 types_compatible(type_t *tp1, type_t *tp2,
1199 bool ignqual, bool promot, bool asgn, bool *dowarn)
1200 {
1201 tspec_t t, to;
1202 int indir;
1203
1204 to = NO_TSPEC;
1205 indir = 0;
1206
1207 while (tp1 != NULL && tp2 != NULL) {
1208
1209 t = tp1->t_tspec;
1210 if (promot) {
1211 if (t == FLOAT) {
1212 t = DOUBLE;
1213 } else if (t == CHAR || t == SCHAR) {
1214 t = INT;
1215 } else if (t == UCHAR) {
1216 t = tflag ? UINT : INT;
1217 } else if (t == SHORT) {
1218 t = INT;
1219 } else if (t == USHORT) {
1220 t = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
1221 }
1222 }
1223
1224 if (asgn && to == PTR) {
1225 if (indir == 1 && (t == VOID || tp2->t_tspec == VOID))
1226 return true;
1227 }
1228
1229 if (t != tp2->t_tspec) {
1230 /*
1231 * Give pointer to types which differ only in
1232 * signedness a chance if not sflag and not hflag.
1233 */
1234 if (sflag || hflag || to != PTR)
1235 return false;
1236 if (signed_type(t) != signed_type(tp2->t_tspec))
1237 return false;
1238 }
1239
1240 if (tp1->t_is_enum && tp2->t_is_enum) {
1241 if (tp1->t_istag && tp2->t_istag) {
1242 return tp1->t_tag == tp2->t_tag;
1243 } else if (tp1->t_istynam && tp2->t_istynam) {
1244 return tp1->t_tynam == tp2->t_tynam;
1245 } else if (tp1->t_isuniqpos && tp2->t_isuniqpos) {
1246 return (tp1->t_uniqpos.p_line ==
1247 tp2->t_uniqpos.p_line &&
1248 tp1->t_uniqpos.p_file ==
1249 tp2->t_uniqpos.p_file &&
1250 tp1->t_uniqpos.p_uniq ==
1251 tp2->t_uniqpos.p_uniq);
1252 } else {
1253 return false;
1254 }
1255 }
1256
1257 /*
1258 * XXX Handle combinations of enum and int if eflag is set. But
1259 * note: enum and 0 should be allowed.
1260 */
1261
1262 if (asgn && indir == 1) {
1263 if (!tp1->t_const && tp2->t_const)
1264 return false;
1265 if (!tp1->t_volatile && tp2->t_volatile)
1266 return false;
1267 } else if (!ignqual && !tflag) {
1268 if (tp1->t_const != tp2->t_const)
1269 return false;
1270 if (tp1->t_const != tp2->t_const)
1271 return false;
1272 }
1273
1274 if (t == STRUCT || t == UNION) {
1275 if (tp1->t_istag && tp2->t_istag) {
1276 return tp1->t_tag == tp2->t_tag;
1277 } else if (tp1->t_istynam && tp2->t_istynam) {
1278 return tp1->t_tynam == tp2->t_tynam;
1279 } else if (tp1->t_isuniqpos && tp2->t_isuniqpos) {
1280 return (tp1->t_uniqpos.p_line ==
1281 tp2->t_uniqpos.p_line &&
1282 tp1->t_uniqpos.p_file ==
1283 tp2->t_uniqpos.p_file &&
1284 tp1->t_uniqpos.p_uniq ==
1285 tp2->t_uniqpos.p_uniq);
1286 } else {
1287 return false;
1288 }
1289 }
1290
1291 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
1292 if (tp1->t_dim != 0 && tp2->t_dim != 0)
1293 return false;
1294 }
1295
1296 if (t == FUNC) {
1297 if (tp1->t_proto && tp2->t_proto) {
1298 if (!prototypes_compatible(tp1, tp2, dowarn))
1299 return false;
1300 } else if (tp1->t_proto) {
1301 if (!matches_no_arg_function(tp1, dowarn))
1302 return false;
1303 } else if (tp2->t_proto) {
1304 if (!matches_no_arg_function(tp2, dowarn))
1305 return false;
1306 }
1307 }
1308
1309 tp1 = tp1->t_subt;
1310 tp2 = tp2->t_subt;
1311 ignqual = promot = false;
1312 to = t;
1313 indir++;
1314 }
1315
1316 return tp1 == tp2;
1317 }
1318
1319 /*
1320 * Compares arguments of two prototypes
1321 */
1322 static bool
1323 prototypes_compatible(type_t *tp1, type_t *tp2, bool *dowarn)
1324 {
1325 type_t **a1, **a2;
1326
1327 if (tp1->t_vararg != tp2->t_vararg)
1328 return false;
1329
1330 a1 = tp1->t_args;
1331 a2 = tp2->t_args;
1332
1333 while (*a1 != NULL && *a2 != NULL) {
1334
1335 if (!types_compatible(*a1, *a2, true, false, false, dowarn))
1336 return false;
1337
1338 a1++;
1339 a2++;
1340 }
1341
1342 return *a1 == *a2;
1343 }
1344
1345 /*
1346 * Returns whether all parameters of a prototype are compatible with an
1347 * old-style function declaration.
1348 *
1349 * This is the case if the following conditions are met:
1350 * 1. the prototype must have a fixed number of parameters
1351 * 2. no parameter is of type float
1352 * 3. no parameter is converted to another type if integer promotion
1353 * is applied on it
1354 */
1355 static bool
1356 matches_no_arg_function(type_t *tp, bool *dowarn)
1357 {
1358 type_t **arg;
1359 tspec_t t;
1360
1361 if (tp->t_vararg && dowarn != NULL)
1362 *dowarn = true;
1363 for (arg = tp->t_args; *arg != NULL; arg++) {
1364 if ((t = (*arg)->t_tspec) == FLOAT)
1365 return false;
1366 if (t == CHAR || t == SCHAR || t == UCHAR)
1367 return false;
1368 if (t == SHORT || t == USHORT)
1369 return false;
1370 }
1371 return true;
1372 }
1373