Here's an example given on MSDN (http://msdn.microsoft.com/en-us/library/hh916383.aspx) that tried to explain SAL annotation could help find a common off-by-one error.
wchar_t * wmemcpy(
_Out_writes_all_(count) wchar_t *dest,
_In_reads_(count) const wchar_t *src,
size_t count)
{
size_t i;
for (i = 0; i <= count; i++) { // BUG: off-by-one error
dest[i] = src[i];
}
return dest;
}
I don't quite get this example. In this example, it looks like the function signature contains a parameter called count
, so we can use In_reads_(count)
to make sure the memory space that src
is pointing to has at least such number of bytes.
My question is, what if there is a function with signature like this
memcpy_example(wchar_t* dest, wchar_t* src)
In this case, the signature does not contain any information about the sizes. Can I use SAL to tell debugger that dest
should be same size or 1-byte larger than src
?