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