Addressing the call to SafeStorageHandleRegister()
:
Given your original prototype:
SafeStorageHandleRegister(
_In_reads_bytes_opt_(UsernameLength) PCHAR Username,
_In_ USHORT UsernameLength,
_In_reads_bytes_opt_(PasswordLength) PCHAR Password,
_In_ USHORT PasswordLength);
One issue is that in your call:
SafeStorageHandleRegister(arg1, (USHORT)strlen(arg1), arg2, (USHORT)strlen(arg2));
...the number of bytes specified in arguments 3 & 4 does not include room for the null byte, resulting in an incorrect number of bytes value.
strlen()
does not include
in its calculation, but
is at the end of all C strings (which are being used here.) Use sizeof(arg1)
and sizeof(arg2)
instead. eg:
SafeStorageHandleRegister(arg1, (USHORT)sizeof(arg1), arg2, (USHORT)sizeof(arg2));
And, because the sizeof
macro returns size_t
if this is your function prototype, (i.e. one that you can modify.) I suggest changing it from using USHORT
to size_t
for the 2nd and 4th arguments:
SafeStorageHandleRegister(
_In_reads_bytes_opt_(UsernameLength) PCHAR Username,
_In_ size_t UsernameLength,
_In_reads_bytes_opt_(PasswordLength) PCHAR Password,
_In_ size_t PasswordLength);
...making it more readable because there will be no casts:
SafeStorageHandleRegister(arg1, sizeof(arg1), arg2, sizeof(arg2));
_Addressing potential issues for SAL notations:
This may or may not be causing your failure, but important to note anyway...
"If the caller is not allowed to pass in a null pointer, use _In_
or _Out_
instead of _In_opt_
or _Out_opt_
. This applies even to a function that checks its parameters and returns an error if it is NULL
when it should not be."...
So, for example:
// Incorrect
void Func1(_Out_opt_ int *p1)
{
*p = 1;
}
// Correct
void Func2(_Out_ int *p1)
{
*p = 1;
}
Since you do not want your functions to have the option to pass NULL
pointers it would be good to consider modifying the SAL notation from _In_reads_bytes_opt_
to _In_reads_bytes_
(removing the option)
SafeStorageHandleRegister(
_In_reads_bytes(UsernameLength) PCHAR Username,
_In_ size_t UsernameLength,
_In_reads_bytes(PasswordLength) PCHAR Password,
_In_ size_t PasswordLength);
Much more on SAL notations.