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