Skip to content

Commit d5b7904

Browse files
committed
log: Allow nvme_msg usage with invalid root pointer
24ac082 ("Add 'nvme_root_t' argument to nvme_msg()") and 19b1283 ("Move global logging variables into nvme_root_t") moved the logging controller knobs into the root object. In case the pointer is invalid we should not try to use it. Instead just bailing out just log the line. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 19a957f commit d5b7904

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

src/nvme/log.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,38 +53,42 @@ __nvme_msg(nvme_root_t r, int lvl,
5353
};
5454
char *header __cleanup__(cleanup_charp) = NULL;
5555
char *message __cleanup__(cleanup_charp) = NULL;
56-
int idx;
56+
int idx = 0;
5757

58-
if (r->log_timestamp) {
58+
if (r && lvl > r->log_level)
59+
return;
60+
61+
if (r && r->log_timestamp) {
5962
struct timespec now;
6063

6164
clock_gettime(LOG_CLOCK, &now);
6265
snprintf(timebuf, sizeof(timebuf), "%6ld.%06ld",
6366
(long)now.tv_sec, now.tv_nsec / 1000);
67+
idx |= 1 << 2;
6468
} else
6569
*timebuf = '\0';
6670

67-
if (r->log_pid)
71+
if (r && r->log_pid) {
6872
snprintf(pidbuf, sizeof(pidbuf), "%ld", (long)getpid());
69-
else
73+
idx |= 1 << 1;
74+
} else
7075
*pidbuf = '\0';
7176

72-
idx = ((r->log_timestamp ? 1 : 0) << 2) |
73-
((r->log_pid ? 1 : 0) << 1) | (func ? 1 : 0);
77+
if (func)
78+
idx |= 1 << 0;
7479

75-
if (asprintf(&header, formats[idx], timebuf, pidbuf, func ? func : "")
76-
== -1)
80+
if (asprintf(&header, formats[idx],
81+
timebuf, pidbuf, func ? func : "") == -1)
7782
header = NULL;
7883

7984
va_start(ap, format);
8085
if (vasprintf(&message, format, ap) == -1)
8186
message = NULL;
8287
va_end(ap);
8388

84-
if (lvl <= r->log_level)
85-
fprintf(fp, "%s%s", header ? header : "<error>",
86-
message ? message : "<error>");
87-
89+
fprintf(fp, "%s%s",
90+
header ? header : "<error>",
91+
message ? message : "<error>");
8892
}
8993

9094
void nvme_init_logging(nvme_root_t r, int lvl, bool log_pid, bool log_tstamp)

0 commit comments

Comments
 (0)