summaryrefslogtreecommitdiffabout
path: root/cache.c
authorLars Hjemli <hjemli@gmail.com>2006-12-12 09:16:41 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2006-12-12 09:16:41 (UTC)
commit58d04f6523b0029281d65f841859fa42d0c744ff (patch) (unidiff)
treeed52e95047ccbb99152f7d3f009e57687e6452f1 /cache.c
parentfbaf1171b4e343929dd43ecac7cd9d1c692b84ec (diff)
downloadcgit-58d04f6523b0029281d65f841859fa42d0c744ff.zip
cgit-58d04f6523b0029281d65f841859fa42d0c744ff.tar.gz
cgit-58d04f6523b0029281d65f841859fa42d0c744ff.tar.bz2
cache_lock: do xstrdup/free on lockfile
Since fmt() uses 8 alternating static buffers, and cache_lock might call cache_create_dirs() multiple times, which in turn might call fmt() twice, after four iterations lockfile would be overwritten by a cachedirectory path. In worst case, this could cause the cachedirectory to be unlinked and replaced by a cachefile. Fix: use xstrdup() on the result from fmt() before assigning to lockfile, and call free(lockfile) before exit. Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (limited to 'cache.c') (more/less context) (ignore whitespace changes)
-rw-r--r--cache.c3
1 files changed, 2 insertions, 1 deletions
diff --git a/cache.c b/cache.c
index b947a34..39e63a5 100644
--- a/cache.c
+++ b/cache.c
@@ -71,13 +71,13 @@ int cache_refill_overdue(const char *lockfile)
71 return (time(NULL) - st.st_mtime > cgit_cache_max_create_time); 71 return (time(NULL) - st.st_mtime > cgit_cache_max_create_time);
72} 72}
73 73
74int cache_lock(struct cacheitem *item) 74int cache_lock(struct cacheitem *item)
75{ 75{
76 int i = 0; 76 int i = 0;
77 char *lockfile = fmt("%s.lock", item->name); 77 char *lockfile = xstrdup(fmt("%s.lock", item->name));
78 78
79 top: 79 top:
80 if (++i > cgit_max_lock_attempts) 80 if (++i > cgit_max_lock_attempts)
81 die("cache_lock: unable to lock %s: %s", 81 die("cache_lock: unable to lock %s: %s",
82 item->name, strerror(errno)); 82 item->name, strerror(errno));
83 83
@@ -87,12 +87,13 @@ int cache_lock(struct cacheitem *item)
87 goto top; 87 goto top;
88 88
89 if (item->fd == NOLOCK && errno == EEXIST && 89 if (item->fd == NOLOCK && errno == EEXIST &&
90 cache_refill_overdue(lockfile) && !unlink(lockfile)) 90 cache_refill_overdue(lockfile) && !unlink(lockfile))
91 goto top; 91 goto top;
92 92
93 free(lockfile);
93 return (item->fd > 0); 94 return (item->fd > 0);
94} 95}
95 96
96int cache_unlock(struct cacheitem *item) 97int cache_unlock(struct cacheitem *item)
97{ 98{
98 close(item->fd); 99 close(item->fd);