diff options
author | Carlos Martín Nieto <cmn@elego.de> | 2011-03-30 11:07:09 +0200 |
---|---|---|
committer | Carlos Martín Nieto <cmn@elego.de> | 2011-03-30 11:34:04 +0200 |
commit | 2e445cacd23dd494e182024cf1b229a3727339aa (patch) | |
tree | 42d08d37a7990b1f939f4b0ed9baedfb2d877746 | |
parent | 9a3c5e55fd7894b8732a8010c94d034d674adbf7 (diff) | |
download | libgit2-2e445cacd23dd494e182024cf1b229a3727339aa.tar.gz |
build_varname: allocate memory
Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
-rw-r--r-- | src/config.c | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/src/config.c b/src/config.c index f9dbda7e9..6679207c0 100644 --- a/src/config.c +++ b/src/config.c @@ -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; } |