nostrdb: ccan: update to latest.

Only change for us: CCAN_TAL_NEVER_RETURN_NULL can be defined if
we don't override tal error handling.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
Rusty Russell
2024-08-17 15:21:42 +09:30
committed by Daniel D’Aquino
parent a8d7d971b1
commit 7831ede057
5 changed files with 68 additions and 68 deletions

View File

@@ -2,4 +2,4 @@ CCAN imported from https://github.com/rustyrussell/ccan
Use "make update-ccan" at top level to refresh from ../ccan. Use "make update-ccan" at top level to refresh from ../ccan.
CCAN version: init-2577-g1ae4c432 CCAN version: init-2587-gf927e4be

View File

@@ -14,21 +14,14 @@
char *tal_strdup_(const tal_t *ctx, const char *p, const char *label) char *tal_strdup_(const tal_t *ctx, const char *p, const char *label)
{ {
/* We have to let through NULL for take(). */ return tal_dup_arr_label(ctx, char, p, strlen(p) + 1, 0, label);
return tal_dup_arr_label(ctx, char, p, p ? strlen(p) + 1: 1, 0, label);
} }
char *tal_strndup_(const tal_t *ctx, const char *p, size_t n, const char *label) char *tal_strndup_(const tal_t *ctx, const char *p, size_t n, const char *label)
{ {
size_t len; size_t len = strnlen(p, n);
char *ret; char *ret;
/* We have to let through NULL for take(). */
if (likely(p))
len = strnlen(p, n);
else
len = n;
ret = tal_dup_arr_label(ctx, char, p, len, 1, label); ret = tal_dup_arr_label(ctx, char, p, len, 1, label);
if (ret) if (ret)
ret[len] = '\0'; ret[len] = '\0';
@@ -84,9 +77,6 @@ char *tal_vfmt_(const tal_t *ctx, const char *fmt, va_list ap, const char *label
{ {
char *buf; char *buf;
if (!fmt && taken(fmt))
return NULL;
/* A decent guess to start. */ /* A decent guess to start. */
buf = tal_arr_label(ctx, char, strlen(fmt) * 2, label); buf = tal_arr_label(ctx, char, strlen(fmt) * 2, label);
if (!do_vfmt(&buf, 0, fmt, ap)) if (!do_vfmt(&buf, 0, fmt, ap))
@@ -96,9 +86,6 @@ char *tal_vfmt_(const tal_t *ctx, const char *fmt, va_list ap, const char *label
bool tal_append_vfmt(char **baseptr, const char *fmt, va_list ap) bool tal_append_vfmt(char **baseptr, const char *fmt, va_list ap)
{ {
if (!fmt && taken(fmt))
return false;
return do_vfmt(baseptr, strlen(*baseptr), fmt, ap); return do_vfmt(baseptr, strlen(*baseptr), fmt, ap);
} }
@@ -120,13 +107,7 @@ char *tal_strcat_(const tal_t *ctx, const char *s1, const char *s2,
size_t len1, len2; size_t len1, len2;
char *ret; char *ret;
if (unlikely(!s2) && taken(s2)) { len1 = strlen(s1);
if (taken(s1))
tal_free(s1);
return NULL;
}
/* We have to let through NULL for take(). */
len1 = s1 ? strlen(s1) : 0;
len2 = strlen(s2); len2 = strlen(s2);
ret = tal_dup_arr_label(ctx, char, s1, len1, len2 + 1, label); ret = tal_dup_arr_label(ctx, char, s1, len1, len2 + 1, label);
@@ -151,13 +132,11 @@ char **tal_strsplit_(const tal_t *ctx,
tal_free(string); tal_free(string);
if (taken(delims)) if (taken(delims))
tal_free(delims); tal_free(delims);
return NULL; return parts;
} }
str = tal_strdup(parts, string); str = tal_strdup(parts, string);
if (unlikely(!str)) if (unlikely(!str))
goto fail; goto fail;
if (unlikely(!delims) && is_taken(delims))
goto fail;
if (flags == STR_NO_EMPTY) if (flags == STR_NO_EMPTY)
str += strspn(str, delims); str += strspn(str, delims);
@@ -185,10 +164,14 @@ char **tal_strsplit_(const tal_t *ctx,
return parts; return parts;
fail: fail:
#ifdef CCAN_TAL_NEVER_RETURN_NULL
abort();
#else
tal_free(parts); tal_free(parts);
if (taken(delims)) if (taken(delims))
tal_free(delims); tal_free(delims);
return NULL; return NULL;
#endif
} }
char *tal_strjoin_(const tal_t *ctx, char *tal_strjoin_(const tal_t *ctx,
@@ -199,12 +182,6 @@ char *tal_strjoin_(const tal_t *ctx,
char *ret = NULL; char *ret = NULL;
size_t totlen = 0, dlen; size_t totlen = 0, dlen;
if (unlikely(!strings) && is_taken(strings))
goto fail;
if (unlikely(!delim) && is_taken(delim))
goto fail;
dlen = strlen(delim); dlen = strlen(delim);
ret = tal_arr_label(ctx, char, dlen*2+1, label); ret = tal_arr_label(ctx, char, dlen*2+1, label);
if (!ret) if (!ret)
@@ -269,15 +246,9 @@ bool tal_strreg_(const tal_t *ctx, const char *string, const char *label,
unsigned int i; unsigned int i;
va_list ap; va_list ap;
if (unlikely(!regex) && is_taken(regex))
goto fail_no_re;
if (regcomp(&r, regex, REG_EXTENDED) != 0) if (regcomp(&r, regex, REG_EXTENDED) != 0)
goto fail_no_re; goto fail_no_re;
if (unlikely(!string) && is_taken(string))
goto fail;
if (regexec(&r, string, nmatch, matches, 0) != 0) if (regexec(&r, string, nmatch, matches, 0) != 0)
goto fail; goto fail;

View File

@@ -12,17 +12,18 @@
/** /**
* tal_strdup - duplicate a string * tal_strdup - duplicate a string
* @ctx: NULL, or tal allocated object to be parent. * @ctx: NULL, or tal allocated object to be parent.
* @p: the string to copy (can be take()). * @p: the string to copy (can be take(), must not be NULL).
* *
* The returned string will have tal_count() == strlen() + 1. * The returned string will have tal_count() == strlen() + 1.
*/ */
#define tal_strdup(ctx, p) tal_strdup_(ctx, p, TAL_LABEL(char, "[]")) #define tal_strdup(ctx, p) tal_strdup_(ctx, p, TAL_LABEL(char, "[]"))
char *tal_strdup_(const tal_t *ctx, const char *p TAKES, const char *label); char *tal_strdup_(const tal_t *ctx, const char *p TAKES, const char *label)
TAL_RETURN_PTR NON_NULL_ARGS(2);
/** /**
* tal_strndup - duplicate a limited amount of a string. * tal_strndup - duplicate a limited amount of a string.
* @ctx: NULL, or tal allocated object to be parent. * @ctx: NULL, or tal allocated object to be parent.
* @p: the string to copy (can be take()). * @p: the string to copy (can be take(), must not be NULL).
* @n: the maximum length to copy. * @n: the maximum length to copy.
* *
* Always gives a nul-terminated string, with strlen() <= @n. * Always gives a nul-terminated string, with strlen() <= @n.
@@ -30,24 +31,25 @@ char *tal_strdup_(const tal_t *ctx, const char *p TAKES, const char *label);
*/ */
#define tal_strndup(ctx, p, n) tal_strndup_(ctx, p, n, TAL_LABEL(char, "[]")) #define tal_strndup(ctx, p, n) tal_strndup_(ctx, p, n, TAL_LABEL(char, "[]"))
char *tal_strndup_(const tal_t *ctx, const char *p TAKES, size_t n, char *tal_strndup_(const tal_t *ctx, const char *p TAKES, size_t n,
const char *label); const char *label)
TAL_RETURN_PTR NON_NULL_ARGS(2);
/** /**
* tal_fmt - allocate a formatted string * tal_fmt - allocate a formatted string
* @ctx: NULL, or tal allocated object to be parent. * @ctx: NULL, or tal allocated object to be parent.
* @fmt: the printf-style format (can be take()). * @fmt: the printf-style format (can be take(), must not be NULL).
* *
* The returned string will have tal_count() == strlen() + 1. * The returned string will have tal_count() == strlen() + 1.
*/ */
#define tal_fmt(ctx, ...) \ #define tal_fmt(ctx, ...) \
tal_fmt_(ctx, TAL_LABEL(char, "[]"), __VA_ARGS__) tal_fmt_(ctx, TAL_LABEL(char, "[]"), __VA_ARGS__)
char *tal_fmt_(const tal_t *ctx, const char *label, const char *fmt TAKES, char *tal_fmt_(const tal_t *ctx, const char *label, const char *fmt TAKES,
...) PRINTF_FMT(3,4); ...) PRINTF_FMT(3,4) TAL_RETURN_PTR NON_NULL_ARGS(3);
/** /**
* tal_vfmt - allocate a formatted string (va_list version) * tal_vfmt - allocate a formatted string (va_list version)
* @ctx: NULL, or tal allocated object to be parent. * @ctx: NULL, or tal allocated object to be parent.
* @fmt: the printf-style format (can be take()). * @fmt: the printf-style format (can be take(), must not be NULL).
* @va: the va_list containing the format args. * @va: the va_list containing the format args.
* *
* The returned string will have tal_count() == strlen() + 1. * The returned string will have tal_count() == strlen() + 1.
@@ -56,40 +58,42 @@ char *tal_fmt_(const tal_t *ctx, const char *label, const char *fmt TAKES,
tal_vfmt_(ctx, fmt, va, TAL_LABEL(char, "[]")) tal_vfmt_(ctx, fmt, va, TAL_LABEL(char, "[]"))
char *tal_vfmt_(const tal_t *ctx, const char *fmt TAKES, va_list ap, char *tal_vfmt_(const tal_t *ctx, const char *fmt TAKES, va_list ap,
const char *label) const char *label)
PRINTF_FMT(2,0); PRINTF_FMT(2,0) TAL_RETURN_PTR NON_NULL_ARGS(2);
/** /**
* tal_append_fmt - append a formatted string to a talloc string. * tal_append_fmt - append a formatted string to a talloc string.
* @baseptr: a pointer to the tal string to be appended to. * @baseptr: a pointer to the tal string to be appended to.
* @fmt: the printf-style format (can be take()). * @fmt: the printf-style format (can be take(), must not be NULL).
* *
* Returns false on allocation failure. * Returns false on allocation failure.
* Otherwise tal_count(*@baseptr) == strlen(*@baseptr) + 1. * Otherwise tal_count(*@baseptr) == strlen(*@baseptr) + 1.
*/ */
bool tal_append_fmt(char **baseptr, const char *fmt TAKES, ...) PRINTF_FMT(2,3); bool tal_append_fmt(char **baseptr, const char *fmt TAKES, ...)
PRINTF_FMT(2,3) NON_NULL_ARGS(2);
/** /**
* tal_append_vfmt - append a formatted string to a talloc string (va_list) * tal_append_vfmt - append a formatted string to a talloc string (va_list)
* @baseptr: a pointer to the tal string to be appended to. * @baseptr: a pointer to the tal string to be appended to.
* @fmt: the printf-style format (can be take()). * @fmt: the printf-style format (can be take(), must not be NULL).
* @va: the va_list containing the format args. * @va: the va_list containing the format args.
* *
* Returns false on allocation failure. * Returns false on allocation failure.
* Otherwise tal_count(*@baseptr) == strlen(*@baseptr) + 1. * Otherwise tal_count(*@baseptr) == strlen(*@baseptr) + 1.
*/ */
bool tal_append_vfmt(char **baseptr, const char *fmt TAKES, va_list ap); bool tal_append_vfmt(char **baseptr, const char *fmt TAKES, va_list ap)
NON_NULL_ARGS(2);
/** /**
* tal_strcat - join two strings together * tal_strcat - join two strings together
* @ctx: NULL, or tal allocated object to be parent. * @ctx: NULL, or tal allocated object to be parent.
* @s1: the first string (can be take()). * @s1: the first string (can be take(), must not be NULL).
* @s2: the second string (can be take()). * @s2: the second string (can be take(), must not be NULL).
* *
* The returned string will have tal_count() == strlen() + 1. * The returned string will have tal_count() == strlen() + 1.
*/ */
#define tal_strcat(ctx, s1, s2) tal_strcat_(ctx, s1, s2, TAL_LABEL(char, "[]")) #define tal_strcat(ctx, s1, s2) tal_strcat_(ctx, s1, s2, TAL_LABEL(char, "[]"))
char *tal_strcat_(const tal_t *ctx, const char *s1 TAKES, const char *s2 TAKES, char *tal_strcat_(const tal_t *ctx, const char *s1 TAKES, const char *s2 TAKES,
const char *label); const char *label) TAL_RETURN_PTR NON_NULL_ARGS(2,3);
enum strsplit { enum strsplit {
STR_EMPTY_OK, STR_EMPTY_OK,
@@ -99,8 +103,8 @@ enum strsplit {
/** /**
* tal_strsplit - Split string into an array of substrings * tal_strsplit - Split string into an array of substrings
* @ctx: the context to tal from (often NULL). * @ctx: the context to tal from (often NULL).
* @string: the string to split (can be take()). * @string: the string to split (can be take(), must not be NULL).
* @delims: delimiters where lines should be split (can be take()). * @delims: delimiters where lines should be split (can be take(), must not be NULL).
* @flags: whether to include empty substrings. * @flags: whether to include empty substrings.
* *
* This function splits a single string into multiple strings. * This function splits a single string into multiple strings.
@@ -137,7 +141,8 @@ char **tal_strsplit_(const tal_t *ctx,
const char *string TAKES, const char *string TAKES,
const char *delims TAKES, const char *delims TAKES,
enum strsplit flag, enum strsplit flag,
const char *label); const char *label)
TAL_RETURN_PTR NON_NULL_ARGS(2,3);
enum strjoin { enum strjoin {
STR_TRAIL, STR_TRAIL,
@@ -147,8 +152,8 @@ enum strjoin {
/** /**
* tal_strjoin - Join an array of substrings into one long string * tal_strjoin - Join an array of substrings into one long string
* @ctx: the context to tal from (often NULL). * @ctx: the context to tal from (often NULL).
* @strings: the NULL-terminated array of strings to join (can be take()) * @strings: the NULL-terminated array of strings to join (can be take(), must not be NULL)
* @delim: the delimiter to insert between the strings (can be take()) * @delim: the delimiter to insert between the strings (can be take(), must not be NULL)
* @flags: whether to add a delimieter to the end * @flags: whether to add a delimieter to the end
* *
* This function joins an array of strings into a single string. The * This function joins an array of strings into a single string. The
@@ -175,13 +180,14 @@ char *tal_strjoin_(const void *ctx,
char *strings[] TAKES, char *strings[] TAKES,
const char *delim TAKES, const char *delim TAKES,
enum strjoin flags, enum strjoin flags,
const char *label); const char *label)
TAL_RETURN_PTR NON_NULL_ARGS(2,3);
/** /**
* tal_strreg - match/extract from a string via (extended) regular expressions. * tal_strreg - match/extract from a string via (extended) regular expressions.
* @ctx: the context to tal from (often NULL) * @ctx: the context to tal from (often NULL)
* @string: the string to try to match (can be take()) * @string: the string to try to match (can be take(), must not be NULL)
* @regex: the regular expression to match (can be take()) * @regex: the regular expression to match (can be take(), must not be NULL)
* ...: pointers to strings to allocate for subexpressions. * ...: pointers to strings to allocate for subexpressions.
* *
* Returns true if we matched, in which case any parenthesized * Returns true if we matched, in which case any parenthesized
@@ -221,5 +227,6 @@ char *tal_strjoin_(const void *ctx,
#define tal_strreg(ctx, string, ...) \ #define tal_strreg(ctx, string, ...) \
tal_strreg_(ctx, string, TAL_LABEL(char, "[]"), __VA_ARGS__) tal_strreg_(ctx, string, TAL_LABEL(char, "[]"), __VA_ARGS__)
bool tal_strreg_(const void *ctx, const char *string TAKES, bool tal_strreg_(const void *ctx, const char *string TAKES,
const char *label, const char *regex, ...); const char *label, const char *regex TAKES, ...)
NON_NULL_ARGS(2,4);
#endif /* CCAN_STR_TAL_H */ #endif /* CCAN_STR_TAL_H */

View File

@@ -456,13 +456,24 @@ static void del_tree(struct tal_hdr *t, const tal_t *orig, int saved_errno)
freefn(t); freefn(t);
} }
/* Don't have compiler complain we're returning NULL if we promised not to! */
static void *null_alloc_failed(void)
{
#ifdef CCAN_TAL_NEVER_RETURN_NULL
abort();
#else
return NULL;
#endif /* CCAN_TAL_NEVER_RETURN_NULL */
}
void *tal_alloc_(const tal_t *ctx, size_t size, bool clear, const char *label) void *tal_alloc_(const tal_t *ctx, size_t size, bool clear, const char *label)
{ {
struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx)); struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx));
child = allocate(sizeof(struct tal_hdr) + size); child = allocate(sizeof(struct tal_hdr) + size);
if (!child) if (!child)
return NULL; return null_alloc_failed();
if (clear) if (clear)
memset(from_tal_hdr(child), 0, size); memset(from_tal_hdr(child), 0, size);
child->prop = (void *)label; child->prop = (void *)label;
@@ -470,7 +481,7 @@ void *tal_alloc_(const tal_t *ctx, size_t size, bool clear, const char *label)
if (!add_child(parent, child)) { if (!add_child(parent, child)) {
freefn(child); freefn(child);
return NULL; return null_alloc_failed();
} }
debug_tal(parent); debug_tal(parent);
if (notifiers) if (notifiers)
@@ -501,7 +512,7 @@ void *tal_alloc_arr_(const tal_t *ctx, size_t size, size_t count, bool clear,
const char *label) const char *label)
{ {
if (!adjust_size(&size, count)) if (!adjust_size(&size, count))
return NULL; return null_alloc_failed();
return tal_alloc_(ctx, size, clear, label); return tal_alloc_(ctx, size, clear, label);
} }

View File

@@ -11,6 +11,14 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdarg.h> #include <stdarg.h>
/* Define this for better optimization if you never override errfn
* to something tat returns */
#ifdef CCAN_TAL_NEVER_RETURN_NULL
#define TAL_RETURN_PTR RETURNS_NONNULL
#else
#define TAL_RETURN_PTR
#endif /* CCAN_TAL_NEVER_RETURN_NULL */
/** /**
* tal_t - convenient alias for void to mark tal pointers. * tal_t - convenient alias for void to mark tal pointers.
* *
@@ -417,7 +425,8 @@ tal_t *tal_parent(const tal_t *ctx);
* @error_fn: called on errors or NULL (default is abort) * @error_fn: called on errors or NULL (default is abort)
* *
* The defaults are set up so tal functions never return NULL, but you * The defaults are set up so tal functions never return NULL, but you
* can override erorr_fn to change that. error_fn can return, and is * can override error_fn to change that. error_fn can return (only if
* you haven't defined CCAN_TAL_NEVER_RETURN_NULL!), and is
* called if alloc_fn or resize_fn fail. * called if alloc_fn or resize_fn fail.
* *
* If any parameter is NULL, that function is unchanged. * If any parameter is NULL, that function is unchanged.
@@ -521,9 +530,11 @@ bool tal_set_name_(tal_t *ctx, const char *name, bool literal);
#define tal_typechk_(ptr, ptype) (ptr) #define tal_typechk_(ptr, ptype) (ptr)
#endif #endif
void *tal_alloc_(const tal_t *ctx, size_t bytes, bool clear, const char *label); void *tal_alloc_(const tal_t *ctx, size_t bytes, bool clear, const char *label)
TAL_RETURN_PTR;
void *tal_alloc_arr_(const tal_t *ctx, size_t bytes, size_t count, bool clear, void *tal_alloc_arr_(const tal_t *ctx, size_t bytes, size_t count, bool clear,
const char *label); const char *label)
TAL_RETURN_PTR;
void *tal_dup_(const tal_t *ctx, const void *p TAKES, size_t size, void *tal_dup_(const tal_t *ctx, const void *p TAKES, size_t size,
size_t n, size_t extra, bool nullok, const char *label); size_t n, size_t extra, bool nullok, const char *label);