strdup invalid read of size 4 when string literal is ending with newline \n

后端 未结 2 1783
庸人自扰
庸人自扰 2021-01-15 05:57

I am getting an invalid read error when the src string ends with \\n, the error disappear when i remove \\n:

#include 

        
2条回答
  •  Happy的楠姐
    2021-01-15 06:25

    The error message seems to indicate that it's strlen that read past the malloced buffer allocated by strdup. On a 32-bit platform, an optimal strlen implementation could read 4 bytes at a time into a 32-bit register and do some bit-twiddling to see if there's a null byte in there. If near the end of the string, there are less than 4 bytes left, but 4 bytes are still read to perform the null byte check, then I could see this error getting printed. In that case, presumably the strlen implementer would know if it's "safe" to do this on the particular platform, in which case the valgrind error is a false positive.

提交回复
热议问题