threads / patch / 5744

patchdo not discard constness in interp_set_entry value argument

Subject: [PATCH] do not discard constness in interp_set_entry value argument

## tl;dr

One message between Sep 28, 2006 and Sep 28, 2006. Diffs are folded; open one to read it.

replies: 0people: 1as markdown or json

Alex Riesen· Sep 28, 2006, 19:12 UTC · lore
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.

Show changes to 2 files +3 −3

interpolate.c, interpolate.h

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,

← back to recent threads