From 01bc8f9091e2bc2b285b568cf1f9d1347a5ec8de Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 22 Mar 2024 09:48:10 +1030 Subject: [PATCH] tal, tal/str: allow annotation with returns_nonnull if they opt in. This is true if you don't override the error callback, and helps with optimization (and warnings!). Signed-off-by: Rusty Russell --- ccan/tal/str/str.h | 18 +++++++++++------- ccan/tal/tal.h | 17 ++++++++++++++--- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/ccan/tal/str/str.h b/ccan/tal/str/str.h index f9394844..a2424cbe 100644 --- a/ccan/tal/str/str.h +++ b/ccan/tal/str/str.h @@ -17,7 +17,8 @@ * The returned string will have tal_count() == strlen() + 1. */ #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; /** * tal_strndup - duplicate a limited amount of a string. @@ -30,7 +31,8 @@ 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, "[]")) char *tal_strndup_(const tal_t *ctx, const char *p TAKES, size_t n, - const char *label); + const char *label) + TAL_RETURN_PTR; /** * tal_fmt - allocate a formatted string @@ -42,7 +44,7 @@ char *tal_strndup_(const tal_t *ctx, const char *p TAKES, size_t n, #define tal_fmt(ctx, ...) \ tal_fmt_(ctx, TAL_LABEL(char, "[]"), __VA_ARGS__) 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; /** * tal_vfmt - allocate a formatted string (va_list version) @@ -56,7 +58,7 @@ char *tal_fmt_(const tal_t *ctx, const char *label, const char *fmt TAKES, tal_vfmt_(ctx, fmt, va, TAL_LABEL(char, "[]")) char *tal_vfmt_(const tal_t *ctx, const char *fmt TAKES, va_list ap, const char *label) - PRINTF_FMT(2,0); + PRINTF_FMT(2,0) TAL_RETURN_PTR; /** * tal_append_fmt - append a formatted string to a talloc string. @@ -89,7 +91,7 @@ bool tal_append_vfmt(char **baseptr, const char *fmt TAKES, va_list ap); */ #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, - const char *label); + const char *label) TAL_RETURN_PTR; enum strsplit { STR_EMPTY_OK, @@ -137,7 +139,8 @@ char **tal_strsplit_(const tal_t *ctx, const char *string TAKES, const char *delims TAKES, enum strsplit flag, - const char *label); + const char *label) + TAL_RETURN_PTR; enum strjoin { STR_TRAIL, @@ -175,7 +178,8 @@ char *tal_strjoin_(const void *ctx, char *strings[] TAKES, const char *delim TAKES, enum strjoin flags, - const char *label); + const char *label) + TAL_RETURN_PTR; /** * tal_strreg - match/extract from a string via (extended) regular expressions. diff --git a/ccan/tal/tal.h b/ccan/tal/tal.h index c486f9e8..347a5e8c 100644 --- a/ccan/tal/tal.h +++ b/ccan/tal/tal.h @@ -11,6 +11,14 @@ #include #include +/* 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. * @@ -417,7 +425,8 @@ tal_t *tal_parent(const tal_t *ctx); * @error_fn: called on errors or NULL (default is abort) * * 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. * * 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) #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, - const char *label); + const char *label) + TAL_RETURN_PTR; 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); -- 2.39.5