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