Recently a developer at my company committed some code that looked something like this:
char buf[50];
string str;
str = sprintf(buf, "%s", "test");
//proceeds to use str
The thing is, it slipped through CI because the compiler raised no warnings despite -Wall and -Werror being set.
Shouldn't this be an obvious type mismatch? You can't assign an integer to an std::string type without std::to_string...
I took a look at the list of string assignments but I can't tell which one is being triggered in this case? Is it using one of these?
c-string (2) string& operator= (const char* s);
character (3) string& operator= (char c);
I'm guessing the latter, but that still seems like a compiler fail since sprintf clearly returns int not char.
Is there a warning we could have enabled that could have saved us in this case not covered by -Wall?
Edit:
A related thread I found: https://stackoverflow.com/a/39285668/2516916
-Wimplicit-int-conversionincluded in-Wall?-Wimplicit-int-conversionstrwith anint. It assigns aninttostrwhich has already been initialized. It was initialized when it was created.