Skip to content

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

@whitslack

Description

@whitslack

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions