miniprintf(char fmt[], int val[])
{
int i = 0;
while( *fmt != '\0' ) {
if( *fmt != '%' )
putchar(*fmt);
else
switch(*++fmt) {
case 'd' : putint(val[i++]); break;
case '%' : putchar('%'); break;
default : prterror(*fmt); break;
}
fmt++;
}
}
If the static string was "%", the else branch would move past the end of the string. Assuming prterror doesn't crash the program, this reads out-of-bounds, right?Wait - does prterror cause the program to exit? It's not defined, so maybe the program does terminate, maybe it doesn't.
Edit: I guess it may not technically be a bug as it's just a small function, but using a single % instead of %% (which is what printf expects) would be a format error, hence prterror()
In your example, "%" would cause switch ('\0') which would fall in the default case.
The function should probably return on error.
Partial Evaluation and Automatic Program Generation by Neil Jones et al. - https://web.archive.org/web/20071012081310/http://www.itu.dk...
Even though it is its own project, most folks use it alongside GCC.
https://sourceware.org/gdb/current/onlinedocs/gdb.html/Calli...