[PATCH] system_path: use a static buffer
- From
Carlos Martín Nieto <cmn@elego.de>
- Date
- Mar 16, 2011, 16:33 UTC
- Message-ID
- <1300293183-26354-1-git-send-email-cmn@elego.de>
- In-Reply-To
- <AANLkTinEMzezKdfGUrwKv7sJ+tSK5duYM6XZMvBa-yj3@mail.gmail.com>
Make system_path behave like the other path functions by using a static buffer, fixing a memory leak.
Also make sure the prefix pointer is always initialized to either PREFIX or NULL.
Signed-off-by: Carlos Martín Nieto <cmn@elego.de> --- exec_cmd.c | 11 ++++++----- 1 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/exec_cmd.c b/exec_cmd.c index 38545e8..5686952 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -9,11 +9,11 @@ static const char *argv0_path; const char *system_path(const char *path) { #ifdef RUNTIME_PREFIX - static const char *prefix; + static const char *prefix = NULL; #else static const char *prefix = PREFIX; #endif - struct strbuf d = STRBUF_INIT; + static char buf[PATH_MAX]; if (is_absolute_path(path)) return path; @@ -33,9 +33,10 @@ const char *system_path(const char *path) } #endif - strbuf_addf(&d, "%s/%s", prefix, path); - path = strbuf_detach(&d, NULL); - return path; + if (snprintf(buf, sizeof(buf), "%s/%s", prefix, path) >= sizeof(buf)) + die("system path too long for %s", path); + + return buf; } const char *git_extract_argv0_path(const char *argv0)
-- 1.7.4.1