[Bug middle-end/88059] Spurious stringop-overflow warning with strlen, malloc and strncpy

2022-03-07 Thread rangel_fischer at yahoo dot com.br via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88059

--- Comment #6 from Rangel Moreira Fischer  
---
When Compiler Optimization Level: Debug(-Og).
Compile OK.

When Compiler Optimization Level: Optimize for performance(-O2).
Compile Error.

[Bug middle-end/88059] Spurious stringop-overflow warning with strlen, malloc and strncpy

2022-03-07 Thread rangel_fischer at yahoo dot com.br via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88059

--- Comment #5 from Rangel Moreira Fischer  
---
I am using esp-idf sdk. I think gcc version is 8.4.0.

[Bug middle-end/88059] Spurious stringop-overflow warning with strlen, malloc and strncpy

2022-03-07 Thread rangel_fischer at yahoo dot com.br via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88059

Rangel Moreira Fischer  changed:

   What|Removed |Added

 CC||rangel_fischer at yahoo dot 
com.br

--- Comment #4 from Rangel Moreira Fischer  
---
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;
^~