From 51b635cc98a5638d22330904c43bd38ad0ae679a Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Wed, 6 May 2026 13:35:09 +0200 Subject: [PATCH] modules: convert snprintf to strbuf Use spa_strbuf instead of snprintf to handle errors better. --- spa/include/spa/debug/mem.h | 11 +++++++---- src/modules/module-protocol-native.c | 10 ++++++---- src/modules/module-protocol-pulse/module.c | 8 +++++--- src/modules/module-zeroconf-discover.c | 12 +++++++----- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/spa/include/spa/debug/mem.h b/spa/include/spa/debug/mem.h index 4285225c7..30276268c 100644 --- a/spa/include/spa/debug/mem.h +++ b/spa/include/spa/debug/mem.h @@ -8,6 +8,7 @@ #include #include +#include #ifdef __cplusplus extern "C" { @@ -30,13 +31,15 @@ SPA_API_DEBUG_MEM int spa_debugc_mem(struct spa_debug_context *ctx, int indent, { const uint8_t *t = (const uint8_t*)data; char buffer[512]; + struct spa_strbuf b; size_t i; - int pos = 0; for (i = 0; i < size; i++) { - if (i % 16 == 0) - pos = snprintf(buffer, sizeof(buffer), "%p: ", &t[i]); - pos += snprintf(buffer + pos, sizeof(buffer) - pos, "%02x ", t[i]); + if (i % 16 == 0) { + spa_strbuf_init(&b, buffer, sizeof(buffer)); + spa_strbuf_append(&b, "%p: ", &t[i]); + } + spa_strbuf_append(&b, "%02x ", t[i]); if (i % 16 == 15 || i == size - 1) { spa_debugc(ctx, "%*s" "%s", indent, "", buffer); } diff --git a/src/modules/module-protocol-native.c b/src/modules/module-protocol-native.c index 91acfc976..b2e8482c2 100644 --- a/src/modules/module-protocol-native.c +++ b/src/modules/module-protocol-native.c @@ -632,13 +632,15 @@ static struct client_data *client_new(struct server *s, int fd) pw_log_warn("server %p: security label error: %m", s); } else { if (!check_print(buffer, len)) { - char *hex, *p; + char *hex; + struct spa_strbuf b; static const char *ch = "0123456789abcdef"; - p = hex = alloca(len * 2 + 10); - p += snprintf(p, 5, "hex:"); + hex = alloca(len * 2 + 10); + spa_strbuf_init(&b, hex, len * 2 + 10); + spa_strbuf_append(&b, "hex:"); for(i = 0; i < (int)len; i++) - p += snprintf(p, 3, "%c%c", + spa_strbuf_append(&b, "%c%c", ch[buffer[i] >> 4], ch[buffer[i] & 0xf]); pw_properties_set(props, PW_KEY_SEC_LABEL, hex); diff --git a/src/modules/module-protocol-pulse/module.c b/src/modules/module-protocol-pulse/module.c index 37989985b..3b0cf0ea5 100644 --- a/src/modules/module-protocol-pulse/module.c +++ b/src/modules/module-protocol-pulse/module.c @@ -288,13 +288,15 @@ void audioinfo_to_properties(struct spa_audio_info_raw *info, struct pw_properti if (info->rate) pw_properties_setf(props, SPA_KEY_AUDIO_RATE, "%u", info->rate); if (info->channels) { - char *s, *p, pos[8]; + char *s, pos[8]; + struct spa_strbuf b; pw_properties_setf(props, SPA_KEY_AUDIO_CHANNELS, "%u", info->channels); - p = s = alloca(info->channels * 8); + s = alloca(info->channels * 8); + spa_strbuf_init(&b, s, info->channels * 8); for (i = 0; i < info->channels; i++) - p += spa_scnprintf(p, 8, "%s%s", i == 0 ? "" : ", ", + spa_strbuf_append(&b, "%s%s", i == 0 ? "" : ", ", channel_id2name(info->position[i], pos, sizeof(pos))); pw_properties_setf(props, SPA_KEY_AUDIO_POSITION, "[ %s ]", s); } diff --git a/src/modules/module-zeroconf-discover.c b/src/modules/module-zeroconf-discover.c index 92f426740..f6f19192a 100644 --- a/src/modules/module-zeroconf-discover.c +++ b/src/modules/module-zeroconf-discover.c @@ -184,18 +184,20 @@ static void pw_properties_from_zeroconf(const char *key, const char *value, else if (spa_streq(key, "channel_map")) { struct channel_map channel_map; uint32_t i, pos[CHANNELS_MAX]; - char *p, *s, buf[8]; + char *s, buf[8]; spa_zero(channel_map); channel_map_parse(value, &channel_map); channel_map_to_positions(&channel_map, pos, CHANNELS_MAX); - p = s = alloca(4 + channel_map.channels * 8); - p += spa_scnprintf(p, 2, "["); + s = alloca(4 + channel_map.channels * 8); + struct spa_strbuf b; + spa_strbuf_init(&b, s, 4 + channel_map.channels * 8); + spa_strbuf_append(&b, "["); for (i = 0; i < channel_map.channels; i++) - p += spa_scnprintf(p, 8, "%s%s", i == 0 ? "" : ",", + spa_strbuf_append(&b, "%s%s", i == 0 ? "" : ",", channel_id2name(pos[i], buf, sizeof(buf))); - p += spa_scnprintf(p, 2, "]"); + spa_strbuf_append(&b, "]"); pw_properties_set(props, SPA_KEY_AUDIO_POSITION, s); } else if (spa_streq(key, "format")) {