Cppcheck has detected a potential problem in a code like this:
float a, b, c;
int count = sscanf(data, "%f,%f,%f", &a, &b, &c);
It says that: "scanf without field width limits can crash with huge data". How is that possible? Is that a known bug in some sscanf implementations? I understand that the numbers may overflow (numerically), but how could the program crash? Is that a false positive in cppcheck?
I have found a similar question: scanf Cppcheck warning, but the answer is not completely satisfying. The answer mentions type safety, but that should not be an issue here.
sscanf_s
isn’t portable and also not actually safe, despite what the name suggests and Microsoft claims. – Habitatsscanf_s
doesn’t actually check (and cannot check) whether the buffer size is correct. So it protects only insofar as it makes the buffer size explicit. A far superior method is preventing buffer overflows in the first place, and C++ makes this trivial. (Also, at least one of the “safe” commands – but I don’t remember which – had a buffer overflow bug. Oh the irony.) – Habitat