debugging sprintf -VS/C++
-
Is there a tool available to process source code to detect mismatches between the sprintf format string and the arguments that follow? For example specifying a %s and neglecting to provide a c string to satisfy it, etc. I use hundreds of sprintf's to format log messages and am looking for a way to insure correctness without eyeballing each and everyone of them. When this happens during runtime in my code the program thread seems to hang and when it's the main server thread the other secondary threads cease to get serviced and as they are designed to timeout, they do, and a programmatic exit occurs.
-
Is there a tool available to process source code to detect mismatches between the sprintf format string and the arguments that follow? For example specifying a %s and neglecting to provide a c string to satisfy it, etc. I use hundreds of sprintf's to format log messages and am looking for a way to insure correctness without eyeballing each and everyone of them. When this happens during runtime in my code the program thread seems to hang and when it's the main server thread the other secondary threads cease to get serviced and as they are designed to timeout, they do, and a programmatic exit occurs.
If you're using C++ don't fart about with sprintf, use std::stringstream - you'll avoid problems of lobbing the wrong thing at a particular format specifier. If you're stuck using C then there's not a lot I can suggest although PC-Lint is pretty good at finding errors in the printf family of functions. Cheers, Ash
-
Is there a tool available to process source code to detect mismatches between the sprintf format string and the arguments that follow? For example specifying a %s and neglecting to provide a c string to satisfy it, etc. I use hundreds of sprintf's to format log messages and am looking for a way to insure correctness without eyeballing each and everyone of them. When this happens during runtime in my code the program thread seems to hang and when it's the main server thread the other secondary threads cease to get serviced and as they are designed to timeout, they do, and a programmatic exit occurs.
Alan Kurlansky wrote:
detect mismatches between the sprintf format string and the arguments that follow?
Yes, there are a couple of tools available. Have a look at the following:
- Splint checks for vulnerabilities and coding mistakes
- PC-Lint the "classic" code analysis tool
- Dr Dobbs had a full article about code analysis
- also see Wikipedia: tools for static code_analysis for C and C++
Happy debugging! :) M PS: This post is for developers who prefer format() over STL string streams. PPS: And if you are one of them, you could have a look at Boost Format library. :)