From: Sebastian Harl Date: Mon, 30 Jun 2014 06:13:53 +0000 (+0200) Subject: Don't free dirname()'s return value. X-Git-Tag: sysdb-0.2.0~13 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=fda0b2679c4d50d2077a593fdc2b73b521524521;p=sysdb.git Don't free dirname()'s return value. It may point to static memory. --- diff --git a/src/frontend/sock.c b/src/frontend/sock.c index 845274e..dd0afa9 100644 --- a/src/frontend/sock.c +++ b/src/frontend/sock.c @@ -97,6 +97,7 @@ static int open_unix_sock(listener_t *listener) { const char *addr; + char *addr_copy; char *base_dir; struct sockaddr_un sa; int status; @@ -118,24 +119,24 @@ open_unix_sock(listener_t *listener) sa.sun_family = AF_UNIX; strncpy(sa.sun_path, addr, sizeof(sa.sun_path)); - base_dir = strdup(addr); - if (! base_dir) { + addr_copy = strdup(addr); + if (! addr_copy) { char errbuf[1024]; sdb_log(SDB_LOG_ERR, "frontend: strdup failed: %s", sdb_strerror(errno, errbuf, sizeof(errbuf))); return -1; } - base_dir = dirname(base_dir); + base_dir = dirname(addr_copy); /* ensure that the directory exists */ if (sdb_mkdir_all(base_dir, 0777)) { char errbuf[1024]; sdb_log(SDB_LOG_ERR, "frontend: Failed to create directory '%s': %s", base_dir, sdb_strerror(errno, errbuf, sizeof(errbuf))); - free(base_dir); + free(addr_copy); return -1; } - free(base_dir); + free(addr_copy); if (unlink(addr) && (errno != ENOENT)) { char errbuf[1024]; diff --git a/src/utils/os.c b/src/utils/os.c index ecca206..08cb188 100644 --- a/src/utils/os.c +++ b/src/utils/os.c @@ -50,6 +50,7 @@ int sdb_mkdir_all(const char *pathname, mode_t mode) { struct stat st; + char *pathname_copy; char *base_dir; int status = 0; @@ -72,16 +73,16 @@ sdb_mkdir_all(const char *pathname, mode_t mode) /* pathname exists but we cannot access it */ return -1; - base_dir = strdup(pathname); - if (! base_dir) + pathname_copy = strdup(pathname); + if (! pathname_copy) return -1; - base_dir = dirname(base_dir); + base_dir = dirname(pathname_copy); status = sdb_mkdir_all(base_dir, mode); if (! status) status = mkdir(pathname, mode); - free(base_dir); + free(pathname_copy); return status; } /* sdb_mkdir_all */