rustyrussell / ccan

The C Code Archive Network
http://ccodearchive.net/
1.09k stars 206 forks source link

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

Open whitslack opened 1 year ago

whitslack commented 1 year ago

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.