summaryrefslogtreecommitdiffabout
authorStefan Gehn <stefan@srcbox.net>2011-03-26 08:51:39 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2011-03-26 10:44:16 (UTC)
commitf15c5833d2190bc62e0e1e3e9753ef33230ecd53 (patch) (unidiff)
treea81dd18bd692d7a5fb00c38910b871c5ffc29464
parentcc59ee502646dc4e3d0f8bbe29b24c7fa3f0d2dd (diff)
downloadcgit-f15c5833d2190bc62e0e1e3e9753ef33230ecd53.zip
cgit-f15c5833d2190bc62e0e1e3e9753ef33230ecd53.tar.gz
cgit-f15c5833d2190bc62e0e1e3e9753ef33230ecd53.tar.bz2
Fix crash when projectsfile cannot be opened
This patch makes cgit properly abort in case the projectsfile cannot be opened. Without the added return cgit continues using the projects pointer which is NULL and thus causes a segfault.
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--scan-tree.c1
1 files changed, 1 insertions, 0 deletions
diff --git a/scan-tree.c b/scan-tree.c
index 627af1b..e5a4baf 100644
--- a/scan-tree.c
+++ b/scan-tree.c
@@ -190,52 +190,53 @@ static void scan_path(const char *base, const char *path, repo_config_fn fn)
190 if (!buf) { 190 if (!buf) {
191 fprintf(stderr, "Alloc error on %s: %s (%d)\n", 191 fprintf(stderr, "Alloc error on %s: %s (%d)\n",
192 path, strerror(errno), errno); 192 path, strerror(errno), errno);
193 exit(1); 193 exit(1);
194 } 194 }
195 sprintf(buf, "%s/%s", path, ent->d_name); 195 sprintf(buf, "%s/%s", path, ent->d_name);
196 if (stat(buf, &st)) { 196 if (stat(buf, &st)) {
197 fprintf(stderr, "Error checking path %s: %s (%d)\n", 197 fprintf(stderr, "Error checking path %s: %s (%d)\n",
198 buf, strerror(errno), errno); 198 buf, strerror(errno), errno);
199 free(buf); 199 free(buf);
200 continue; 200 continue;
201 } 201 }
202 if (S_ISDIR(st.st_mode)) 202 if (S_ISDIR(st.st_mode))
203 scan_path(base, buf, fn); 203 scan_path(base, buf, fn);
204 free(buf); 204 free(buf);
205 } 205 }
206end: 206end:
207 closedir(dir); 207 closedir(dir);
208} 208}
209 209
210#define lastc(s) s[strlen(s) - 1] 210#define lastc(s) s[strlen(s) - 1]
211 211
212void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn) 212void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
213{ 213{
214 char line[MAX_PATH * 2], *z; 214 char line[MAX_PATH * 2], *z;
215 FILE *projects; 215 FILE *projects;
216 int err; 216 int err;
217 217
218 projects = fopen(projectsfile, "r"); 218 projects = fopen(projectsfile, "r");
219 if (!projects) { 219 if (!projects) {
220 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n", 220 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
221 projectsfile, strerror(errno), errno); 221 projectsfile, strerror(errno), errno);
222 return;
222 } 223 }
223 while (fgets(line, sizeof(line), projects) != NULL) { 224 while (fgets(line, sizeof(line), projects) != NULL) {
224 for (z = &lastc(line); 225 for (z = &lastc(line);
225 strlen(line) && strchr("\n\r", *z); 226 strlen(line) && strchr("\n\r", *z);
226 z = &lastc(line)) 227 z = &lastc(line))
227 *z = '\0'; 228 *z = '\0';
228 if (strlen(line)) 229 if (strlen(line))
229 scan_path(path, fmt("%s/%s", path, line), fn); 230 scan_path(path, fmt("%s/%s", path, line), fn);
230 } 231 }
231 if ((err = ferror(projects))) { 232 if ((err = ferror(projects))) {
232 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n", 233 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
233 projectsfile, strerror(err), err); 234 projectsfile, strerror(err), err);
234 } 235 }
235 fclose(projects); 236 fclose(projects);
236} 237}
237 238
238void scan_tree(const char *path, repo_config_fn fn) 239void scan_tree(const char *path, repo_config_fn fn)
239{ 240{
240 scan_path(path, path, fn); 241 scan_path(path, path, fn);
241} 242}