Windows 7, Visual Studio 2008, C++
The question is: why is the strcpy_s returning an error that the destination is too small?
I am following a book and playing around with constructors. The constructor in question is this:
Two instances of the class are created:
The first is okay (uses a different constructor), and the second fails at the strcpy_s. So I when step into the strcpy_s function the code shown is from file tcsscpy_s.inl and looks like:
Reference lines
_VALIDATE_STRING(_DEST, _SIZE);
_VALIDATE_POINTER_RESET_STRING(_SRC, _DEST, _SIZE);
First I find it curious that the debugger does not provide any information about _DEST, _SIZE, etc, so I cannot determine what it is doing.
Regardless, the variable "available" gets the value 3, the size of the source string. The while loop does the copy and available bumps down to 0. That looks right to me. Then the debugger hits:
There is something wrong here.
The if is taken and and Visual Studio pops up an error box containaing, in part:
ExpressionL"Buffer is too small" && 0 )
What else could happen? Available starts at the size of the string being copied and counts down to zero. Why should this result in an error?
We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
The question is: why is the strcpy_s returning an error that the destination is too small?
I am following a book and playing around with constructors. The constructor in question is this:
Code:
C_box( char * new_name, float w = 1.0, float l = 1.0, float h = 1.0 )
{
if( new_name == NULL )
{
name = NULL;
}
else
{
size_t name_size = strlen( new_name );
// this started with "name_size + 1" then bumped
// it up to 10. That made no difference.
name = new char[ name_size + 10 ];
errno_t error_number = strcpy_s( name, name_size, new_name );
}
width = w;
length = l;
height = h;
};
Two instances of the class are created:
Code:
C_box box1;
C_box box2( "two", 2.0, 2.0, 2.0 );
The first is okay (uses a different constructor), and the second fails at the strcpy_s. So I when step into the strcpy_s function the code shown is from file tcsscpy_s.inl and looks like:
Code:
_FUNC_PROLOGUE
errno_t __cdecl _FUNC_NAME(_CHAR *_DEST, size_t _SIZE, const _CHAR *_SRC)
{
_CHAR *p;
size_t available;
/* validation section */
_VALIDATE_STRING(_DEST, _SIZE);
_VALIDATE_POINTER_RESET_STRING(_SRC, _DEST, _SIZE);
p = _DEST;
available = _SIZE;
while ((*p++ = *_SRC++) != 0 && --available > 0)
{
}
if (available == 0)
{
_RESET_STRING(_DEST, _SIZE);
_RETURN_BUFFER_TOO_SMALL(_DEST, _SIZE);
}
_FILL_STRING(_DEST, _SIZE, _SIZE - available + 1);
_RETURN_NO_ERROR;
}
_VALIDATE_STRING(_DEST, _SIZE);
_VALIDATE_POINTER_RESET_STRING(_SRC, _DEST, _SIZE);
First I find it curious that the debugger does not provide any information about _DEST, _SIZE, etc, so I cannot determine what it is doing.
Regardless, the variable "available" gets the value 3, the size of the source string. The while loop does the copy and available bumps down to 0. That looks right to me. Then the debugger hits:
Code:
if (available == 0)
The if is taken and and Visual Studio pops up an error box containaing, in part:
ExpressionL"Buffer is too small" && 0 )
What else could happen? Available starts at the size of the string being copied and counts down to zero. Why should this result in an error?
We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)