I think what he means by "And it doesn't null-terminate v properly?" is that when you use strlen, then the value it returns is the length without the final null terminator strlen("test\0")==4, and since the for loop uses < u_s instead of <= u_s it will not copy the null terminator to the other string, making this a segfault casino. Also if the length of u is larger than v you end up with problems.
I don't know "_plib_strlen", but even on the off-chance that it includes the trailing \0 terminator in the length count (quite odd, that), it's still really weird to then see the defensive \0-char-assignment to v[0]. More likely it's not copying the trailing \0. It's a bit weird to copy a string except the trailing \0, but it's even weirder to copy a string except the trailing \0 except when it's empty, and then DO copy that trailing \0.
i.e. the macro kind of makes sense then - it _is_ counting the string length including the 0 terminator, with a special case returning 0 when the string pointer is itself null. That just means the copy function projects both an empty and missing "u" string to the empty "v" output.
View all comments
Show parent comment
5
u/AyrA_ch Jan 27 '26 edited Jan 27 '26
I think what he means by "And it doesn't null-terminate v properly?" is that when you use strlen, then the value it returns is the length without the final null terminator
strlen("test\0")==4, and since the for loop uses< u_sinstead of<= u_sit will not copy the null terminator to the other string, making this a segfault casino. Also if the length ofuis larger thanvyou end up with problems.