I have large C++ project written by someone else long time ago. It contains code like:
string CVersion::GetVersionStr() const
{
string ret;
char VersionStr[100];
DWORD v1, v2, v3, Build;
GetVersion(&v1, &v2, &v3, &Build);
sprintf(VersionStr, "%d.%d.%d.%d", v1, v2, v3, Build);
return string(VersionStr);
}
Now I think because of wrong format specifier (%d
) this code has undefined behaviour.
DWORD
on my PC is declared as
typedef unsigned long DWORD;
My questions are:
- does code contain undefined behaviour?
- Is there any platform/situation where it would not be undefined behaviour? Maybe it is fine for some values of v1?
- The software has been working correctly for long time, so can it happen that in practice, despite above is undefined behaviour, the software still works fine?
PS. This software was written something like 10 years ago using Visual Studio