From 8c8fce82e5e357c407533d8f74f469271cf61d4d Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Fri, 21 Sep 2012 16:47:43 +0200 Subject: [PATCH] add proper error handling for in-place restarts (Thanks Markus) fixes #806 --- src/ipc.c | 2 +- src/load_layout.c | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/ipc.c b/src/ipc.c index 7dfbc871..169c659f 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -52,7 +52,7 @@ static bool mkdirp(const char *path) { ELOG("mkdir(%s) failed: %s\n", path, strerror(errno)); return false; } - char *copy = strdup(path); + char *copy = sstrdup(path); /* strip trailing slashes, if any */ while (copy[strlen(copy)-1] == '/') copy[strlen(copy)-1] = '\0'; diff --git a/src/load_layout.c b/src/load_layout.c index cce1a712..fc513f51 100644 --- a/src/load_layout.c +++ b/src/load_layout.c @@ -350,11 +350,22 @@ void tree_append_json(const char *filename) { /* TODO: percent of other windows are not correctly fixed at the moment */ FILE *f; if ((f = fopen(filename, "r")) == NULL) { - LOG("Cannot open file\n"); + LOG("Cannot open file \"%s\"\n", filename); + return; + } + struct stat stbuf; + if (fstat(fileno(f), &stbuf) != 0) { + LOG("Cannot fstat() the file\n"); + fclose(f); + return; + } + char *buf = smalloc(stbuf.st_size); + int n = fread(buf, 1, stbuf.st_size, f); + if (n != stbuf.st_size) { + LOG("File \"%s\" could not be read entirely, not loading.\n", filename); + fclose(f); return; } - char *buf = malloc(65535); /* TODO */ - int n = fread(buf, 1, 65535, f); LOG("read %d bytes\n", n); yajl_gen g; yajl_handle hand; -- 2.39.5