Module Name:    src
Committed By:   rillig
Date:           Sun Jul 26 19:44:04 UTC 2020

Modified Files:
        src/usr.bin/make: nonints.h var.c

Log Message:
make(1): mark result of Var_Parse as const

Var_Parse can return either a string literal or an allocated string.  The
former must not be modified by the caller.  If the string is allocated,
it is returned in freePtr as well, but only for freeing it after use.


To generate a diff of this commit:
cvs rdiff -u -r1.83 -r1.84 src/usr.bin/make/nonints.h
cvs rdiff -u -r1.328 -r1.329 src/usr.bin/make/var.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/make/nonints.h
diff -u src/usr.bin/make/nonints.h:1.83 src/usr.bin/make/nonints.h:1.84
--- src/usr.bin/make/nonints.h:1.83	Sun Jul 26 18:11:12 2020
+++ src/usr.bin/make/nonints.h	Sun Jul 26 19:44:04 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: nonints.h,v 1.83 2020/07/26 18:11:12 rillig Exp $	*/
+/*	$NetBSD: nonints.h,v 1.84 2020/07/26 19:44:04 rillig Exp $	*/
 
 /*-
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -191,7 +191,7 @@ void Var_Set(const char *, const char *,
 void Var_Append(const char *, const char *, GNode *);
 Boolean Var_Exists(const char *, GNode *);
 char *Var_Value(const char *, GNode *, char **);
-char *Var_Parse(const char *, GNode *, VarEvalFlags, int *, void **);
+const char *Var_Parse(const char *, GNode *, VarEvalFlags, int *, void **);
 char *Var_Subst(const char *, const char *, GNode *, VarEvalFlags);
 char *Var_GetTail(const char *);
 char *Var_GetHead(const char *);

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.328 src/usr.bin/make/var.c:1.329
--- src/usr.bin/make/var.c:1.328	Sun Jul 26 19:36:24 2020
+++ src/usr.bin/make/var.c	Sun Jul 26 19:44:04 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.328 2020/07/26 19:36:24 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.329 2020/07/26 19:44:04 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.328 2020/07/26 19:36:24 rillig Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.329 2020/07/26 19:44:04 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: var.c,v 1.328 2020/07/26 19:36:24 rillig Exp $");
+__RCSID("$NetBSD: var.c,v 1.329 2020/07/26 19:44:04 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -1838,7 +1838,6 @@ ParseModifierPart(const char **tstr, int
 		    Buf_AddByte(&buf, *cp);
 	    } else {
 		if (eflags & VARE_WANTRES) {
-		    char   *cp2;
 		    int     len;
 		    void   *freeIt;
 
@@ -1847,8 +1846,9 @@ ParseModifierPart(const char **tstr, int
 		     * delimiter, assume it's a variable
 		     * substitution and recurse.
 		     */
-		    cp2 = Var_Parse(cp, ctxt, errnum | (eflags & VARE_WANTRES),
-				    &len, &freeIt);
+		    VarEvalFlags sub_eflags = errnum | (eflags & VARE_WANTRES);
+		    const char *cp2 = Var_Parse(cp, ctxt, sub_eflags,
+						&len, &freeIt);
 		    Buf_AddStr(&buf, cp2);
 		    free(freeIt);
 		    cp += len - 1;
@@ -2145,11 +2145,11 @@ ApplyModifier_Defined(const char *mod, A
 	     * If unescaped dollar sign, assume it's a
 	     * variable substitution and recurse.
 	     */
-	    char    *cp2;
 	    int	    len;
 	    void    *freeIt;
 
-	    cp2 = Var_Parse(st->cp, st->ctxt, neflags, &len, &freeIt);
+	    const char *cp2 = Var_Parse(st->cp, st->ctxt, neflags,
+					&len, &freeIt);
 	    Buf_AddStr(&buf, cp2);
 	    free(freeIt);
 	    st->cp += len - 1;
@@ -3077,11 +3077,11 @@ ApplyModifiers(char *nstr, const char *t
 	     * We may have some complex modifiers in a variable.
 	     */
 	    void *freeIt;
-	    char *rval;
 	    int rlen;
 	    int c;
 
-	    rval = Var_Parse(p, st.ctxt, st.eflags, &rlen, &freeIt);
+	    const char *rval = Var_Parse(p, st.ctxt, st.eflags,
+					 &rlen, &freeIt);
 
 	    /*
 	     * If we have not parsed up to st.endc or ':',
@@ -3383,7 +3383,7 @@ cleanup:
  *-----------------------------------------------------------------------
  */
 /* coverity[+alloc : arg-*4] */
-char *
+const char *
 Var_Parse(const char * const str, GNode *ctxt, VarEvalFlags flags,
 	  int *lengthPtr, void **freePtr)
 {
@@ -3436,13 +3436,13 @@ Var_Parse(const char * const str, GNode 
 		 */
 		switch (str[1]) {
 		case '@':
-		    return UNCONST("$(.TARGET)");
+		    return "$(.TARGET)";
 		case '%':
-		    return UNCONST("$(.MEMBER)");
+		    return "$(.MEMBER)";
 		case '*':
-		    return UNCONST("$(.PREFIX)");
+		    return "$(.PREFIX)";
 		case '!':
-		    return UNCONST("$(.ARCHIVE)");
+		    return "$(.ARCHIVE)";
 		}
 	    }
 	    return (flags & VARE_UNDEFERR) ? var_Error : varNoError;
@@ -3475,7 +3475,8 @@ Var_Parse(const char * const str, GNode 
 	    if (*tstr == '$') {
 		int rlen;
 		void *freeIt;
-		char *rval = Var_Parse(tstr, ctxt, flags, &rlen, &freeIt);
+		const char *rval = Var_Parse(tstr, ctxt, flags,
+					     &rlen, &freeIt);
 		if (rval != NULL)
 		    Buf_AddStr(&buf, rval);
 		free(freeIt);
@@ -3713,7 +3714,7 @@ char *
 Var_Subst(const char *var, const char *str, GNode *ctxt, VarEvalFlags flags)
 {
     Buffer	buf;		/* Buffer for forming things */
-    char	*val;		/* Value to substitute for a variable */
+    const char	*val;		/* Value to substitute for a variable */
     int		length;		/* Length of the variable invocation */
     Boolean	trailingBslash;	/* variable ends in \ */
     void	*freeIt = NULL;	/* Set if it should be freed */

Reply via email to