Do not rely on prio_to_str() being always inline
authorLucas De Marchi <lucas.demarchi@intel.com>
Thu, 9 Oct 2014 03:01:45 +0000 (00:01 -0300)
committerLucas De Marchi <lucas.demarchi@intel.com>
Thu, 9 Oct 2014 04:26:39 +0000 (01:26 -0300)
This function was declared as always-inline so there was not really a
problem in returning prioname, that could possibly point to the local
buf[] variable.

However static analysis tools are often confused about this and being
always-inline was just a workaround to make it work.

So, let's move the buffer to the caller. We have only 2 callers so it
doesn't matter much. This always reduce the size of log.o, since now the
function is not inlined anymore. Below is the size for "-g -O2" with
gcc:

before:
   text    data     bss     dec     hex filename
   1325       4       1    1330     532 tools/log.o

after:
   text    data     bss     dec     hex filename
   1171       4       1    1176     498 tools/log.o

tools/log.c

index 05ff96f..4ae4aed 100644 (file)
 
 #include "kmod.h"
 
+#define PRIO_MAX_SIZE 32
+
 static bool log_use_syslog;
 static int log_priority = LOG_ERR;
 
-static _always_inline_ const char *prio_to_str(int prio)
+static const char *prio_to_str(char buf[static PRIO_MAX_SIZE], int prio)
 {
        const char *prioname;
-       char buf[32];
 
        switch (prio) {
        case LOG_CRIT:
@@ -54,7 +55,7 @@ static _always_inline_ const char *prio_to_str(int prio)
                prioname = "DEBUG";
                break;
        default:
-               snprintf(buf, sizeof(buf), "LOG-%03d", prio);
+               snprintf(buf, PRIO_MAX_SIZE, "LOG-%03d", prio);
                prioname = buf;
        }
 
@@ -65,9 +66,12 @@ _printf_format_(6, 0)
 static void log_kmod(void *data, int priority, const char *file, int line,
                     const char *fn, const char *format, va_list args)
 {
-       const char *prioname = prio_to_str(priority);
+       char buf[PRIO_MAX_SIZE];
+       const char *prioname;
        char *str;
 
+       prioname = prio_to_str(buf, priority);
+
        if (vasprintf(&str, format, args) < 0)
                return;
 
@@ -108,6 +112,7 @@ void log_close(void)
 
 void log_printf(int prio, const char *fmt, ...)
 {
+       char buf[PRIO_MAX_SIZE];
        const char *prioname;
        char *msg;
        va_list args;
@@ -122,7 +127,7 @@ void log_printf(int prio, const char *fmt, ...)
        if (msg == NULL)
                return;
 
-       prioname = prio_to_str(prio);
+       prioname = prio_to_str(buf, prio);
 
        if (log_use_syslog)
                syslog(prio, "%s: %s", prioname, msg);