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