Copied name for certain functions can be 32 bytes, but destination is also only 32 bytes. After adding prefix, name may be truncated, causing compiler warning from -Wformat-truncation.
drivers/staging/greybus/audio_topology.c: In function ‘gbaudio_tplg_process_kcontrols’: drivers/staging/greybus/audio_topology.c:1172:65: warning: ‘%s’ directive output may be truncated writing up to 31 bytes into a region of size between 17 and 27 [-Wformat-truncation=] drivers/staging/greybus/audio_topology.c: In function ‘gbaudio_tplg_create_widget’: drivers/staging/greybus/audio_topology.c:1090:51: warning: ‘%s’ directive output may be truncated writing up to 31 bytes into a region of size between 17 and 27 [-Wformat-truncation=]
Modify the temporary name buffer size in gbaudio_tplg_process_kcontrols() and gbaudio_tplg_create_widget() to match the maximum size that can be copied back to the name field. Add a bitmask to the ID put in the prefix to assure values 0-63 as stated in "include/linux/greybus/greybus_protocols.h" for struct gb_audio_control. The compiler expects 2 digits max when bitmask is presnet, allowing one more character in controls compared to widgets.
For the widgets, name may only be 24 bytes max for IDs 0-255. For the controls, name may only be 25 bytes max for IDs 0-63. IDs with less digits get truncated regardless.
Compile tested only.
Signed-off-by: Garrett Floyd garrettfloyd345@hotmail.com --- drivers/staging/greybus/audio_topology.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/drivers/staging/greybus/audio_topology.c b/drivers/staging/greybus/audio_topology.c index 76146f91cddc..1a6ef1bdf170 100644 --- a/drivers/staging/greybus/audio_topology.c +++ b/drivers/staging/greybus/audio_topology.c @@ -1018,7 +1018,8 @@ static int gbaudio_tplg_create_widget(struct gbaudio_module_info *module, struct gb_audio_control *curr; struct gbaudio_control *control, *_control; size_t size; - char temp_name[NAME_SIZE]; + /* Subtracting 3 for "GB ", 3 for ID 0-255, 1 for final space */ + char temp_name[NAME_SIZE - 7];
ret = gbaudio_validate_kcontrol_count(w); if (ret) { @@ -1087,7 +1088,8 @@ static int gbaudio_tplg_create_widget(struct gbaudio_module_info *module,
/* Prefix dev_id to widget control_name */ strscpy(temp_name, w->name, sizeof(temp_name)); - snprintf(w->name, sizeof(w->name), "GB %d %s", module->dev_id, temp_name); + snprintf(w->name, sizeof(w->name), "GB %u %s", module->dev_id, + temp_name);
switch (w->type) { case snd_soc_dapm_spk: @@ -1143,7 +1145,8 @@ static int gbaudio_tplg_process_kcontrols(struct gbaudio_module_info *module, struct gb_audio_control *curr; struct gbaudio_control *control, *_control; size_t size; - char temp_name[NAME_SIZE]; + /* Subtracting 3 for "GB ", 2 for ID 0-63, 1 for final space */ + char temp_name[NAME_SIZE - 6];
size = sizeof(struct snd_kcontrol_new) * module->num_controls; dapm_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL); @@ -1169,7 +1172,8 @@ static int gbaudio_tplg_process_kcontrols(struct gbaudio_module_info *module, control->id = curr->id; /* Prefix dev_id to widget_name */ strscpy(temp_name, curr->name, sizeof(temp_name)); - snprintf(curr->name, sizeof(curr->name), "GB %d %s", module->dev_id, + /* bitmask enforces values between 0-63 */ + snprintf(curr->name, sizeof(curr->name), "GB %u %s", module->dev_id & 0x3f, temp_name); control->name = curr->name; if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) {
On Tue, Nov 17, 2026 at 08:44:33PM -0700, Garrett Floyd wrote:
Copied name for certain functions can be 32 bytes, but destination is also only 32 bytes. After adding prefix, name may be truncated, causing compiler warning from -Wformat-truncation.
drivers/staging/greybus/audio_topology.c: In function ‘gbaudio_tplg_process_kcontrols’: drivers/staging/greybus/audio_topology.c:1172:65: warning: ‘%s’ directive output may be truncated writing up to 31 bytes into a region of size between 17 and 27 [-Wformat-truncation=] drivers/staging/greybus/audio_topology.c: In function ‘gbaudio_tplg_create_widget’: drivers/staging/greybus/audio_topology.c:1090:51: warning: ‘%s’ directive output may be truncated writing up to 31 bytes into a region of size between 17 and 27 [-Wformat-truncation=]
Just ignore this warning. It's stupid.
We're using snprintf() as a belt and suspenders approach to preventing buffer overflows. If it gets truncated that's fine. Great even.
The fix is to disable stupid warnings from W=1.
regards, dan carpenter