git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] do not discard constness in interp_set_entry value argument

From
Alex Riesen <fork0@t-online.de>
Date
Sep 28, 2006, 19:12 UTC
Message-ID
<20060928191255.GB4294@steel.home>
Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---

Actually, both of the struct interp's fields could be made const, if not this interp_set_entry. And even then it could just return the old value of ->value casting away its constness. I.e.:

struct interp
{
    const char *name;
    const char *value;
};
static inline char *interp_set_entry(struct interp *table, int slot, const char *value)
{
	char *oldval = (char *)table[slot].value;
	table[slot].value = newval;
	return oldval;
}

The caller can than decide if it should be freed. This leaves compiler with a chance to optimize something.

diff --git a/interpolate.c b/interpolate.c
index 62701d8..5d9d188 100644
--- a/interpolate.c
+++ b/interpolate.c
@@ -8,10 +8,10 @@ #include "git-compat-util.h"
 #include "interpolate.h"
 
 
-void interp_set_entry(struct interp *table, int slot, char *value)
+void interp_set_entry(struct interp *table, int slot, const char *value)
 {
 	char *oldval = table[slot].value;
-	char *newval = value;
+	char *newval = NULL;
 
 	if (oldval)
 		free(oldval);
diff --git a/interpolate.h b/interpolate.h
index a55fb8e..190a180 100644
--- a/interpolate.h
+++ b/interpolate.h
@@ -16,7 +16,7 @@ struct interp {
 	char *value;
 };
 
-extern void interp_set_entry(struct interp *table, int slot, char *value);
+extern void interp_set_entry(struct interp *table, int slot, const char *value);
 extern void interp_clear_table(struct interp *table, int ninterps);
 
 extern int interpolate(char *result, int reslen,
Message 1 of 1 in “do not discard constness in interp_set_entry value argument”
  1. do not discard constness in interp_set_entry value argumentAlex Riesen, Sep 28, 2006

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.