Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/xlint lint: replce sprintf with snprintf



details:   https://anonhg.NetBSD.org/src/rev/7d4f81960e0e
branches:  trunk
changeset: 377394:7d4f81960e0e
user:      rillig <rillig%NetBSD.org@localhost>
date:      Mon Jul 10 13:55:55 2023 +0000

description:
lint: replce sprintf with snprintf

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

No functional change.

diffstat:

 usr.bin/xlint/common/emit.c |   6 +++---
 usr.bin/xlint/lint2/main2.c |   6 +++---
 usr.bin/xlint/lint2/msg.c   |  39 +++++++++++++++++++--------------------
 3 files changed, 25 insertions(+), 26 deletions(-)

diffs (121 lines):

diff -r d6e2b3f8e338 -r 7d4f81960e0e usr.bin/xlint/common/emit.c
--- a/usr.bin/xlint/common/emit.c       Mon Jul 10 13:05:35 2023 +0000
+++ b/usr.bin/xlint/common/emit.c       Mon Jul 10 13:55:55 2023 +0000
@@ -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 */
diff -r d6e2b3f8e338 -r 7d4f81960e0e usr.bin/xlint/lint2/main2.c
--- a/usr.bin/xlint/lint2/main2.c       Mon Jul 10 13:05:35 2023 +0000
+++ b/usr.bin/xlint/lint2/main2.c       Mon Jul 10 13:55:55 2023 +0000
@@ -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;
diff -r d6e2b3f8e338 -r 7d4f81960e0e usr.bin/xlint/lint2/msg.c
--- a/usr.bin/xlint/lint2/msg.c Mon Jul 10 13:05:35 2023 +0000
+++ b/usr.bin/xlint/lint2/msg.c Mon Jul 10 13:55:55 2023 +0000
@@ -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;
+
+       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;
 
-       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);
+       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;
 }



Home | Main Index | Thread Index | Old Index