Skip to contentSkip to conversation

src: don't write to possible rodata location

PATCH New Florian Westphal 1 participant 1 message May 4 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 src: don't write to possible rodata location

seen with gcc-16.0.1:
src/libnftables.c: In function 'nft_ctx_add_var':
src/libnftables.c:153:27: warning: initialization discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
153 | char *separator = strchr(var, '=');

function arg says "const char *", write to this memory location is not expected.

---
 src/libnftables.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
src/libnftables.c +9 -3
index bc42c32de889..db9ee388adde 100644
@@ -150,21 +150,27 @@ static void nft_exit(struct nft_ctx *ctx)
 EXPORT_SYMBOL(nft_ctx_add_var);
 int nft_ctx_add_var(struct nft_ctx *ctx, const char *var)
 {
-	char *separator = strchr(var, '=');
+	const char *separator = strchr(var, '=');
 	int pcount = ctx->num_vars;
 	struct nft_vars *tmp;
 	const char *value;
+	size_t len;
+	char *key;
 
 	if (!separator)
 		return -1;
 
 	tmp = xrealloc(ctx->vars, (pcount + 1) * sizeof(struct nft_vars));
 
-	*separator = '\0';
 	value = separator + 1;
+	len = separator - var;
+
+	key = xmalloc(len + 1);
+	memcpy(key, var, len);
+	key[len] = '\0';
 
 	ctx->vars = tmp;
-	ctx->vars[pcount].key = xstrdup(var);
+	ctx->vars[pcount].key = key;
 	ctx->vars[pcount].value = xstrdup(value);
 	ctx->num_vars++;
 
Signature · 1 line
2.53.0