mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-11 18:17:54 +00:00
build_varname: allocate memory
Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
This commit is contained in:
parent
9a3c5e55fd
commit
2e445cacd2
20
src/config.c
20
src/config.c
@ -596,16 +596,24 @@ static int config_parse(git_config *cfg_file)
|
|||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *build_varname(const char *section, const char *name, int len)
|
/*
|
||||||
|
* Gives $section.$name back, using only name_len chars from the name,
|
||||||
|
* which is useful so we don't have to copy the variable name twice.
|
||||||
|
* Don't forget to free the memory you get.
|
||||||
|
*/
|
||||||
|
static char *build_varname(const char *section, const char *name, int name_len)
|
||||||
{
|
{
|
||||||
static char varname[1024]; /* TODO: What's the longest we should allow? */
|
char *varname;
|
||||||
|
int section_len, ret;
|
||||||
|
size_t total_len;
|
||||||
|
|
||||||
if(strlen(section) + len + 2 > sizeof(varname))
|
section_len = strlen(section);
|
||||||
|
total_len = section_len + name_len + 2;
|
||||||
|
varname = malloc(total_len);
|
||||||
|
if(varname == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
strcpy(varname, section);
|
ret = snprintf(varname, total_len, "%s.%s", section, name);
|
||||||
strcat(varname, ".");
|
|
||||||
strncat(varname, name, len);
|
|
||||||
|
|
||||||
return varname;
|
return varname;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user