From bd2b8b26aa6052b156b256d55b24d7f7c3d57796 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Mon, 7 Dec 2020 11:03:54 +0100 Subject: add some safety around stat failures --- src/common.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src/common.c') diff --git a/src/common.c b/src/common.c index 352a706..c5d9435 100644 --- a/src/common.c +++ b/src/common.c @@ -25,8 +25,11 @@ void print_help(const struct Command* commands, const size_t size) struct stat getStat(const char* path) { - struct stat sb; + // fill with 0s by default in the case stat fails + struct stat sb = {0}; + // the return value signifies if stat failes (e.g. file not found) + // unimportant for us if it fails it won't touch sb stat(path, &sb); return sb; @@ -48,9 +51,10 @@ bool isDir(const char* path) void makeDir(const char* path) { - struct stat st = {0}; + // we do not care about the contents but what stat returns + struct stat sb; - if (stat(path, &st) == -1) + if (stat(path, &sb) < 0) { mkdir(path, 0755); } -- cgit v1.2.3