/SALDemo

Source code annotation (SAL) Demo in C/C++

Primary LanguageC++

Using VC++, compile at /W4 - you get no warnings.

Clearly, there's a memory corruption issue as the code allocates 32 bytes and creates 60 '!' characters in that buffer. Oops!

Now change: char *szBuff to Out_cap(cb) char *szBuff

Now recompile. You get two warnings, one is because of the memory corruption and the other is about a mismatch from malloc. When malloc() fails it returns NULL, but char *szBuff cannot be NULL, otherwise the annotation would include _Opt (optional)

The compiler knows that malloc() can return null because of its SAL annotation in corecrt_malloc.h:

Check_return Ret_maybenull Post_writable_byte_size(_Size) _ACRTIMP _CRTALLOCATOR _CRT_JIT_INTRINSIC _CRTRESTRICT _CRT_HYBRIDPATCHABLE void* __cdecl malloc( In _CRT_GUARDOVERFLOW size_t _Size );

Note the use of Ret_maybenull

So to fix these issues, change: FillMemory(d, 60, '!'); to if (d) FillMemory(d, 32, '!');

Voila :)