Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
whitslack opened this issue Sep 4, 2023 · 0 comments
Open

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

whitslack opened this issue Sep 4, 2023 · 0 comments

Comments

@whitslack
Copy link

The contract for tal_strndup says:

ccan/ccan/tal/str/str.h

Lines 22 to 30 in cd56b18

/**
* tal_strndup - duplicate a limited amount of a string.
* @ctx: NULL, or tal allocated object to be parent.
* @p: the string to copy (can be take()).
* @n: the maximum length to copy.
*
* Always gives a nul-terminated string, with strlen() <= @n.
* The returned string will have tal_count() == strlen() + 1.
*/

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.

ccan/ccan/tal/str/str.c

Lines 27 to 30 in cd56b18

if (likely(p))
len = strnlen(p, n);
else
len = n;

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant