Home | History | Annotate | Line # | Download | only in error
      1 /*	$NetBSD: filter.c,v 1.20 2023/08/26 14:59:44 rillig Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)filter.c	8.1 (Berkeley) 6/6/93";
     36 #endif
     37 __RCSID("$NetBSD: filter.c,v 1.20 2023/08/26 14:59:44 rillig Exp $");
     38 #endif /* not lint */
     39 
     40 #include <sys/param.h>
     41 #include <pwd.h>
     42 #include <unistd.h>
     43 #include <stdio.h>
     44 #include <ctype.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include "error.h"
     48 #include "pathnames.h"
     49 
     50 static const char *lint_libs[] = {
     51 	IG_FILE1,
     52 	IG_FILE2,
     53 	IG_FILE3,
     54 	IG_FILE4,
     55 	NULL
     56 };
     57 
     58 static int lexsort(const void *, const void *);
     59 static int search_ignore(const char *);
     60 
     61 /*
     62  * Read the file ERRORNAME of the names of functions in lint
     63  * to ignore complaints about.
     64  */
     65 void
     66 getignored(const char *auxname)
     67 {
     68 	int i;
     69 	FILE *fyle;
     70 	char inbuffer[256];
     71 	uid_t uid;
     72 	char filename[MAXPATHLEN];
     73 	const char *username;
     74 	struct passwd *passwdentry;
     75 
     76 	nignored = 0;
     77 	if (auxname == 0) {	/* use the default */
     78 		if ((username = getlogin()) == NULL) {
     79 			username = "Unknown";
     80 			uid = getuid();
     81 			if ((passwdentry = getpwuid(uid)) == NULL) {
     82 				return;
     83 			}
     84 		} else {
     85 			if ((passwdentry = getpwnam(username)) == NULL)
     86 				return;
     87 		}
     88 		strlcpy(filename, passwdentry->pw_dir, sizeof(filename));
     89 		(void)strlcat(filename, ERRORNAME, sizeof(filename));
     90 	} else
     91 		(void)strlcpy(filename, auxname, sizeof(filename));
     92 #ifdef FULLDEBUG
     93 	printf("Opening file \"%s\" to read names to ignore.\n",
     94 		filename);
     95 #endif
     96 	if ((fyle = fopen(filename, "r")) == NULL) {
     97 #ifdef FULLDEBUG
     98 		fprintf(stderr, "%s: Can't open file \"%s\"\n",
     99 			processname, filename);
    100 #endif
    101 		return;
    102 	}
    103 
    104 	/*
    105 	 * Make the first pass through the file, counting lines
    106 	 */
    107 	for (nignored = 0;
    108 	     fgets(inbuffer, sizeof(inbuffer)-1, fyle) != NULL; nignored++)
    109 		continue;
    110 	names_ignored = Calloc(nignored+1, sizeof (char *));
    111 	rewind(fyle);
    112 	for (i=0; i < nignored &&
    113 	          (fgets (inbuffer, sizeof(inbuffer)-1, fyle) != NULL); i++) {
    114 		names_ignored[i] = Strdup(inbuffer);
    115 		(void)substitute(names_ignored[i], '\n', '\0');
    116 	}
    117 	qsort(names_ignored, nignored, sizeof *names_ignored, lexsort);
    118 	fclose(fyle);
    119 #ifdef FULLDEBUG
    120 	printf("Names to ignore follow.\n");
    121 	for (i=0; i < nignored; i++) {
    122 		printf("\tIgnore: %s\n", names_ignored[i]);
    123 	}
    124 #endif
    125 }
    126 
    127 static int
    128 lexsort(const void *c1, const void *c2)
    129 {
    130 	const char *const *cpp1;
    131 	const char *const *cpp2;
    132 
    133 	cpp1 = c1;
    134 	cpp2 = c2;
    135 	return strcmp(*cpp1, *cpp2);
    136 }
    137 
    138 static int
    139 search_ignore(const char *key)
    140 {
    141 	int ub, lb;
    142 	int halfway;
    143 	int order;
    144 
    145 	if (nignored == 0)
    146 		return -1;
    147 	for (lb = 0, ub = nignored - 1; ub >= lb; ) {
    148 		halfway = (ub + lb)/2;
    149 		if ( (order = strcmp(key, names_ignored[halfway])) == 0)
    150 			return halfway;
    151 		if (order < 0)	/* key is less than probe, throw away above */
    152 			ub = halfway - 1;
    153 		 else
    154 			lb = halfway + 1;
    155 	}
    156 	return -1;
    157 }
    158 
    159 /*
    160  * Tell if the error text is to be ignored.
    161  * The error must have been canonicalized, with
    162  * the file name the zeroth entry in the errorv,
    163  * and the linenumber the second.
    164  * Return the new categorization of the error class.
    165  */
    166 Errorclass
    167 discardit(Eptr errorp)
    168 {
    169 	int i;
    170 	Errorclass errorclass = errorp->error_e_class;
    171 
    172 	switch (errorclass) {
    173 		case C_SYNC:
    174 		case C_NONSPEC:
    175 		case C_UNKNOWN: return errorclass;
    176 		default:	;
    177 	}
    178 	if (errorp->error_lgtext < 2) {
    179 		return C_NONSPEC;
    180 	}
    181 	if (errorp->error_language == INLINT) {
    182 		if (errorclass != C_NONSPEC) {	/* no file */
    183 			for (i=0; lint_libs[i] != 0; i++) {
    184 				if (strcmp(errorp->error_text[0], lint_libs[i]) == 0) {
    185 					return C_DISCARD;
    186 				}
    187 			}
    188 		}
    189 		/* check if the argument to the error message is to be ignored */
    190 		if (ispunct((unsigned char)lastchar(errorp->error_text[2])))
    191 			clob_last(errorp->error_text[2], '\0');
    192 		if (search_ignore(errorp->error_text[errorclass == C_NONSPEC ? 0 : 2]) >= 0) {
    193 			return C_NULLED;
    194 		}
    195 	}
    196 	return errorclass;
    197 }
    198