chk.c revision 1.26 1 /* $NetBSD: chk.c,v 1.26 2020/12/28 19:47:42 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.26 2020/12/28 19:47:42 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 int eqtype(type_t *, type_t *, int, int, int, int *);
69 static int eqargs(type_t *, type_t *, int *);
70 static int mnoarg(type_t *, int *);
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", 0)) != NULL)
82 hte->h_used = 1;
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_nxt) {
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_nxt) {
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_nxt) {
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 int 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_nxt) {
255 tp2 = TP(call->f_type)->t_subt;
256 eq = eqtype(tp1, tp2, 1, 0, 0, (dowarn = 0, &dowarn));
257 if (!call->f_rused) {
258 /* no return value used */
259 if ((t1 == STRUCT || t1 == UNION) && !eq) {
260 /*
261 * If a function returns a struct or union it
262 * must be declared to return a struct or
263 * union, also if the return value is ignored.
264 * This is necessary because the caller must
265 * allocate stack space for the return value.
266 * If it does not, the return value would over-
267 * write other data.
268 * XXX Following massage may be confusing
269 * because it appears also if the return value
270 * was declared inconsistently. But this
271 * behaviour matches pcc based lint, so it is
272 * accepted for now.
273 */
274 pos1 = xstrdup(mkpos(&def->s_pos));
275 /* %s value must be decl. before use %s :: %s */
276 msg(17, hte->h_name,
277 pos1, mkpos(&call->f_pos));
278 free(pos1);
279 }
280 continue;
281 }
282 if (!eq || (sflag && dowarn)) {
283 pos1 = xstrdup(mkpos(&def->s_pos));
284 /* %s value used inconsistenty\t%s :: %s */
285 msg(4, hte->h_name, pos1, mkpos(&call->f_pos));
286 free(pos1);
287 }
288 }
289 }
290
291 /*
292 * Print a warning if a definition/declaration does not match another
293 * definition/declaration of the same name. For functions, only the
294 * types of return values are tested.
295 */
296 static void
297 chkvtdi(hte_t *hte, sym_t *def, sym_t *decl)
298 {
299 sym_t *sym;
300 type_t *tp1, *tp2;
301 int eq, dowarn;
302 char *pos1;
303
304 if (def == NULL)
305 def = decl;
306 if (def == NULL)
307 return;
308
309 tp1 = TP(def->s_type);
310 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
311 type_t *xt1, *xt2;
312 if (sym == def)
313 continue;
314 tp2 = TP(sym->s_type);
315 dowarn = 0;
316 if (tp1->t_tspec == FUNC && tp2->t_tspec == FUNC) {
317 eq = eqtype(xt1 = tp1->t_subt, xt2 = tp2->t_subt,
318 1, 0, 0, &dowarn);
319 } else {
320 eq = eqtype(xt1 = tp1, xt2 = tp2, 0, 0, 0, &dowarn);
321 }
322 if (!eq || (sflag && dowarn)) {
323 char b1[64], b2[64];
324 pos1 = xstrdup(mkpos(&def->s_pos));
325 /* %s value declared inconsistently\t%s :: %s */
326 msg(5, hte->h_name, tyname(b1, sizeof(b1), xt1),
327 tyname(b2, sizeof(b2), xt2), pos1,
328 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_nxt;
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_nxt) {
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_nxt) {
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 int promote, asgn, dowarn;
452 tspec_t t1, t2;
453 arginf_t *ai, *ai1;
454 char *pos1;
455 char tyname1[64], tyname2[64];
456
457 /*
458 * If a function definition is available (def != NULL), we compair the
459 * function call (call) with the definition. Otherwise, if a function
460 * definition is available and it is not an old style definition
461 * (decl != NULL && TP(decl->s_type)->t_proto), we compair the call
462 * with this declaration. Otherwise we compair it with the first
463 * call we have found (call1).
464 */
465
466 /* arg1 must be promoted if it stems from an old style definition */
467 promote = def != NULL && def->s_osdef;
468
469 /*
470 * If we compair with a definition or declaration, we must perform
471 * the same checks for qualifiers in indirected types as in
472 * assignments.
473 */
474 asgn = def != NULL || (decl != NULL && TP(decl->s_type)->t_proto);
475
476 dowarn = 0;
477 if (eqtype(arg1, arg2, 1, promote, asgn, &dowarn) && (!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 interger 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 * interger 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 (tspec_is_int(t1) && tspec_is_int(t2) &&
499 !arg1->t_isenum && !arg2->t_isenum) {
500 if (promote) {
501 /*
502 * XXX Here is a problem: Althrough 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 (styp(t1) == styp(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_nxt) {
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_nxt) {
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 && tspec_is_int(t2)) {
578 for (ai = call->f_args; ai != NULL; ai = ai->a_nxt) {
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,
594 tyname(tyname1, sizeof(tyname1), arg1),
595 mkpos(&call->f_pos),
596 tyname(tyname2, sizeof(tyname2), arg2));
597 free(pos1);
598 }
599
600 /*
601 * Compare the types in the NULL-terminated array ap with the format
602 * string fmt.
603 */
604 static void
605 printflike(hte_t *hte, fcall_t *call, int n, const char *fmt, type_t **ap)
606 {
607 const char *fp;
608 int fc;
609 int fwidth, prec, left, sign, space, alt, zero;
610 tspec_t sz, t1, t2 = NOTSPEC;
611 type_t *tp;
612
613 fp = fmt;
614 fc = *fp++;
615
616 for ( ; ; ) {
617 if (fc == '\0') {
618 if (*ap != NULL)
619 tomanyarg(hte, call);
620 break;
621 }
622 if (fc != '%') {
623 badfmt(hte, call);
624 break;
625 }
626 fc = *fp++;
627 fwidth = prec = left = sign = space = alt = zero = 0;
628 sz = NOTSPEC;
629
630 /* Flags */
631 for ( ; ; ) {
632 if (fc == '-') {
633 if (left)
634 break;
635 left = 1;
636 } else if (fc == '+') {
637 if (sign)
638 break;
639 sign = 1;
640 } else if (fc == ' ') {
641 if (space)
642 break;
643 space = 1;
644 } else if (fc == '#') {
645 if (alt)
646 break;
647 alt = 1;
648 } else if (fc == '0') {
649 if (zero)
650 break;
651 zero = 1;
652 } else {
653 break;
654 }
655 fc = *fp++;
656 }
657
658 /* field width */
659 if (isdigit(fc)) {
660 fwidth = 1;
661 do { fc = *fp++; } while (isdigit(fc)) ;
662 } else if (fc == '*') {
663 fwidth = 1;
664 fc = *fp++;
665 if ((tp = *ap++) == NULL) {
666 tofewarg(hte, call);
667 break;
668 }
669 n++;
670 if ((t1 = tp->t_tspec) != INT && (hflag || t1 != UINT))
671 inconarg(hte, call, n);
672 }
673
674 /* precision */
675 if (fc == '.') {
676 fc = *fp++;
677 prec = 1;
678 if (isdigit(fc)) {
679 do { fc = *fp++; } while (isdigit(fc));
680 } else if (fc == '*') {
681 fc = *fp++;
682 if ((tp = *ap++) == NULL) {
683 tofewarg(hte, call);
684 break;
685 }
686 n++;
687 if (tp->t_tspec != INT)
688 inconarg(hte, call, n);
689 } else {
690 badfmt(hte, call);
691 break;
692 }
693 }
694
695 if (fc == 'h') {
696 sz = SHORT;
697 } else if (fc == 'l') {
698 sz = LONG;
699 } else if (fc == 'q') {
700 sz = QUAD;
701 } else if (fc == 'L') {
702 sz = LDOUBLE;
703 }
704 if (sz != NOTSPEC)
705 fc = *fp++;
706
707 if (fc == '%') {
708 if (sz != NOTSPEC || left || sign || space ||
709 alt || zero || prec || fwidth) {
710 badfmt(hte, call);
711 }
712 fc = *fp++;
713 continue;
714 }
715
716 if (fc == '\0') {
717 badfmt(hte, call);
718 break;
719 }
720
721 if ((tp = *ap++) == NULL) {
722 tofewarg(hte, call);
723 break;
724 }
725 n++;
726 if ((t1 = tp->t_tspec) == PTR)
727 t2 = tp->t_subt->t_tspec;
728
729 if (fc == 'd' || fc == 'i') {
730 if (alt || sz == LDOUBLE) {
731 badfmt(hte, call);
732 break;
733 }
734 int_conv:
735 if (sz == LONG) {
736 if (t1 != LONG && (hflag || t1 != ULONG))
737 inconarg(hte, call, n);
738 } else if (sz == QUAD) {
739 if (t1 != QUAD && (hflag || t1 != UQUAD))
740 inconarg(hte, call, n);
741 } else {
742 /*
743 * SHORT is always promoted to INT, USHORT
744 * to INT or UINT.
745 */
746 if (t1 != INT && (hflag || t1 != UINT))
747 inconarg(hte, call, n);
748 }
749 } else if (fc == 'o' || fc == 'u' || fc == 'x' || fc == 'X') {
750 if ((alt && fc == 'u') || sz == LDOUBLE)
751 badfmt(hte, call);
752 uint_conv:
753 if (sz == LONG) {
754 if (t1 != ULONG && (hflag || t1 != LONG))
755 inconarg(hte, call, n);
756 } else if (sz == QUAD) {
757 if (t1 != UQUAD && (hflag || t1 != QUAD))
758 inconarg(hte, call, n);
759 } else if (sz == SHORT) {
760 /* USHORT was promoted to INT or UINT */
761 if (t1 != UINT && t1 != INT)
762 inconarg(hte, call, n);
763 } else {
764 if (t1 != UINT && (hflag || t1 != INT))
765 inconarg(hte, call, n);
766 }
767 } else if (fc == 'D' || fc == 'O' || fc == 'U') {
768 if ((alt && fc != 'O') || sz != NOTSPEC || !tflag)
769 badfmt(hte, call);
770 sz = LONG;
771 if (fc == 'D') {
772 goto int_conv;
773 } else {
774 goto uint_conv;
775 }
776 } else if (fc == 'f' || fc == 'e' || fc == 'E' ||
777 fc == 'g' || fc == 'G') {
778 if (sz == NOTSPEC)
779 sz = DOUBLE;
780 if (sz != DOUBLE && sz != LDOUBLE)
781 badfmt(hte, call);
782 if (t1 != sz)
783 inconarg(hte, call, n);
784 } else if (fc == 'c') {
785 if (sz != NOTSPEC || alt || zero)
786 badfmt(hte, call);
787 if (t1 != INT)
788 inconarg(hte, call, n);
789 } else if (fc == 's') {
790 if (sz != NOTSPEC || alt || zero)
791 badfmt(hte, call);
792 if (t1 != PTR ||
793 (t2 != CHAR && t2 != UCHAR && t2 != SCHAR)) {
794 inconarg(hte, call, n);
795 }
796 } else if (fc == 'p') {
797 if (fwidth || prec || sz != NOTSPEC || alt || zero)
798 badfmt(hte, call);
799 if (t1 != PTR || (hflag && t2 != VOID))
800 inconarg(hte, call, n);
801 } else if (fc == 'n') {
802 if (fwidth || prec || alt || zero || sz == LDOUBLE)
803 badfmt(hte, call);
804 if (t1 != PTR) {
805 inconarg(hte, call, n);
806 } else if (sz == LONG) {
807 if (t2 != LONG && t2 != ULONG)
808 inconarg(hte, call, n);
809 } else if (sz == SHORT) {
810 if (t2 != SHORT && t2 != USHORT)
811 inconarg(hte, call, n);
812 } else {
813 if (t2 != INT && t2 != UINT)
814 inconarg(hte, call, n);
815 }
816 } else {
817 badfmt(hte, call);
818 break;
819 }
820
821 fc = *fp++;
822 }
823 }
824
825 /*
826 * Compare the types in the NULL-terminated array ap with the format
827 * string fmt.
828 */
829 static void
830 scanflike(hte_t *hte, fcall_t *call, int n, const char *fmt, type_t **ap)
831 {
832 const char *fp;
833 int fc;
834 int noasgn, fwidth;
835 tspec_t sz, t1 = NOTSPEC, t2 = NOTSPEC;
836 type_t *tp = NULL;
837
838 fp = fmt;
839 fc = *fp++;
840
841 for ( ; ; ) {
842 if (fc == '\0') {
843 if (*ap != NULL)
844 tomanyarg(hte, call);
845 break;
846 }
847 if (fc != '%') {
848 badfmt(hte, call);
849 break;
850 }
851 fc = *fp++;
852
853 noasgn = fwidth = 0;
854 sz = NOTSPEC;
855
856 if (fc == '*') {
857 noasgn = 1;
858 fc = *fp++;
859 }
860
861 if (isdigit(fc)) {
862 fwidth = 1;
863 do { fc = *fp++; } while (isdigit(fc));
864 }
865
866 if (fc == 'h') {
867 sz = SHORT;
868 } else if (fc == 'l') {
869 sz = LONG;
870 } else if (fc == 'q') {
871 sz = QUAD;
872 } else if (fc == 'L') {
873 sz = LDOUBLE;
874 }
875 if (sz != NOTSPEC)
876 fc = *fp++;
877
878 if (fc == '%') {
879 if (sz != NOTSPEC || noasgn || fwidth)
880 badfmt(hte, call);
881 fc = *fp++;
882 continue;
883 }
884
885 if (!noasgn) {
886 if ((tp = *ap++) == NULL) {
887 tofewarg(hte, call);
888 break;
889 }
890 n++;
891 if ((t1 = tp->t_tspec) == PTR)
892 t2 = tp->t_subt->t_tspec;
893 }
894
895 if (fc == 'd' || fc == 'i' || fc == 'n') {
896 if (sz == LDOUBLE)
897 badfmt(hte, call);
898 if (sz != SHORT && sz != LONG && sz != QUAD)
899 sz = INT;
900 conv:
901 if (!noasgn) {
902 if (t1 != PTR) {
903 inconarg(hte, call, n);
904 } else if (t2 != styp(sz)) {
905 inconarg(hte, call, n);
906 } else if (hflag && t2 != sz) {
907 inconarg(hte, call, n);
908 } else if (tp->t_subt->t_const) {
909 inconarg(hte, call, n);
910 }
911 }
912 } else if (fc == 'o' || fc == 'u' || fc == 'x') {
913 if (sz == LDOUBLE)
914 badfmt(hte, call);
915 if (sz == SHORT) {
916 sz = USHORT;
917 } else if (sz == LONG) {
918 sz = ULONG;
919 } else if (sz == QUAD) {
920 sz = UQUAD;
921 } else {
922 sz = UINT;
923 }
924 goto conv;
925 } else if (fc == 'D') {
926 if (sz != NOTSPEC || !tflag)
927 badfmt(hte, call);
928 sz = LONG;
929 goto conv;
930 } else if (fc == 'O') {
931 if (sz != NOTSPEC || !tflag)
932 badfmt(hte, call);
933 sz = ULONG;
934 goto conv;
935 } else if (fc == 'X') {
936 /*
937 * XXX valid in ANSI C, but in NetBSD's libc imple-
938 * mented as "lx". Thats why it should be avoided.
939 */
940 if (sz != NOTSPEC || !tflag)
941 badfmt(hte, call);
942 sz = ULONG;
943 goto conv;
944 } else if (fc == 'E') {
945 /*
946 * XXX valid in ANSI C, but in NetBSD's libc imple-
947 * mented as "lf". Thats why it should be avoided.
948 */
949 if (sz != NOTSPEC || !tflag)
950 badfmt(hte, call);
951 sz = DOUBLE;
952 goto conv;
953 } else if (fc == 'F') {
954 /* XXX only for backward compatibility */
955 if (sz != NOTSPEC || !tflag)
956 badfmt(hte, call);
957 sz = DOUBLE;
958 goto conv;
959 } else if (fc == 'G') {
960 /*
961 * XXX valid in ANSI C, but in NetBSD's libc not
962 * implemented
963 */
964 if (sz != NOTSPEC && sz != LONG && sz != LDOUBLE)
965 badfmt(hte, call);
966 goto fconv;
967 } else if (fc == 'e' || fc == 'f' || fc == 'g') {
968 fconv:
969 if (sz == NOTSPEC) {
970 sz = FLOAT;
971 } else if (sz == LONG) {
972 sz = DOUBLE;
973 } else if (sz != LDOUBLE) {
974 badfmt(hte, call);
975 sz = FLOAT;
976 }
977 goto conv;
978 } else if (fc == 's' || fc == '[' || fc == 'c') {
979 if (sz != NOTSPEC)
980 badfmt(hte, call);
981 if (fc == '[') {
982 if ((fc = *fp++) == '-') {
983 badfmt(hte, call);
984 fc = *fp++;
985 }
986 if (fc != ']') {
987 badfmt(hte, call);
988 if (fc == '\0')
989 break;
990 }
991 }
992 if (!noasgn) {
993 if (t1 != PTR) {
994 inconarg(hte, call, n);
995 } else if (t2 != CHAR && t2 != UCHAR &&
996 t2 != SCHAR) {
997 inconarg(hte, call, n);
998 }
999 }
1000 } else if (fc == 'p') {
1001 if (sz != NOTSPEC)
1002 badfmt(hte, call);
1003 if (!noasgn) {
1004 if (t1 != PTR || t2 != PTR) {
1005 inconarg(hte, call, n);
1006 } else if (tp->t_subt->t_subt->t_tspec!=VOID) {
1007 if (hflag)
1008 inconarg(hte, call, n);
1009 }
1010 }
1011 } else {
1012 badfmt(hte, call);
1013 break;
1014 }
1015
1016 fc = *fp++;
1017 }
1018 }
1019
1020 static void
1021 badfmt(hte_t *hte, fcall_t *call)
1022 {
1023
1024 /* %s: malformed format string\t%s */
1025 msg(13, hte->h_name, mkpos(&call->f_pos));
1026 }
1027
1028 static void
1029 inconarg(hte_t *hte, fcall_t *call, int n)
1030 {
1031
1032 /* %s, arg %d inconsistent with format\t%s(%d) */
1033 msg(14, hte->h_name, n, mkpos(&call->f_pos));
1034 }
1035
1036 static void
1037 tofewarg(hte_t *hte, fcall_t *call)
1038 {
1039
1040 /* %s: too few args for format \t%s */
1041 msg(15, hte->h_name, mkpos(&call->f_pos));
1042 }
1043
1044 static void
1045 tomanyarg(hte_t *hte, fcall_t *call)
1046 {
1047
1048 /* %s: too many args for format \t%s */
1049 msg(16, hte->h_name, mkpos(&call->f_pos));
1050 }
1051
1052 /*
1053 * List of functions where we usually don't care about their result.
1054 * NB: Must be sorted.
1055 */
1056 static const char ignorelist[][8] = {
1057 "memcpy",
1058 "memmove",
1059 "memset",
1060 "printf",
1061 "strcat",
1062 "strcpy",
1063 "vprintf",
1064 };
1065
1066 /*
1067 * Print warnings for return values which are used, but not returned,
1068 * or return values which are always or sometimes ignored.
1069 */
1070 static void
1071 chkrvu(hte_t *hte, sym_t *def)
1072 {
1073 fcall_t *call;
1074 int used, ignored;
1075
1076 if (def == NULL)
1077 /* don't know whether or not the functions returns a value */
1078 return;
1079
1080 if (hte->h_calls == NULL)
1081 return;
1082
1083 if (def->s_rval) {
1084 /*
1085 * XXX as soon as we are able to disable single warnings
1086 * the following dependencies from hflag should be removed.
1087 * but for now I do'nt want to be botherd by this warnings
1088 * which are almost always useless.
1089 */
1090 if (hflag == 0)
1091 return;
1092 if (hflag == 1 && bsearch(hte->h_name, ignorelist,
1093 __arraycount(ignorelist), sizeof(ignorelist[0]),
1094 (int (*)(const void *, const void *))strcmp) != NULL)
1095 return;
1096
1097 /* function has return value */
1098 used = ignored = 0;
1099 for (call = hte->h_calls; call != NULL; call = call->f_nxt) {
1100 used |= call->f_rused || call->f_rdisc;
1101 ignored |= !call->f_rused && !call->f_rdisc;
1102 }
1103 if (!used && ignored) {
1104 /* %s returns value which is always ignored */
1105 msg(8, hte->h_name);
1106 } else if (used && ignored) {
1107 /* %s returns value which is sometimes ign. */
1108 msg(9, hte->h_name);
1109 }
1110 } else {
1111 /* function has no return value */
1112 for (call = hte->h_calls; call != NULL; call = call->f_nxt) {
1113 if (call->f_rused)
1114 /* %s value is used( %s ), but none ret. */
1115 msg(10, hte->h_name, mkpos(&call->f_pos));
1116 }
1117 }
1118 }
1119
1120 /*
1121 * Print warnings for inconsistent argument declarations.
1122 */
1123 static void
1124 chkadecl(hte_t *hte, sym_t *def, sym_t *decl)
1125 {
1126 int osdef, eq, dowarn, n;
1127 sym_t *sym1, *sym;
1128 type_t **ap1, **ap2, *tp1, *tp2;
1129 char *pos1;
1130 const char *pos2;
1131
1132 osdef = 0;
1133 if (def != NULL) {
1134 osdef = def->s_osdef;
1135 sym1 = def;
1136 } else if (decl != NULL && TP(decl->s_type)->t_proto) {
1137 sym1 = decl;
1138 } else {
1139 return;
1140 }
1141 if (TP(sym1->s_type)->t_tspec != FUNC)
1142 return;
1143
1144 /*
1145 * XXX Prototypes should also be compared with old style function
1146 * declarations.
1147 */
1148
1149 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
1150 if (sym == sym1 || !TP(sym->s_type)->t_proto)
1151 continue;
1152 ap1 = TP(sym1->s_type)->t_args;
1153 ap2 = TP(sym->s_type)->t_args;
1154 n = 0;
1155 while (*ap1 != NULL && *ap2 != NULL) {
1156 type_t *xt1, *xt2;
1157 dowarn = 0;
1158 eq = eqtype(xt1 = *ap1, xt2 = *ap2, 1, osdef, 0, &dowarn);
1159 if (!eq || dowarn) {
1160 char b1[64], b2[64];
1161 pos1 = xstrdup(mkpos(&sym1->s_pos));
1162 pos2 = mkpos(&sym->s_pos);
1163 /* %s, arg %d declared inconsistently ... */
1164 msg(11, hte->h_name, n + 1,
1165 tyname(b1, sizeof(b1), xt1),
1166 tyname(b2, sizeof(b2), xt2), pos1, pos2);
1167 free(pos1);
1168 }
1169 n++;
1170 ap1++;
1171 ap2++;
1172 }
1173 if (*ap1 == *ap2) {
1174 tp1 = TP(sym1->s_type);
1175 tp2 = TP(sym->s_type);
1176 if (tp1->t_vararg == tp2->t_vararg)
1177 continue;
1178 if (tp2->t_vararg &&
1179 sym1->s_va && sym1->s_nva == n && !sflag) {
1180 continue;
1181 }
1182 }
1183 /* %s: variable # of args declared\t%s :: %s */
1184 pos1 = xstrdup(mkpos(&sym1->s_pos));
1185 msg(12, hte->h_name, pos1, mkpos(&sym->s_pos));
1186 free(pos1);
1187 }
1188 }
1189
1190
1191 /*
1192 * Check compatibility of two types. Returns 1 if types are compatible,
1193 * otherwise 0.
1194 *
1195 * ignqual if set, ignore qualifiers of outhermost type; used for
1196 * function arguments
1197 * promote if set, promote left type before comparison; used for
1198 * comparisons of arguments with parameters of old style
1199 * definitions
1200 * asgn left indirected type must have at least the same qualifiers
1201 * like right indirected type (for assignments and function
1202 * arguments)
1203 * *dowarn set to 1 if an old style declaration was compared with
1204 * an incompatible prototype declaration
1205 */
1206 static int
1207 eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int asgn, int *dowarn)
1208 {
1209 tspec_t t, to;
1210 int indir;
1211
1212 to = NOTSPEC;
1213 indir = 0;
1214
1215 while (tp1 != NULL && tp2 != NULL) {
1216
1217 t = tp1->t_tspec;
1218 if (promot) {
1219 if (t == FLOAT) {
1220 t = DOUBLE;
1221 } else if (t == CHAR || t == SCHAR) {
1222 t = INT;
1223 } else if (t == UCHAR) {
1224 t = tflag ? UINT : INT;
1225 } else if (t == SHORT) {
1226 t = INT;
1227 } else if (t == USHORT) {
1228 /* CONSTCOND */
1229 t = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
1230 }
1231 }
1232
1233 if (asgn && to == PTR) {
1234 if (indir == 1 && (t == VOID || tp2->t_tspec == VOID))
1235 return (1);
1236 }
1237
1238 if (t != tp2->t_tspec) {
1239 /*
1240 * Give pointer to types which differ only in
1241 * signedness a chance if not sflag and not hflag.
1242 */
1243 if (sflag || hflag || to != PTR)
1244 return (0);
1245 if (styp(t) != styp(tp2->t_tspec))
1246 return (0);
1247 }
1248
1249 if (tp1->t_isenum && tp2->t_isenum) {
1250 if (tp1->t_istag && tp2->t_istag) {
1251 return (tp1->t_tag == tp2->t_tag);
1252 } else if (tp1->t_istynam && tp2->t_istynam) {
1253 return (tp1->t_tynam == tp2->t_tynam);
1254 } else if (tp1->t_isuniqpos && tp2->t_isuniqpos) {
1255 return (tp1->t_uniqpos.p_line ==
1256 tp2->t_uniqpos.p_line &&
1257 tp1->t_uniqpos.p_file ==
1258 tp2->t_uniqpos.p_file &&
1259 tp1->t_uniqpos.p_uniq ==
1260 tp2->t_uniqpos.p_uniq);
1261 } else {
1262 return (0);
1263 }
1264 }
1265
1266 /*
1267 * XXX Handle combinations of enum and int if eflag is set.
1268 * But note: enum and 0 should be allowed.
1269 */
1270
1271 if (asgn && indir == 1) {
1272 if (!tp1->t_const && tp2->t_const)
1273 return (0);
1274 if (!tp1->t_volatile && tp2->t_volatile)
1275 return (0);
1276 } else if (!ignqual && !tflag) {
1277 if (tp1->t_const != tp2->t_const)
1278 return (0);
1279 if (tp1->t_const != tp2->t_const)
1280 return (0);
1281 }
1282
1283 if (t == STRUCT || t == UNION) {
1284 if (tp1->t_istag && tp2->t_istag) {
1285 return (tp1->t_tag == tp2->t_tag);
1286 } else if (tp1->t_istynam && tp2->t_istynam) {
1287 return (tp1->t_tynam == tp2->t_tynam);
1288 } else if (tp1->t_isuniqpos && tp2->t_isuniqpos) {
1289 return (tp1->t_uniqpos.p_line ==
1290 tp2->t_uniqpos.p_line &&
1291 tp1->t_uniqpos.p_file ==
1292 tp2->t_uniqpos.p_file &&
1293 tp1->t_uniqpos.p_uniq ==
1294 tp2->t_uniqpos.p_uniq);
1295 } else {
1296 return (0);
1297 }
1298 }
1299
1300 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
1301 if (tp1->t_dim != 0 && tp2->t_dim != 0)
1302 return (0);
1303 }
1304
1305 if (t == FUNC) {
1306 if (tp1->t_proto && tp2->t_proto) {
1307 if (!eqargs(tp1, tp2, dowarn))
1308 return (0);
1309 } else if (tp1->t_proto) {
1310 if (!mnoarg(tp1, dowarn))
1311 return (0);
1312 } else if (tp2->t_proto) {
1313 if (!mnoarg(tp2, dowarn))
1314 return (0);
1315 }
1316 }
1317
1318 tp1 = tp1->t_subt;
1319 tp2 = tp2->t_subt;
1320 ignqual = promot = 0;
1321 to = t;
1322 indir++;
1323
1324 }
1325
1326 return (tp1 == tp2);
1327 }
1328
1329 /*
1330 * Compares arguments of two prototypes
1331 */
1332 static int
1333 eqargs(type_t *tp1, type_t *tp2, int *dowarn)
1334 {
1335 type_t **a1, **a2;
1336
1337 if (tp1->t_vararg != tp2->t_vararg)
1338 return (0);
1339
1340 a1 = tp1->t_args;
1341 a2 = tp2->t_args;
1342
1343 while (*a1 != NULL && *a2 != NULL) {
1344
1345 if (eqtype(*a1, *a2, 1, 0, 0, dowarn) == 0)
1346 return (0);
1347
1348 a1++;
1349 a2++;
1350
1351 }
1352
1353 return (*a1 == *a2);
1354 }
1355
1356 /*
1357 * mnoarg() (matches functions with no argument type information)
1358 * returns 1 if all parameters of a prototype are compatible with
1359 * and old style function declaration.
1360 * This is the case if following conditions are met:
1361 * 1. the prototype must have a fixed number of parameters
1362 * 2. no parameter is of type float
1363 * 3. no parameter is converted to another type if integer promotion
1364 * is applied on it
1365 */
1366 static int
1367 mnoarg(type_t *tp, int *dowarn)
1368 {
1369 type_t **arg;
1370 tspec_t t;
1371
1372 if (tp->t_vararg && dowarn != NULL)
1373 *dowarn = 1;
1374 for (arg = tp->t_args; *arg != NULL; arg++) {
1375 if ((t = (*arg)->t_tspec) == FLOAT)
1376 return (0);
1377 if (t == CHAR || t == SCHAR || t == UCHAR)
1378 return (0);
1379 if (t == SHORT || t == USHORT)
1380 return (0);
1381 }
1382 return (1);
1383 }
1384