[Bug middle-end/88059] Spurious stringop-overflow warning with strlen, malloc and strncpy
rangel_fischer at yahoo dot com.br
gcc-bugzilla@gcc.gnu.org
Mon Mar 7 20:01:11 GMT 2022
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88059
Rangel Moreira Fischer <rangel_fischer at yahoo dot com.br> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |rangel_fischer at yahoo dot com.br
--- Comment #4 from Rangel Moreira Fischer <rangel_fischer at yahoo dot com.br> ---
Same problem too.
static void write_my_message( char* message, size_t max_message_size, char*
my_message )
{
size_t messag_len = strlen(my_message) + 1;
if( messag_len > max_message_size )
{
// trunc message
strncpy(message, my_message, max_message_size - 1); // ex:
max_message_size = max buf size = 50(0 to 49). 0 to 48 = 49
elements(max_message_size - 1).
message[max_message_size - 1] = '\0'; // message[50-1] = message[49] =
'\0'.
}
else
{
strncpy(message, my_message, messag_len);
}
}
In function 'write_my_message':
error: 'strncpy' specified bound depends on the length of the source argument
[-Werror=stringop-overflow=]
strncpy(message, my_message, messag_len);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: length computed here
size_t messag_len = strlen(my_message) + 1;
^~~~~~~~~~~~~~~~~~
More information about the Gcc-bugs
mailing list