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