Skip to content

Commit d8fefbf

Browse files
committed
fix error message in logger.
If path to log file was too long, we was printing uninitialized variable. This patch fixes this issue.
1 parent d21ec95 commit d8fefbf

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

src/utils/utils_log.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,23 @@ void util_log_init(void) {
156156
len = strlen(arg);
157157
}
158158

159-
if (len <= MAX_FILE_PATH) {
160-
memcpy(file, arg, len);
161-
file[len] = '\0';
162-
loggerConfig.output = fopen(file, "w");
159+
if (len > MAX_FILE_PATH) {
160+
loggerConfig.output = stderr;
161+
LOG_ERR("Cannot open output file - path too long");
162+
loggerConfig.output = NULL;
163+
return;
163164
}
165+
166+
memcpy(file, arg, len);
167+
file[len] = '\0';
168+
loggerConfig.output = fopen(file, "w");
164169
if (!loggerConfig.output) {
165170
loggerConfig.output = stderr;
166171
LOG_ERR("Cannot open output file %s - logging disabled", file);
167172
loggerConfig.output = NULL;
168173
return;
169174
}
170-
}
171-
172-
else {
175+
} else {
173176
loggerConfig.output = stderr;
174177
LOG_ERR("Logging output not set - logging disabled");
175178
loggerConfig.output = NULL;

test/utils/utils_log.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ void helper_checkConfig(util_log_config_t *expected, util_log_config_t *is) {
9090
}
9191

9292
TEST_F(test, parseEnv_errors) {
93+
expected_message = "";
9394
loggerConfig = {0, 0, LOG_ERROR, LOG_ERROR, NULL};
9495
expect_fput_count = 0;
9596
expected_stream = stderr;
@@ -105,6 +106,9 @@ TEST_F(test, parseEnv_errors) {
105106
helper_checkConfig(&b, &loggerConfig);
106107
helper_log_init("_level:debug");
107108
helper_checkConfig(&b, &loggerConfig);
109+
expect_fput_count = 1;
110+
expected_message = "[ERROR UMF] Cannot open output file - path too long\n";
111+
helper_log_init(("output:file," + std::string(300, 'x')).c_str());
108112
}
109113

110114
TEST_F(test, parseEnv) {
@@ -148,17 +152,20 @@ TEST_F(test, parseEnv) {
148152
output.first + ";" +
149153
timestamp.first + ";" + pid.first;
150154
b = loggerConfig = {0, 0, LOG_ERROR, LOG_ERROR, NULL};
155+
expect_fput_count = 0;
156+
expect_fopen_count = 0;
157+
expected_stream = stderr;
158+
expected_message = "";
159+
expected_filename = "";
160+
auto n = output.first.find(',');
161+
if (n != std::string::npos) {
162+
expected_filename = output.first.substr(n + 1);
163+
}
164+
151165
if (output.second != NULL) {
152166
b.output = output.second;
153167
if (output.second == MOCK_FILE_PTR) {
154168
expect_fopen_count = 1;
155-
} else {
156-
expect_fopen_count = 0;
157-
}
158-
auto n = output.first.find(',');
159-
if (n != std::string::npos) {
160-
expected_filename =
161-
output.first.substr(n + 1).c_str();
162169
}
163170
expected_stream = output.second;
164171
b.timestamp = timestamp.second;
@@ -168,13 +175,14 @@ TEST_F(test, parseEnv) {
168175
b.level = (util_log_level_t)logLevel.second;
169176
if (logLevel.second <= LOG_INFO) {
170177
expect_fput_count = 1;
171-
} else {
172-
expect_fput_count = 0;
173178
}
174179
} else {
175180
expect_fput_count = 1;
176-
expect_fopen_count = 0;
177-
expected_stream = stderr;
181+
if (expected_filename.size() > MAX_FILE_PATH) {
182+
expected_message =
183+
"[ERROR UMF] Cannot open output file - "
184+
"path too long\n";
185+
}
178186
}
179187
helper_log_init(envVar.c_str());
180188
helper_checkConfig(&b, &loggerConfig);

0 commit comments

Comments
 (0)