[Bug middle-end/88059] Spurious stringop-overflow warning with strlen, malloc and strncpy
molli.bender at gmail dot com
gcc-bugzilla@gcc.gnu.org
Tue Jul 2 18:52:00 GMT 2019
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88059
Moritz Bender <molli.bender at gmail dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |molli.bender at gmail dot com
--- Comment #2 from Moritz Bender <molli.bender at gmail dot com> ---
I don't know whether this comment will ever be read, but I'd like to add that I
have the same problem with strncpy incorrectly getting the "warning: 'strncpy'
specified bound depends on the length of the source argument". I do believe
that this warning is wrong in the case that the destination buffer is also
dependant on the strlen of the source string.
Assume the following code:
char my_string[strlen(argv[1] - 1)];
strncpy(my_string, argv[1], strlen(argv[1]) - 5);
memcpy(&my_string[strlen(argv[1]) - 5], ".7z", 4);
printf("my string: %s\n", my_string);
This code will generate the mentioned warning, although I don't think it
should. I HAVE to use strlen to achieve what I want to; this is a relatively
common use case. I know I can use memcpy instead of the strncpy, but regarding
that her I'm just dealing with strings I'd rather use strncpy.
More information about the Gcc-bugs
mailing list