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