Do not rely on prio_to_str() being always inline

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
This commit is contained in:
Lucas De Marchi 2014-10-09 00:01:45 -03:00
parent b95506ff61
commit 66bf1a7ff9

View File

@ -26,13 +26,14 @@
#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);