I am wondering if it's possible to detect this kind of buffer overflow somehow in Windows. Buffer is global ( not on stack ) so /RTC in Visual Studio 2008, VS2012 is not checking it. MinGW gcc also failed.
#include <stdio.h>
char buffer[2];
void main()
{
sprintf(buffer,"12345");
}
My first thought was static analysis.
- VS2012 Code Analysis : nothing
- CppCheck: nothing
- PCLint Online: nothing ( http://www.gimpel-online.com/OnlineTesting.html )
- PVS-Studio: nothing
another solution is to use _s version.
#include <stdio.h>
char buffer[2];
void main()
{
sprintf_s(buffer, sizeof(buffer), "12345");
}
but with code looking like that
#include <stdio.h>
char buffer[2];
void main()
{
sprintf_s(buffer, 20, "12345");
}
there is still same problem of not detected buffer overrun.
Is is possible to use memory guard, canaries on global data ( like on stack ) as well or resolve this problem using better Static,Dynamic Analysis?