Commit 2e445cac by Carlos Martín Nieto

build_varname: allocate memory

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
parent 9a3c5e55
......@@ -596,16 +596,24 @@ static int config_parse(git_config *cfg_file)
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? */
if(strlen(section) + len + 2 > sizeof(varname))
char *varname;
int section_len, ret;
size_t total_len;
section_len = strlen(section);
total_len = section_len + name_len + 2;
varname = malloc(total_len);
if(varname == NULL)
return NULL;
strcpy(varname, section);
strcat(varname, ".");
strncat(varname, name, len);
ret = snprintf(varname, total_len, "%s.%s", section, name);
return varname;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment