Fix cast to enum 94/172894/2
authorMichal Bloch <m.bloch@samsung.com>
Fri, 16 Mar 2018 13:48:10 +0000 (14:48 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Mon, 19 Mar 2018 12:43:26 +0000 (13:43 +0100)
Checks against values outside the enum range are legal for the compiler
to optimize out, ergo they have to be done before the cast.

Change-Id: I3059b3a16e6331c3a3a1389f0a4acfbae1d3a16f
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
src/shared/logprint.c

index 9b23342..4aa7167 100644 (file)
@@ -572,11 +572,12 @@ int log_process_log_buffer(struct logger_entry *entry_raw, log_entry *entry)
        entry->pid = entry_raw->pid;
        entry->tid = entry_raw->tid;
 
-       entry->priority = entry_raw->msg[0];
-       if (entry->priority < 0 || entry->priority > DLOG_SILENT) {
-               fprintf(stderr, "Wrong priority message, %d but should be <%d\n", entry->priority, DLOG_SILENT);
+       const unsigned char pri = entry_raw->msg[0]; // cannot cast to log_priority directly because then the compiler is allowed to optimize the below check out
+       if (pri > DLOG_SILENT) {
+               fprintf(stderr, "Wrong priority message, %u but should be <%u\n", pri, DLOG_SILENT);
                return -1;
        }
+       entry->priority = (log_priority)pri;
 
        entry->tag = entry_raw->msg + 1;
        if (!strlen(entry->tag)) {