2

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.

Mike P
  • 742
  • 11
  • 26
Phantom
  • 833
  • 1
  • 9
  • 26
  • Does this answer your question? [gcc-8 -Wstringop-truncation what is the good practice?](https://stackoverflow.com/questions/50198319/gcc-8-wstringop-truncation-what-is-the-good-practice) – Guillaume Petitjean Dec 09 '19 at 13:48
  • Why not using `strcat`? – Guillaume Petitjean Dec 09 '19 at 13:51
  • Because I didn't think about it ^^ And in the link you give, they suggest `memcpy`, which could be a work around. I'll try `strncat` – Phantom Dec 09 '19 at 13:55
  • With `strncat` the problem is the same – Phantom Dec 09 '19 at 14:03
  • Generally speaking, all these string manipulation functions look obvious but they can lead to a fair amount of nasty bugs... – Guillaume Petitjean Dec 09 '19 at 14:03
  • Do something like this: https://onlinegdb.com/rkgWI0oaB. Just make sure the dest string is large enough to contain the result of the concatenation – Guillaume Petitjean Dec 09 '19 at 14:11
  • It's what I do, but gcc still complain – Phantom Dec 09 '19 at 14:16
  • 1
    `strncpy` is a dangerous function that does not null terminate the destination string in case you copy the same amount of characters as the specified buffer size. This is supposedly what the warning is about. Use `strcat` instead. – Lundin Dec 09 '19 at 14:25
  • As I said before, I tried with `strncat` and gcc throw the same error. I used `memcpy` instead, and gcc stoped to complain. And I know, it does not null terminate the destination string. It's not a solution I like, but for now it's the only one for which gcc does not complain. – Phantom Dec 09 '19 at 14:33
  • But do you really need strings ? Or just buffers of bytes ? Because then `memcpy`is fine – Guillaume Petitjean Dec 09 '19 at 14:38
  • I really need strings, because after I parse them. This is to get a complete line in one buffer, and not the begining of a line in one, and the end of the line in an other one – Phantom Dec 09 '19 at 14:41
  • 1
    Use memcpy and always set a zero terminator at the end of the string yourself, to be sure it is done correctly. – Zan Lynx Dec 09 '19 at 15:31
  • [My article on `strncpy`](https://the-flat-trantor-society.blogspot.com/2012/03/no-strncpy-is-not-safer-strcpy.html). `strncpy` is rarely the right tool. (Maybe it is in your case?) – Keith Thompson Sep 18 '20 at 22:49

0 Answers0