ccan icon indicating copy to clipboard operation
ccan copied to clipboard

`tal_strndup(ctx, NULL, 1)` breaks contract

Open whitslack opened this issue 1 year ago • 0 comments

The contract for tal_strndup says:

https://github.com/rustyrussell/ccan/blob/cd56b18ae87de6981b88abbe52544d8cc9f8aa0e/ccan/tal/str/str.h#L22-L30

However, when tal_strndup(ctx, NULL, n) is called with n being any non-zero integer, the returned string will have tal_count() equal to n + 1 (i.e., at least 2), but strlen() will be equal to zero. 2 != 0 + 1.

https://github.com/rustyrussell/ccan/blob/cd56b18ae87de6981b88abbe52544d8cc9f8aa0e/ccan/tal/str/str.c#L27-L30

It seems like this^ really should say len = 0; in the else branch.

whitslack avatar Sep 04 '23 03:09 whitslack