From 1d2bdde5de13a5c553648165a345a798f0343415 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Mon, 16 Dec 2024 15:58:42 +0100 Subject: [PATCH] fixup! fixup! sys/string_utils: add string_writer helper --- sys/include/string_utils.h | 13 +++++++++++-- sys/libc/string.c | 16 +++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/sys/include/string_utils.h b/sys/include/string_utils.h index c2c140e6ace7..f3b79b9a5b3e 100644 --- a/sys/include/string_utils.h +++ b/sys/include/string_utils.h @@ -87,6 +87,12 @@ static inline const char *string_writer_str(const string_writer_t *sw) return sw->start; } +#if IS_ACTIVE(HAS_FLASH_UTILS_ARCH) +#define __swprintf flash_swprintf +#else +#define __swprintf swprintf +#endif + /** * @brief Write a formatted string to a buffer * The string will be truncated if there is not enough space left in @@ -99,8 +105,11 @@ static inline const char *string_writer_str(const string_writer_t *sw) * @return number of bytes written on success * -E2BIG if the string was truncated */ -__attribute__ ((format (printf, 2, 3))) -int swprintf(string_writer_t *sw, FLASH_ATTR const char *restrict format, ...); +int __swprintf(string_writer_t *sw, FLASH_ATTR const char *restrict format, ...); + +#if IS_ACTIVE(HAS_FLASH_UTILS_ARCH) +#define swprintf(sw, fmt, ...) flash_swprintf(sw, TO_FLASH(fmt), ## __VA_ARGS__) +#endif /* explicit_bzero is provided if: * - glibc is used as C lib (only with board natvie) diff --git a/sys/libc/string.c b/sys/libc/string.c index 5d071e1c57c4..1ab18f461c05 100644 --- a/sys/libc/string.c +++ b/sys/libc/string.c @@ -52,18 +52,20 @@ const void *memchk(const void *data, uint8_t c, size_t len) return NULL; } -int swprintf(string_writer_t *sw, FLASH_ATTR const char *restrict format, ...) +int __swprintf(string_writer_t *sw, FLASH_ATTR const char *restrict format, ...) { va_list args; int res; - /* make sure flash_vsnprintf() is not used */ - #if HAS_FLASH_UTILS_ARCH - #undef vsnprintf - #endif - va_start(args, format); - res = vsnprintf(sw->position, sw->capacity, format, args); +#ifdef __clang__ + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wformat-nonliteral\"") \ + res = flash_vsnprintf(sw->position, sw->capacity, format, args); + _Pragma("clang diagnostic pop") +#else + res = flash_vsnprintf(sw->position, sw->capacity, format, args); +#endif va_end(args); if (res < (int)sw->capacity) {