curl: when allocating variables, add the name into the struct

This saves the name from being an extra separate allocation.

Closes #12891
This commit is contained in:
Daniel Stenberg 2024-02-07 14:28:38 +01:00
parent d1c9f38b8c
commit ef4bd8d6c0
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
2 changed files with 15 additions and 21 deletions

View File

@ -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 = calloc(1, sizeof(struct var) + nlen);
if(p) {
memcpy(p->name, name, nlen);
p->name = Memdup(name, nlen);
if(!p->name)
goto err;
p->content = contalloc ? content: Memdup(content, clen);
if(p->content) {
p->clen = clen;
p->content = contalloc ? content: Memdup(content, clen);
if(!p->content)
goto err;
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;
}

View File

@ -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;