Check the snprintf() return value in order to avoid the following
warnings from gcc:
config.c: In function ‘config_create’:
config.c:921:52: warning: ‘%s’ directive output may be truncated writing up to
9679 bytes into a region of size 33 [-Wformat-truncation=]
921 | snprintf(buf, sizeof(buf), "global.%s", ci->label);
| ^~
config.c:921:17: note: ‘snprintf’ output between 8 and 9687 bytes into a
destination of size 40
921 | snprintf(buf, sizeof(buf), "global.%s", ci->label);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
config.c:364:40: warning: ‘%s’ directive output may be truncated writing up to
9679 bytes into a region of size 133 -Wformat-truncation=]
364 | snprintf(buf, sizeof(buf), "%s.%s", section, name);
| ^~
In function ‘config_section_item’,
inlined from ‘config_global_item’ at config.c:371:9,
inlined from ‘config_create’ at config.c:931:8:
config.c:364:9: note: ‘snprintf’ output between 8 and 9687 bytes into a
destination of size 140
364 | snprintf(buf, sizeof(buf), "%s.%s", section, name);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Miroslav Lichvar <[email protected]>
---
config.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/config.c b/config.c
index e454c91..e21cde7 100644
--- a/config.c
+++ b/config.c
@@ -361,7 +361,8 @@ static struct config_item *config_section_item(struct
config *cfg,
{
char buf[CONFIG_LABEL_SIZE + MAX_IFNAME_SIZE];
- snprintf(buf, sizeof(buf), "%s.%s", section, name);
+ if (snprintf(buf, sizeof(buf), "%s.%s", section, name) >= sizeof(buf))
+ return NULL;
return hash_lookup(cfg->htab, buf);
}
@@ -918,7 +919,11 @@ struct config *config_create(void)
for (i = 0; i < N_CONFIG_ITEMS; i++) {
ci = &config_tab[i];
ci->flags |= CFG_ITEM_STATIC;
- snprintf(buf, sizeof(buf), "global.%s", ci->label);
+ if (snprintf(buf, sizeof(buf), "global.%s", ci->label) >=
+ sizeof(buf)) {
+ fprintf(stderr, "option %s too long\n", ci->label);
+ goto fail;
+ }
if (hash_insert(cfg->htab, buf, ci)) {
fprintf(stderr, "duplicate item %s\n", ci->label);
goto fail;
--
2.37.3
_______________________________________________
Linuxptp-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel