I'm reading a file block by block; I want to concatenate the blocks together. For this, I do a strncpy
to copy the end of the block into the buffer (TMP_BUF_SIZE
is the size of the buffer):
strncpy(tmpData, &(data[nextToRead]), TMP_BUF_SIZE -1);
Then I calculate the space left in tmpData
(NB_READ
is the size of data
):
nLast=NB_READ - nextToRead;
Finally, I do another strncpy
to copy the beginning of the next block into the end of the buffer:
strncpy(&(tmpData[nLast]), data, TMP_BUF_SIZE - nLast - 1);
On compile, GCC reports an error for the second strncpy
:
error: the output of « strncpy » could be truncated by copying between 0 and 127 bytes from a string of size 1023
But this is exactly what I want to do. How can I prevent GCC
reporting this, without removing -Werror
?
PS: the original error message, might not perfectly translated:
erreur: la sortie de « strncpy » peut être tronquée en copiant entre 0 et 127 octets depuis une chaîne de longueur 1023 [-Werror=stringop-truncation]
EDIT:
As mentioned by @GuillaumePetitjean there is this thread; they provide a solution by turning off the compiler warning. I don't want to change the behavior of the compiler. I'd like a solution, in the code, that doesn't raise the warning/error.