Notes from C++ static analysis

renoX renozyx at gmail.com
Thu Jun 27 03:57:49 PDT 2013


On Wednesday, 26 June 2013 at 18:08:10 UTC, bearophile wrote:
[cut]
> The most common problem they find are errors in the format 
> string of printf-like functions (despite the code is C++):
>
>>The top type of bug that /analyze finds is format string errors 
>>– mismatches between printf-style format strings and the 
>>corresponding arguments. Sometimes there is a missing argument, 
>>sometimes there is an extra argument, and sometimes the 
>>arguments don’t match, such as printing a float, long or ‘long 
>>long’ with %d.<
>
> Such errors in D are less bad, because writef("%d",x) is usable 
> for all kind of integral values. On the other hand this D 
> program prints just "10" with no errors, ignoring the second x:
>
> import std.stdio;
> void main() {
>     size_t x = 10;
>     writefln("%d", x, x);
> }
>
> In a modern statically typed language I'd like such code to 
> give a compile-time error.

An even better thing would be to have a design which reduce a lot 
the probability of format string error, see Scala:

val name = "James"
println(s"Hello, $name")


renoX


More information about the Digitalmars-d mailing list