简体   繁体   中英

sprintf/printf/fprintf - tool to identify format specifiers mismatches to param counts in multiple src files

I am working on several old programs written in c (100s of files) that have had some mismatch with printf() family functions. Specifically the number of parameters and number of format specifiers mismatch.
Is there a tool that exists that I could use to easily find the line numbers in the src files. These programs are segfaulting at run time, but I want to proactively find the issues as opposed to using gdb to find the bug.

Thanks!

Both clang and gcc will throw warnings by default. You might try using -Wall and -Wextra if for some reason you don't see the compiler warnings.

Clang Example:

$ cc parg.c -o parg
parg.c:5:34: warning: more '%' conversions than data arguments [-Wformat]
        printf("arg1: %i\narg2: %i\n", 8);
                                ~^
parg.c:6:33: warning: data argument not used by format string
      [-Wformat-extra-args]
        printf("arg3: %i\n", 3, 5);
               ~~~~~~~~~~~~     ^

If your issue is that you're dynamically building your format strings, then you need to review how you're doing that. I don't know of any software that reviews code and finds out if you're likely to pass bad format strings, since it's such a open-ended problem.

Edit

-Wall and -Wextra are useful flags, but I noticed that it's a bit of cargo-cult programming for me to suggest blaring out all warnings when really what you want are -Wformat and -Wformat-extra-args ( -Wformat actually turns on -Wformat-extra-args ). And, as @JonathanLeffler pointed out in the comments, -Wformat-nonliteral could be useful for ensuring that you look at instances where you pass non-literal strings and analyze them to see if you're likely to cause runtime errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM