[PATCH v4 27/32] commit_lock_file(): use a strbuf to manage temporary space
- From
Michael Haggerty <mhagger@alum.mit.edu>
- Date
- Sep 6, 2014, 07:50 UTC
- Message-ID
- <1409989846-22401-28-git-send-email-mhagger@alum.mit.edu>
- In-Reply-To
- <1409989846-22401-1-git-send-email-mhagger@alum.mit.edu>
Avoid relying on the filename length restrictions that are currently checked by lock_file().
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> --- lockfile.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/lockfile.c b/lockfile.c index f51f73f..f6b3866 100644 --- a/lockfile.c +++ b/lockfile.c @@ -318,8 +318,9 @@ int reopen_lock_file(struct lock_file *lk) int commit_lock_file(struct lock_file *lk) { - char result_file[PATH_MAX]; + static struct strbuf result_file = STRBUF_INIT; int save_errno; + int err; if (!lk->active) die("BUG: attempt to commit unlocked object"); @@ -327,13 +328,13 @@ int commit_lock_file(struct lock_file *lk) if (lk->fd >= 0 && close_lock_file(lk)) goto rollback; - strcpy(result_file, lk->filename); /* remove ".lock": */ - result_file[strlen(result_file) - LOCK_SUFFIX_LEN] = 0; - - if (rename(lk->filename, result_file)) + strbuf_add(&result_file, lk->filename, + strlen(lk->filename) - LOCK_SUFFIX_LEN); + err = rename(lk->filename, result_file.buf); + strbuf_reset(&result_file); + if (err) goto rollback; - lk->active = 0; lk->filename[0] = 0; return 0;
-- 2.1.0