ibm-power-utilities / librtas

librtas library for Linux on Power systems
GNU Lesser General Public License v2.1
8 stars 14 forks source link

dbg macro issues #20

Open nathanlynch opened 2 years ago

nathanlynch commented 2 years ago

Couple problems I see with dbg() in syscall.h:

#define dbg(_fmt, _args...)                       \
    do {                                  \
        if (dbg_lvl > 0)                      \
            printf("librtas %s(): " _fmt, __func__, ##_args); \
    } while (0)
  1. This seems to defeat the format string type-checking that the compiler can do. cppcheck detects many problems (see below)
  2. printf with the assumption that the output will go somewhere useful or safe is poor practice in a library. syslog may be better?

cppcheck report:

cppcheck --enable=warning .
Checking librtas_src/ofdt.c ...
1/18 files checked 2% done
Checking librtas_src/syscall_calls.c ...
librtas_src/syscall_calls.c:106:2: warning: %ld in format string (no. 3) requires 'long' but the argument type is 'unsigned long'. [invalidPrintfArgType_sint]
 dbg("Return status %d, delaying for %ld ms\n", status, ms);
 ^
librtas_src/syscall_calls.c:704:2: warning: %d in format string (no. 2) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %d, %p) = %d\n", parameter, length, data, rc ? rc : status);
 ^
librtas_src/syscall_calls.c:704:2: warning: %d in format string (no. 3) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %d, %p) = %d\n", parameter, length, data, rc ? rc : status);
 ^
librtas_src/syscall_calls.c:744:2: warning: %d in format string (no. 3) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("() = %d, %d, %d, %d, %d, %d, %d, %d\n", rc ? rc : status, *year,
 ^
librtas_src/syscall_calls.c:744:2: warning: %d in format string (no. 4) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("() = %d, %d, %d, %d, %d, %d, %d, %d\n", rc ? rc : status, *year,
 ^
librtas_src/syscall_calls.c:744:2: warning: %d in format string (no. 5) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("() = %d, %d, %d, %d, %d, %d, %d, %d\n", rc ? rc : status, *year,
 ^
librtas_src/syscall_calls.c:744:2: warning: %d in format string (no. 6) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("() = %d, %d, %d, %d, %d, %d, %d, %d\n", rc ? rc : status, *year,
 ^
librtas_src/syscall_calls.c:744:2: warning: %d in format string (no. 7) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("() = %d, %d, %d, %d, %d, %d, %d, %d\n", rc ? rc : status, *year,
 ^
librtas_src/syscall_calls.c:744:2: warning: %d in format string (no. 8) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("() = %d, %d, %d, %d, %d, %d, %d, %d\n", rc ? rc : status, *year,
 ^
librtas_src/syscall_calls.c:744:2: warning: %d in format string (no. 9) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("() = %d, %d, %d, %d, %d, %d, %d, %d\n", rc ? rc : status, *year,
 ^
librtas_src/syscall_calls.c:811:2: warning: %d in format string (no. 5) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%s, 0x%p, %zd, %d) = %d, %d, %d", loc_code ? loc_code : "NULL",
 ^
librtas_src/syscall_calls.c:811:2: warning: %d in format string (no. 7) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%s, 0x%p, %zd, %d) = %d, %d, %d", loc_code ? loc_code : "NULL",
 ^
librtas_src/syscall_calls.c:811:2: warning: %d in format string (no. 8) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%s, 0x%p, %zd, %d) = %d, %d, %d", loc_code ? loc_code : "NULL",
 ^
librtas_src/syscall_calls.c:865:2: warning: %d in format string (no. 4) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %p, %d, %d, %p) = %d, %d\n", subfunc, workarea,
 ^
librtas_src/syscall_calls.c:865:2: warning: %d in format string (no. 5) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %p, %d, %d, %p) = %d, %d\n", subfunc, workarea,
 ^
librtas_src/syscall_calls.c:865:2: warning: %d in format string (no. 8) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %p, %d, %d, %p) = %d, %d\n", subfunc, workarea,
 ^
librtas_src/syscall_calls.c:1167:2: warning: %d in format string (no. 2) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %d, %d, %d, %d, %d, %d) = %d\n", year, month, day, hour,
 ^
librtas_src/syscall_calls.c:1167:2: warning: %d in format string (no. 3) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %d, %d, %d, %d, %d, %d) = %d\n", year, month, day, hour,
 ^
librtas_src/syscall_calls.c:1167:2: warning: %d in format string (no. 4) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %d, %d, %d, %d, %d, %d) = %d\n", year, month, day, hour,
 ^
librtas_src/syscall_calls.c:1167:2: warning: %d in format string (no. 5) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %d, %d, %d, %d, %d, %d) = %d\n", year, month, day, hour,
 ^
librtas_src/syscall_calls.c:1167:2: warning: %d in format string (no. 6) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %d, %d, %d, %d, %d, %d) = %d\n", year, month, day, hour,
 ^
librtas_src/syscall_calls.c:1167:2: warning: %d in format string (no. 7) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %d, %d, %d, %d, %d, %d) = %d\n", year, month, day, hour,
 ^
librtas_src/syscall_calls.c:1167:2: warning: %d in format string (no. 8) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %d, %d, %d, %d, %d, %d) = %d\n", year, month, day, hour,
 ^
librtas_src/syscall_calls.c:1203:2: warning: %d in format string (no. 2) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %p) = %d\n", parameter, data, rc ? rc : status);
 ^
librtas_src/syscall_calls.c:1233:2: warning: %d in format string (no. 2) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %d, %d, %d, %d, %d, %d) = %d\n", year, month, day, hour,
 ^
librtas_src/syscall_calls.c:1233:2: warning: %d in format string (no. 3) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %d, %d, %d, %d, %d, %d) = %d\n", year, month, day, hour,
 ^
librtas_src/syscall_calls.c:1233:2: warning: %d in format string (no. 4) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %d, %d, %d, %d, %d, %d) = %d\n", year, month, day, hour,
 ^
librtas_src/syscall_calls.c:1233:2: warning: %d in format string (no. 5) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %d, %d, %d, %d, %d, %d) = %d\n", year, month, day, hour,
 ^
librtas_src/syscall_calls.c:1233:2: warning: %d in format string (no. 6) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %d, %d, %d, %d, %d, %d) = %d\n", year, month, day, hour,
 ^
librtas_src/syscall_calls.c:1233:2: warning: %d in format string (no. 7) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %d, %d, %d, %d, %d, %d) = %d\n", year, month, day, hour,
 ^
librtas_src/syscall_calls.c:1233:2: warning: %d in format string (no. 8) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%d, %d, %d, %d, %d, %d, %d) = %d\n", year, month, day, hour,
 ^
librtas_src/syscall_calls.c:1290:2: warning: %d in format string (no. 3) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%p) %d = %d\n", workarea, scope, rc ? rc : status);
 ^
librtas_src/syscall_calls.c:1329:2: warning: %d in format string (no. 3) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
 dbg("(%p) %d = %d\n", workarea, scope, rc ? rc : status);
 ^
nathanlynch commented 1 year ago

This seems to defeat the format string type-checking that the compiler can do.

Actually, GCC's -Wformat-signedness (not included in -Wall or -Wformat) does identify the same problems as cppcheck.