modules: convert snprintf to strbuf

Use spa_strbuf instead of snprintf to handle errors better.
This commit is contained in:
Wim Taymans 2026-05-06 13:35:09 +02:00
parent 2c4dc2d22f
commit 51b635cc98
4 changed files with 25 additions and 16 deletions

View file

@ -8,6 +8,7 @@
#include <inttypes.h> #include <inttypes.h>
#include <spa/debug/context.h> #include <spa/debug/context.h>
#include <spa/utils/string.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { 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; const uint8_t *t = (const uint8_t*)data;
char buffer[512]; char buffer[512];
struct spa_strbuf b;
size_t i; size_t i;
int pos = 0;
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
if (i % 16 == 0) if (i % 16 == 0) {
pos = snprintf(buffer, sizeof(buffer), "%p: ", &t[i]); spa_strbuf_init(&b, buffer, sizeof(buffer));
pos += snprintf(buffer + pos, sizeof(buffer) - pos, "%02x ", t[i]); spa_strbuf_append(&b, "%p: ", &t[i]);
}
spa_strbuf_append(&b, "%02x ", t[i]);
if (i % 16 == 15 || i == size - 1) { if (i % 16 == 15 || i == size - 1) {
spa_debugc(ctx, "%*s" "%s", indent, "", buffer); spa_debugc(ctx, "%*s" "%s", indent, "", buffer);
} }

View file

@ -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); pw_log_warn("server %p: security label error: %m", s);
} else { } else {
if (!check_print(buffer, len)) { if (!check_print(buffer, len)) {
char *hex, *p; char *hex;
struct spa_strbuf b;
static const char *ch = "0123456789abcdef"; static const char *ch = "0123456789abcdef";
p = hex = alloca(len * 2 + 10); hex = alloca(len * 2 + 10);
p += snprintf(p, 5, "hex:"); spa_strbuf_init(&b, hex, len * 2 + 10);
spa_strbuf_append(&b, "hex:");
for(i = 0; i < (int)len; i++) 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]); ch[buffer[i] >> 4], ch[buffer[i] & 0xf]);
pw_properties_set(props, PW_KEY_SEC_LABEL, hex); pw_properties_set(props, PW_KEY_SEC_LABEL, hex);

View file

@ -288,13 +288,15 @@ void audioinfo_to_properties(struct spa_audio_info_raw *info, struct pw_properti
if (info->rate) if (info->rate)
pw_properties_setf(props, SPA_KEY_AUDIO_RATE, "%u", info->rate); pw_properties_setf(props, SPA_KEY_AUDIO_RATE, "%u", info->rate);
if (info->channels) { 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); 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++) 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))); channel_id2name(info->position[i], pos, sizeof(pos)));
pw_properties_setf(props, SPA_KEY_AUDIO_POSITION, "[ %s ]", s); pw_properties_setf(props, SPA_KEY_AUDIO_POSITION, "[ %s ]", s);
} }

View file

@ -184,18 +184,20 @@ static void pw_properties_from_zeroconf(const char *key, const char *value,
else if (spa_streq(key, "channel_map")) { else if (spa_streq(key, "channel_map")) {
struct channel_map channel_map; struct channel_map channel_map;
uint32_t i, pos[CHANNELS_MAX]; uint32_t i, pos[CHANNELS_MAX];
char *p, *s, buf[8]; char *s, buf[8];
spa_zero(channel_map); spa_zero(channel_map);
channel_map_parse(value, &channel_map); channel_map_parse(value, &channel_map);
channel_map_to_positions(&channel_map, pos, CHANNELS_MAX); channel_map_to_positions(&channel_map, pos, CHANNELS_MAX);
p = s = alloca(4 + channel_map.channels * 8); s = alloca(4 + channel_map.channels * 8);
p += spa_scnprintf(p, 2, "["); 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++) 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))); 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); pw_properties_set(props, SPA_KEY_AUDIO_POSITION, s);
} }
else if (spa_streq(key, "format")) { else if (spa_streq(key, "format")) {