Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/xlint/lint1 lint: sprinkle const in memory management



details:   https://anonhg.NetBSD.org/src/rev/eb497e5fdf49
branches:  trunk
changeset: 379811:eb497e5fdf49
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Jun 20 18:51:50 2021 +0000

description:
lint: sprinkle const in memory management

No functional change.

diffstat:

 usr.bin/xlint/lint1/mem1.c |  26 +++++++++++++++-----------
 1 files changed, 15 insertions(+), 11 deletions(-)

diffs (72 lines):

diff -r 151e06a3c1dc -r eb497e5fdf49 usr.bin/xlint/lint1/mem1.c
--- a/usr.bin/xlint/lint1/mem1.c        Sun Jun 20 18:44:48 2021 +0000
+++ b/usr.bin/xlint/lint1/mem1.c        Sun Jun 20 18:51:50 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: mem1.c,v 1.43 2021/04/02 12:16:50 rillig Exp $ */
+/*     $NetBSD: mem1.c,v 1.44 2021/06/20 18:51:50 rillig Exp $ */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: mem1.c,v 1.43 2021/04/02 12:16:50 rillig Exp $");
+__RCSID("$NetBSD: mem1.c,v 1.44 2021/06/20 18:51:50 rillig Exp $");
 #endif
 
 #include <sys/types.h>
@@ -49,10 +49,11 @@
 #include "lint1.h"
 
 /*
- * Filenames allocated by record_filename are shared.
+ * Filenames allocated by record_filename are shared and have unlimited
+ * lifetime.
  */
 struct filename {
-       char    *fn_name;
+       const char *fn_name;
        size_t  fn_len;
        int     fn_id;
        struct  filename *fn_next;
@@ -74,10 +75,10 @@ search_filename(const char *s, size_t le
 }
 
 struct filename_replacement {
-       char *orig;
+       const char *orig;
        size_t orig_len;
-       char *repl;
-       struct filename_replacement *next;
+       const char *repl;
+       const struct filename_replacement *next;
 };
 
 static struct filename_replacement *filename_replacements;
@@ -132,6 +133,7 @@ record_filename(const char *s, size_t sl
 {
        const struct filename *existing_fn;
        struct filename *fn;
+       char *name;
 
        if (s == NULL)
                return NULL;
@@ -139,11 +141,13 @@ record_filename(const char *s, size_t sl
        if ((existing_fn = search_filename(s, slen)) != NULL)
                return existing_fn->fn_name;
 
+       /* Do not use strdup() because s is not NUL-terminated.*/
+       name = xmalloc(slen + 1);
+       (void)memcpy(name, s, slen);
+       name[slen] = '\0';
+
        fn = xmalloc(sizeof(*fn));
-       /* Do not use strdup() because s is not NUL-terminated.*/
-       fn->fn_name = xmalloc(slen + 1);
-       (void)memcpy(fn->fn_name, s, slen);
-       fn->fn_name[slen] = '\0';
+       fn->fn_name = name;
        fn->fn_len = slen;
        fn->fn_id = next_filename_id();
        fn->fn_next = filenames;



Home | Main Index | Thread Index | Old Index