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