Module Name:    src
Committed By:   rillig
Date:           Mon Jan 29 21:30:25 UTC 2024

Modified Files:
        src/usr.bin/xlint/lint1: ckgetopt.c debug.c emit1.c lex.c lint1.h
            tree.c

Log Message:
lint: do not remember content of wide string literals

The plain char literals are needed for checking printf/scanf format
strings; lint has no similar check for wide strings. These format
strings are checked by modern compilers, making this check less
relevant.


To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.19 src/usr.bin/xlint/lint1/ckgetopt.c
cvs rdiff -u -r1.66 -r1.67 src/usr.bin/xlint/lint1/debug.c
cvs rdiff -u -r1.81 -r1.82 src/usr.bin/xlint/lint1/emit1.c
cvs rdiff -u -r1.203 -r1.204 src/usr.bin/xlint/lint1/lex.c
cvs rdiff -u -r1.209 -r1.210 src/usr.bin/xlint/lint1/lint1.h
cvs rdiff -u -r1.598 -r1.599 src/usr.bin/xlint/lint1/tree.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/xlint/lint1/ckgetopt.c
diff -u src/usr.bin/xlint/lint1/ckgetopt.c:1.18 src/usr.bin/xlint/lint1/ckgetopt.c:1.19
--- src/usr.bin/xlint/lint1/ckgetopt.c:1.18	Mon Jan 29 21:04:21 2024
+++ src/usr.bin/xlint/lint1/ckgetopt.c	Mon Jan 29 21:30:24 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: ckgetopt.c,v 1.18 2024/01/29 21:04:21 rillig Exp $ */
+/* $NetBSD: ckgetopt.c,v 1.19 2024/01/29 21:30:24 rillig Exp $ */
 
 /*-
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: ckgetopt.c,v 1.18 2024/01/29 21:04:21 rillig Exp $");
+__RCSID("$NetBSD: ckgetopt.c,v 1.19 2024/01/29 21:30:24 rillig Exp $");
 #endif
 
 #include <stdbool.h>
@@ -100,7 +100,7 @@ is_getopt_condition(const tnode_t *tn, c
 	    && last_arg->tn_left->tn_op == ADDR
 	    && last_arg->tn_left->tn_left->tn_op == STRING
 	    && (str = last_arg->tn_left->tn_left->tn_string)->st_char) {
-		*out_options = xstrdup(str->st_mem);
+		*out_options = xstrdup(str->st_chars);
 		return true;
 	}
 	return false;

Index: src/usr.bin/xlint/lint1/debug.c
diff -u src/usr.bin/xlint/lint1/debug.c:1.66 src/usr.bin/xlint/lint1/debug.c:1.67
--- src/usr.bin/xlint/lint1/debug.c:1.66	Tue Jan 23 19:44:28 2024
+++ src/usr.bin/xlint/lint1/debug.c	Mon Jan 29 21:30:24 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: debug.c,v 1.66 2024/01/23 19:44:28 rillig Exp $ */
+/* $NetBSD: debug.c,v 1.67 2024/01/29 21:30:24 rillig Exp $ */
 
 /*-
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: debug.c,v 1.66 2024/01/23 19:44:28 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.67 2024/01/29 21:30:24 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -238,15 +238,9 @@ debug_node(const tnode_t *tn) // NOLINT(
 		if (tn->tn_string->st_char)
 			debug_printf(", length %zu, \"%s\"\n",
 			    tn->tn_string->st_len,
-			    (const char *)tn->tn_string->st_mem);
-		else {
-			size_t n = MB_CUR_MAX * (tn->tn_string->st_len + 1);
-			char *s = xmalloc(n);
-			(void)wcstombs(s, tn->tn_string->st_mem, n);
-			debug_printf(", length %zu, L\"%s\"\n",
-			    tn->tn_string->st_len, s);
-			free(s);
-		}
+			    tn->tn_string->st_chars);
+		else
+			debug_printf(", length %zu\n", tn->tn_string->st_len);
 		break;
 	default:
 		debug_printf("\n");

Index: src/usr.bin/xlint/lint1/emit1.c
diff -u src/usr.bin/xlint/lint1/emit1.c:1.81 src/usr.bin/xlint/lint1/emit1.c:1.82
--- src/usr.bin/xlint/lint1/emit1.c:1.81	Sun Dec  3 18:17:41 2023
+++ src/usr.bin/xlint/lint1/emit1.c	Mon Jan 29 21:30:24 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: emit1.c,v 1.81 2023/12/03 18:17:41 rillig Exp $ */
+/* $NetBSD: emit1.c,v 1.82 2024/01/29 21:30:24 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: emit1.c,v 1.81 2023/12/03 18:17:41 rillig Exp $");
+__RCSID("$NetBSD: emit1.c,v 1.82 2024/01/29 21:30:24 rillig Exp $");
 #endif
 
 #include "lint1.h"
@@ -450,17 +450,13 @@ outqchar(char c)
 static void
 outfstrg(strg_t *strg)
 {
-	char c, oc;
-	bool first;
-	const char *cp;
 
 	lint_assert(strg->st_char);
-	cp = strg->st_mem;
+	const char *cp = strg->st_chars;
 
 	outchar('"');
 
-	c = *cp++;
-
+	char c = *cp++;
 	while (c != '\0') {
 
 		if (c != '%') {
@@ -511,7 +507,7 @@ outfstrg(strg_t *strg)
 		 */
 		if (c != '\0') {
 			outqchar(c);
-			oc = c;
+			char oc = c;
 			c = *cp++;
 			/*
 			 * handle [ for scanf. [-] means that a minus sign was
@@ -522,7 +518,7 @@ outfstrg(strg_t *strg)
 					c = *cp++;
 				if (c == ']')
 					c = *cp++;
-				first = true;
+				bool first = true;
 				while (c != '\0' && c != ']') {
 					if (c == '-') {
 						if (!first && *cp != ']')

Index: src/usr.bin/xlint/lint1/lex.c
diff -u src/usr.bin/xlint/lint1/lex.c:1.203 src/usr.bin/xlint/lint1/lex.c:1.204
--- src/usr.bin/xlint/lint1/lex.c:1.203	Sat Jan 27 20:03:14 2024
+++ src/usr.bin/xlint/lint1/lex.c	Mon Jan 29 21:30:25 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: lex.c,v 1.203 2024/01/27 20:03:14 rillig Exp $ */
+/* $NetBSD: lex.c,v 1.204 2024/01/29 21:30:25 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: lex.c,v 1.203 2024/01/27 20:03:14 rillig Exp $");
+__RCSID("$NetBSD: lex.c,v 1.204 2024/01/29 21:30:25 rillig Exp $");
 #endif
 
 #include <ctype.h>
@@ -1259,28 +1259,26 @@ clear_warn_flags(void)
 int
 lex_string(void)
 {
-	unsigned char *s;
-	int c;
-	size_t len, max;
-
-	s = xmalloc(max = 64);
+	size_t s_len = 0;
+	size_t s_cap = 64;
+	char *s = xmalloc(s_cap);
 
-	len = 0;
+	int c;
 	while ((c = get_escaped_char('"')) >= 0) {
 		/* +1 to reserve space for a trailing NUL character */
-		if (len + 1 == max)
-			s = xrealloc(s, max *= 2);
-		s[len++] = (char)c;
+		if (s_len + 1 == s_cap)
+			s = xrealloc(s, s_cap *= 2);
+		s[s_len++] = (char)c;
 	}
-	s[len] = '\0';
+	s[s_len] = '\0';
 	if (c == -2)
 		/* unterminated string constant */
 		error(258);
 
 	strg_t *strg = xcalloc(1, sizeof(*strg));
 	strg->st_char = true;
-	strg->st_len = len;
-	strg->st_mem = s;
+	strg->st_len = s_len;
+	strg->st_chars = s;
 
 	yylval.y_string = strg;
 	return T_STRING;
@@ -1317,7 +1315,7 @@ lex_wide_string(void)
 			n = 1;
 	}
 
-	wchar_t *ws = xmalloc((wlen + 1) * sizeof(*ws));
+	wchar_t *ws = xcalloc(wlen + 1, sizeof(*ws));
 	size_t wi = 0;
 	/* convert from multibyte to wide char */
 	(void)mbtowc(NULL, NULL, 0);
@@ -1327,13 +1325,12 @@ lex_wide_string(void)
 		if (n == 0)
 			n = 1;
 	}
-	ws[wi] = 0;
 	free(s);
+	free(ws);
 
 	strg_t *strg = xcalloc(1, sizeof(*strg));
 	strg->st_char = false;
 	strg->st_len = wlen;
-	strg->st_mem = ws;
 
 	yylval.y_string = strg;
 	return T_STRING;
@@ -1580,7 +1577,7 @@ freeyyv(void *sp, int tok)
 		free(val);
 	} else if (tok == T_STRING) {
 		strg_t *strg = *(strg_t **)sp;
-		free(strg->st_mem);
+		free(strg->st_chars);
 		free(strg);
 	}
 }

Index: src/usr.bin/xlint/lint1/lint1.h
diff -u src/usr.bin/xlint/lint1/lint1.h:1.209 src/usr.bin/xlint/lint1/lint1.h:1.210
--- src/usr.bin/xlint/lint1/lint1.h:1.209	Tue Jan 23 19:44:28 2024
+++ src/usr.bin/xlint/lint1/lint1.h	Mon Jan 29 21:30:25 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: lint1.h,v 1.209 2024/01/23 19:44:28 rillig Exp $ */
+/* $NetBSD: lint1.h,v 1.210 2024/01/29 21:30:25 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -81,7 +81,7 @@ typedef struct {
 typedef struct strg {
 	bool	st_char;	/* string doesn't have an 'L' prefix */
 	size_t	st_len;		/* length without trailing NUL */
-	void	*st_mem;	/* char[] for st_char, or wchar_t[] */
+	char	*st_chars;	/* only if st_char */
 } strg_t;
 
 // TODO: Use bit-fields instead of plain bool, but keep an eye on arm and

Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.598 src/usr.bin/xlint/lint1/tree.c:1.599
--- src/usr.bin/xlint/lint1/tree.c:1.598	Tue Jan 23 20:03:42 2024
+++ src/usr.bin/xlint/lint1/tree.c	Mon Jan 29 21:30:25 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: tree.c,v 1.598 2024/01/23 20:03:42 rillig Exp $	*/
+/*	$NetBSD: tree.c,v 1.599 2024/01/29 21:30:25 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: tree.c,v 1.598 2024/01/23 20:03:42 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.599 2024/01/29 21:30:25 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -542,9 +542,12 @@ build_string(strg_t *strg)
 
 	size_t chsize = strg->st_char ? sizeof(char) : sizeof(wchar_t);
 	size_t size = (len + 1) * chsize;
-	n->tn_string->st_mem = expr_zero_alloc(size, "tnode.string.data");
-	(void)memcpy(n->tn_string->st_mem, strg->st_mem, size);
-	free(strg->st_mem);
+	if (strg->st_char) {
+		n->tn_string->st_chars = expr_zero_alloc(size,
+		    "tnode.string.data");
+		(void)memcpy(n->tn_string->st_chars, strg->st_chars, size);
+		free(strg->st_chars);
+	}
 	free(strg);
 
 	return n;
@@ -4700,13 +4703,11 @@ cat_strings(strg_t *s1, strg_t *s2)
 
 	size_t len1 = s1->st_len;
 	size_t len2 = s2->st_len;
-	size_t chsize = s1->st_char ? sizeof(char) : sizeof(wchar_t);
-	size_t size1 = len1 * chsize;
-	size_t size2 = (len2 + 1) * chsize;
-	s1->st_mem = xrealloc(s1->st_mem, size1 + size2);
-	memcpy((char *)s1->st_mem + size1, s2->st_mem, size2);
-	free(s2->st_mem);
-
+	if (s1->st_char) {
+		s1->st_chars = xrealloc(s1->st_chars, len1 + len2 + 1);
+		memcpy(s1->st_chars + len1, s2->st_chars, len2 + 1);
+		free(s2->st_chars);
+	}
 	s1->st_len = len1 + len2;
 	free(s2);
 

Reply via email to