Module Name:    src
Committed By:   rillig
Date:           Mon Jul 10 13:55:55 UTC 2023

Modified Files:
        src/usr.bin/xlint/common: emit.c
        src/usr.bin/xlint/lint2: main2.c msg.c

Log Message:
lint: replce sprintf with snprintf

Even though the sprintf calls were safe, they looked suspicious.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.20 -r1.21 src/usr.bin/xlint/common/emit.c
cvs rdiff -u -r1.32 -r1.33 src/usr.bin/xlint/lint2/main2.c
cvs rdiff -u -r1.21 -r1.22 src/usr.bin/xlint/lint2/msg.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/common/emit.c
diff -u src/usr.bin/xlint/common/emit.c:1.20 src/usr.bin/xlint/common/emit.c:1.21
--- src/usr.bin/xlint/common/emit.c:1.20	Sun Jul  9 12:15:07 2023
+++ src/usr.bin/xlint/common/emit.c	Mon Jul 10 13:55:55 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: emit.c,v 1.20 2023/07/09 12:15:07 rillig Exp $	*/
+/*	$NetBSD: emit.c,v 1.21 2023/07/10 13:55:55 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: emit.c,v 1.20 2023/07/09 12:15:07 rillig Exp $");
+__RCSID("$NetBSD: emit.c,v 1.21 2023/07/10 13:55:55 rillig Exp $");
 #endif
 
 #include <stdio.h>
@@ -153,7 +153,7 @@ outint(int i)
 
 	if ((size_t)(ob.o_end - ob.o_next) < 3 * sizeof(int))
 		outxbuf();
-	ob.o_next += sprintf(ob.o_next, "%d", i);
+	ob.o_next += snprintf(ob.o_next, ob.o_end - ob.o_next, "%d", i);
 }
 
 /* write a name to the output buffer, preceded by its length */

Index: src/usr.bin/xlint/lint2/main2.c
diff -u src/usr.bin/xlint/lint2/main2.c:1.32 src/usr.bin/xlint/lint2/main2.c:1.33
--- src/usr.bin/xlint/lint2/main2.c:1.32	Mon Jul 10 12:40:22 2023
+++ src/usr.bin/xlint/lint2/main2.c	Mon Jul 10 13:55:55 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: main2.c,v 1.32 2023/07/10 12:40:22 rillig Exp $	*/
+/*	$NetBSD: main2.c,v 1.33 2023/07/10 13:55:55 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: main2.c,v 1.32 2023/07/10 12:40:22 rillig Exp $");
+__RCSID("$NetBSD: main2.c,v 1.33 2023/07/10 13:55:55 rillig Exp $");
 #endif
 
 #include <stdio.h>
@@ -99,7 +99,7 @@ main(int argc, char *argv[])
 		case 'C':
 			len = strlen(optarg);
 			lname = xmalloc(len + 10);
-			(void)sprintf(lname, "llib-l%s.ln", optarg);
+			(void)snprintf(lname, len + 10, "llib-l%s.ln", optarg);
 			libname = lname;
 			Cflag = true;
 			uflag = true;

Index: src/usr.bin/xlint/lint2/msg.c
diff -u src/usr.bin/xlint/lint2/msg.c:1.21 src/usr.bin/xlint/lint2/msg.c:1.22
--- src/usr.bin/xlint/lint2/msg.c:1.21	Mon Jul 10 12:40:22 2023
+++ src/usr.bin/xlint/lint2/msg.c	Mon Jul 10 13:55:55 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: msg.c,v 1.21 2023/07/10 12:40:22 rillig Exp $	*/
+/*	$NetBSD: msg.c,v 1.22 2023/07/10 13:55:55 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: msg.c,v 1.21 2023/07/10 12:40:22 rillig Exp $");
+__RCSID("$NetBSD: msg.c,v 1.22 2023/07/10 13:55:55 rillig Exp $");
 #endif
 
 #include <stdarg.h>
@@ -104,31 +104,30 @@ lbasename(const char *path)
 const char *
 mkpos(const pos_t *posp)
 {
-	size_t len;
-	const char *fn;
 	static char *buf;
-	static size_t blen = 0;
-	bool qm;
-	int src, line;
+	static size_t buf_size;
 
+	int filename;
+	int lineno;
 	if (Hflag && posp->p_src != posp->p_isrc) {
-		src = posp->p_isrc;
-		line = posp->p_iline;
+		filename = posp->p_isrc;
+		lineno = posp->p_iline;
 	} else {
-		src = posp->p_src;
-		line = posp->p_line;
+		filename = posp->p_src;
+		lineno = posp->p_line;
 	}
-	qm = !Hflag && posp->p_src != posp->p_isrc;
 
-	len = strlen(fn = lbasename(fnames[src]));
-	len += 3 * sizeof(unsigned short) + 4;
-
-	if (len > blen)
-		buf = xrealloc(buf, blen = len);
-	if (line != 0)
-		(void)sprintf(buf, "%s%s(%d)", fn, qm ? "?" : "", line);
+	bool qm = !Hflag && posp->p_src != posp->p_isrc;
+	const char *fn = lbasename(fnames[filename]);
+	size_t len = strlen(fn) + 1 + 1 + 3 * sizeof(int) + 1 + 1;
+
+	if (len > buf_size)
+		buf = xrealloc(buf, buf_size = len);
+	if (lineno != 0)
+		(void)snprintf(buf, buf_size, "%s%s(%d)",
+		    fn, qm ? "?" : "", lineno);
 	else
-		(void)sprintf(buf, "%s", fn);
+		(void)snprintf(buf, buf_size, "%s", fn);
 
 	return buf;
 }

Reply via email to