limiter: fix internal priority mapping 08/193908/1
authorMichal Bloch <m.bloch@samsung.com>
Tue, 27 Nov 2018 10:11:23 +0000 (11:11 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Tue, 27 Nov 2018 10:26:36 +0000 (11:26 +0100)
Now makes sure the result is one of the chars "?*VDIWEFS".

Previously it could return lowercase characters, numerical
characters (except '8' which stands for SILENT but including
'1' which stands for DEFAULT which is not a real priority),
or the null character (when passed the integers 0 or 1).

Change-Id: I3766d699e553d71265a979ababe59fccdd16d571
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
src/libdlog/loglimiter.c

index 7c02cfe..4d610bc 100644 (file)
@@ -120,7 +120,10 @@ static int rule_match(struct rule* r1, unsigned key, const char* s, int prio)
 /* Translate fancy priority notation into common denominator */
 static int util_prio_to_char(int prio)
 {
+       static const int NUM_CHAR_OFFSET = '0' - 0;
        static const char pri_table[DLOG_PRIO_MAX] = {
+               [DLOG_UNKNOWN] = '?',
+               [DLOG_DEFAULT] = '*',
                [DLOG_VERBOSE] = 'V',
                [DLOG_DEBUG] = 'D',
                [DLOG_INFO] = 'I',
@@ -130,26 +133,35 @@ static int util_prio_to_char(int prio)
                [DLOG_SILENT] = 'S',
        };
 
-       if (DLOG_PRIO_MAX > prio && prio >= 0) {
+       switch (prio) {
+       case 'V':
+       case 'D':
+       case 'I':
+       case 'W':
+       case 'E':
+       case 'F':
+       case 'S':
+       case '*':
+               return prio;
+
+       case 'v':
+       case 'd':
+       case 'i':
+       case 'w':
+       case 'e':
+       case 'f':
+       case 's':
+               return toupper(prio);
+
+       case '0' ... (DLOG_PRIO_MAX - 1 + NUM_CHAR_OFFSET):
+               return pri_table[prio - NUM_CHAR_OFFSET];
+
+       case 0 ... (DLOG_PRIO_MAX - 1):
                return pri_table[prio];
-       } else {
-               switch (prio) {
-               case 'V': case 'v': case '1':
-               case 'D': case 'd': case '2':
-               case 'I': case 'i': case '3':
-               case 'W': case 'w': case '4':
-               case 'E': case 'e': case '5':
-               case 'F': case 'f': case '6':
-               case 'S': case 's': case '7':
-               case '*':
-                       return prio;
-
-               default:
-                               ;;
-               }
-       }
 
-       return '?';// LCOV_EXCL_LINE
+       default:
+               return '?';
+       }
 }
 
 /* The key is built from TAG and priority by DJB algorithm (Dan Bernstein).