From ef4bd8d6c0820ba96c37aae5857accad87ed9a90 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 7 Feb 2024 14:28:38 +0100 Subject: [PATCH] curl: when allocating variables, add the name into the struct This saves the name from being an extra separate allocation. Closes #12891 --- src/var.c | 34 ++++++++++++++-------------------- src/var.h | 2 +- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/var.c b/src/var.c index 74410ad4e0..4dad09989d 100644 --- a/src/var.c +++ b/src/var.c @@ -63,7 +63,6 @@ void varcleanup(struct GlobalConfig *global) struct var *t = list; list = list->next; free((char *)t->content); - free((char *)t->name); free(t); } } @@ -343,7 +342,7 @@ ParameterError varexpand(struct GlobalConfig *global, } /* - * Created in a way that is not revealing how variables is actually stored so + * Created in a way that is not revealing how variables are actually stored so * that we can improve this if we want better performance when managing many * at a later point. */ @@ -356,29 +355,24 @@ static ParameterError addvariable(struct GlobalConfig *global, { struct var *p; const struct var *check = varcontent(global, name, nlen); + DEBUGASSERT(nlen); if(check) notef(global, "Overwriting variable '%s'", check->name); - p = calloc(1, sizeof(struct var)); - if(!p) - return PARAM_NO_MEM; - - p->name = Memdup(name, nlen); - if(!p->name) - goto err; + p = calloc(1, sizeof(struct var) + nlen); + if(p) { + memcpy(p->name, name, nlen); - p->content = contalloc ? content: Memdup(content, clen); - if(!p->content) - goto err; - p->clen = clen; + p->content = contalloc ? content: Memdup(content, clen); + if(p->content) { + p->clen = clen; - p->next = global->variables; - global->variables = p; - return PARAM_OK; -err: - free((char *)p->content); - free((char *)p->name); - free(p); + p->next = global->variables; + global->variables = p; + return PARAM_OK; + } + free(p); + } return PARAM_NO_MEM; } diff --git a/src/var.h b/src/var.h index 4a71943a3c..2ea9797275 100644 --- a/src/var.h +++ b/src/var.h @@ -29,9 +29,9 @@ struct var { struct var *next; - const char *name; const char *content; size_t clen; /* content length */ + char name[1]; /* allocated as part of the struct */ }; struct GlobalConfig; -- 2.47.3