From 324b0991d2c4544635483bc9386e2e6c58d86859 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Mon, 25 Sep 2017 20:17:33 +0200 Subject: Fix minor coverity issues --- src/sp_utils.c | 17 ++++++++++++----- src/tests/dump_request_invalid_folder.phpt | 1 + 2 files changed, 13 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/sp_utils.c b/src/sp_utils.c index 2370a6d..f696a55 100644 --- a/src/sp_utils.c +++ b/src/sp_utils.c @@ -52,14 +52,17 @@ int compute_hash(const char* const filename, char* file_hash) { return SUCCESS; } -static void construct_filename(char* filename, const char* folder) { +static int construct_filename(char* filename, const char* folder) { time_t t = time(NULL); struct tm* tm = localtime(&t); // FIXME use `localtime_r` instead struct timeval tval; struct stat st = {0}; if (-1 == stat(folder, &st)) { - mkdir(folder, 0700); + if (0 != mkdir(folder, 0700)) { + sp_log_err("request_logging", "Unable to create the folder '%s'.", + folder); + } } memcpy(filename, folder, strlen(folder)); @@ -70,12 +73,14 @@ static void construct_filename(char* filename, const char* folder) { strcat(filename, "_"); char* remote_addr = getenv("REMOTE_ADDR"); - if (remote_addr) { - strcat(filename, remote_addr); + if (remote_addr) { // ipv6: 8*4 bytes + 7 colons = 39 chars max + strncat(filename, remote_addr, 40); } else { strcat(filename, "0.0.0.0"); } strcat(filename, ".dump"); + + return 0; } int sp_log_request(const char* folder) { @@ -90,7 +95,9 @@ int sp_log_request(const char* folder) { {"COOKIE", TRACK_VARS_COOKIE}, {"SERVER", TRACK_VARS_SERVER}, {"ENV", TRACK_VARS_ENV}, {NULL, 0}}; - construct_filename(filename, folder); + if (0 != construct_filename(filename, folder)) { + return -1; + } if (NULL == (file = fopen(filename, "a"))) { sp_log_err("request_logging", "Unable to open %s", filename); return -1; diff --git a/src/tests/dump_request_invalid_folder.phpt b/src/tests/dump_request_invalid_folder.phpt index b866f70..e7ed45d 100644 --- a/src/tests/dump_request_invalid_folder.phpt +++ b/src/tests/dump_request_invalid_folder.phpt @@ -21,5 +21,6 @@ echo "2\n"; --EXPECTF-- 1 [snuffleupagus][0.0.0.0][disabled_function][drop] The call to the function 'system' in %atests/dump_request_invalid_folder.php:3 has been disabled. +[snuffleupagus][0.0.0.0][request_logging][error] Unable to create the folder '/root/NON_EXISTENT/FOLDER/PLEASE/'. [snuffleupagus][0.0.0.0][request_logging][error] Unable to open /root/NON_EXISTENT/FOLDER/PLEASE/sp_dump_%a_0.0.0.0.dump 2 \ No newline at end of file -- cgit v1.3