From: Gerhard Sittig Date: Tue, 16 Aug 2022 20:17:45 +0000 (+0200) Subject: device: don't accept empty names for sigrok channels X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=7b4d9cbd59f16fc2df1a76feeeb35b1e86e1260a;p=libsigrok.git device: don't accept empty names for sigrok channels It's unexpected when sigrok channels "have no name". Previous versions would not strictly enforce non-empty names and could let empty names slip in, but application code was reported to be confused when it happened. The benefit of having channels "with no names" is questioned. Adjust the public API routines. Accept missing name specs in the sr_channel_new() routine for maximum backwards compatibility. Reject missing names in sr_dev_channel_name_set() so that empty or non-empty names can only be replaced by non-empty names. Check the return code of sr_channel_new() in sr_dev_inst_channel_add() so that failed channel creation gets propagated to callers. The current implementation will never fail sr_channel_new(), but future versions might. None of libsigrok code (device drivers, input modules) assign empty names to channels. Applications still need to get checked. A future commit might make names in sr_channel_new() mandatory. --- diff --git a/src/device.c b/src/device.c index 7973d1e5..5c681378 100644 --- a/src/device.c +++ b/src/device.c @@ -67,7 +67,7 @@ SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi, ch->index = index; ch->type = type; ch->enabled = enabled; - if (name) + if (name && *name) ch->name = g_strdup(name); sdi->channels = g_slist_append(sdi->channels, ch); @@ -120,6 +120,8 @@ SR_API int sr_dev_channel_name_set(struct sr_channel *channel, { if (!channel) return SR_ERR_ARG; + if (!name || !*name) + return SR_ERR_ARG; g_free(channel->name); channel->name = g_strdup(name); @@ -489,7 +491,8 @@ SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, if (!sdi || sdi->inst_type != SR_INST_USER || index < 0) return SR_ERR_ARG; - sr_channel_new(sdi, index, type, TRUE, name); + if (!sr_channel_new(sdi, index, type, TRUE, name)) + return SR_ERR_DATA; return SR_OK; }