Why is GCC complaining with Wstringop-overflow?

383 views Asked by At

I have code which looks like this:

char* newChar = new char[strlen(inputCharArray)+1];
if (NULL == newChar) {
    return;
}
strncpy(newChar, inputCharArray, strlen(inputCharArray));
newChar[strlen(inputCharArray)] = '\0';

This seems for me like totally valid code. But GCC 9.2.1 is complaining about it, with the following warning:

warning: 'char* strncpy(char*, const char*, size_t)' specified bound depends on the length of the source argument [-Wstringop-overflow=]

0

There are 0 answers