upvote
Yup, it's a bug. The '%' character gets you into the while loop and into the else clause. The switch increments to the nul terminator, which then executes prterror(0). Then fmt is incremented again so it points just outside of the string. As long as that out-of-bounds byte is not also the nul value the loop continues into out-of-bounds territory.

Wait - does prterror cause the program to exit? It's not defined, so maybe the program does terminate, maybe it doesn't.

reply
Right. If fmt was "%d%d" and val had only one element that'd cause an out-of-bounds read as well. Can't say it's a bug without seeing the rest of the code though; if the function is used correctly everywhere, there's no bug.
reply
++fmt increments the counter before it's read, so it'll point at the null terminator and enter the default case.

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()

reply
Was suspecting the same thing. But I think, because C strings are null-terminated, they allow reading up to 1 byte after its end.

In your example, "%" would cause switch ('\0') which would fall in the default case.

reply
Yeah, but that only prints an error and keeps going, so next loop it will read past the null terminator. So I think looperhacks is correct.

The function should probably return on error.

reply