diff options
author | Sven Wegener <sven.wegener@stealer.net> | 2020-11-26 05:34:50 +0100 |
---|---|---|
committer | Sven Wegener <sven.wegener@stealer.net> | 2020-11-26 05:34:50 +0100 |
commit | 8225cc46adc2351d8f14f752cdc943cd07f0a2e0 (patch) | |
tree | c3a3e0481bc7a742507e592c32338e50ce9c2414 | |
parent | a8792abc4a978ce0ca5fe7ffebc62303ffb5b920 (diff) | |
download | distcc-git-8225cc46adc2351d8f14f752cdc943cd07f0a2e0.tar.gz |
serve: Correctly check asprintf() result and free memory
asprintf() returns -1 on failure, which evaluates to true, and the
resulting string is undefined in this case.
-rw-r--r-- | src/serve.c | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/src/serve.c b/src/serve.c index b7fdac6..1be57e0 100644 --- a/src/serve.c +++ b/src/serve.c @@ -403,8 +403,9 @@ static int dcc_check_compiler_whitelist(char *_compiler_name) if (faccessat(dirfd, compiler_name, X_OK, 0) < 0) { char *compiler_path = NULL; - if (asprintf(&compiler_path, "/usr/lib/distcc/%s", compiler_name) && compiler_path) { + if (asprintf(&compiler_path, "/usr/lib/distcc/%s", compiler_name) >= 0) { if (access(compiler_path, X_OK) < 0) { + free(compiler_path); close(dirfd); rs_log_crit("%s not in %s or %s whitelist.", compiler_name, LIBDIR "/distcc", "/usr/lib/distcc"); return EXIT_BAD_ARGUMENTS; /* ENOENT, EACCESS, etc */ @@ -419,27 +420,27 @@ static int dcc_check_compiler_whitelist(char *_compiler_name) return 0; #else // make do with access(): - char *compiler_path = NULL; + char *compiler_path; int ret = 0; - if (asprintf(&compiler_path, "%s/distcc/%s", LIBDIR, compiler_name) && compiler_path) { + if (asprintf(&compiler_path, "%s/distcc/%s", LIBDIR, compiler_name) >= 0) { if (access(compiler_path, X_OK) < 0) { free(compiler_path); /* check /usr/lib/distcc too */ - if (asprintf(&compiler_path, "/usr/lib/distcc/%s", compiler_name) && compiler_path) { + if (asprintf(&compiler_path, "/usr/lib/distcc/%s", compiler_name) >= 0) { if (access(compiler_path, X_OK) < 0) { rs_log_crit("%s not in %s or %s whitelist.", compiler_name, LIBDIR "/distcc", "/usr/lib/distcc"); ret = EXIT_BAD_ARGUMENTS; /* ENOENT, EACCESS, etc */ } + free(compiler_path); } - } + } else { + free(compiler_path); + } rs_trace("%s in" LIBDIR "/distcc whitelist", compiler_name); } else { rs_log_crit("Couldn't check if %s is in %s whitelist.", compiler_name, LIBDIR "/distcc"); ret = EXIT_DISTCC_FAILED; } - if (compiler_path) { - free(compiler_path); - } return ret; #endif } |