From 196b1d6289e34836404681400a4a6d91044d324b Mon Sep 17 00:00:00 2001 From: Fabio Baltieri Date: Thu, 14 Sep 2023 14:07:02 +0000 Subject: [PATCH] modem: hl7800: replace an snprintk with a strncpy GCC format-truncation kicks in on this printk whenever the logging configuration transforms the LOG_WRN below into a noop, and the snprintk return argument become unused. Fix that by switching to strncpy, fixes: zephyrproject/zephyr/drivers/modem/hl7800.c:1786:74: warning: '%s' directive output may be truncated writing up to 53 bytes into a region of size 21 [-Wformat-truncation=] reproduced with: west build -p -b mg100 -T samples/net/telnet/sample.net.telnet Signed-off-by: Fabio Baltieri --- drivers/modem/hl7800.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/drivers/modem/hl7800.c b/drivers/modem/hl7800.c index 9e59cf5e33b..291856a01ff 100644 --- a/drivers/modem/hl7800.c +++ b/drivers/modem/hl7800.c @@ -23,6 +23,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME, CONFIG_MODEM_LOG_LEVEL); #include #include +#include #include #include @@ -1762,9 +1763,9 @@ done: */ static bool on_cmd_atcmdinfo_iccid(struct net_buf **buf, uint16_t len) { - int ret; char value[MDM_CCID_RESP_MAX_SIZE]; char *delim; + int iccid_len; size_t out_len; out_len = net_buf_linearize(value, sizeof(value), *buf, 0, len); @@ -1783,9 +1784,13 @@ static bool on_cmd_atcmdinfo_iccid(struct net_buf **buf, uint16_t len) LOG_INF("EID: %s", delim + 1); } - ret = snprintk(iface_ctx.mdm_iccid, sizeof(iface_ctx.mdm_iccid), "%s", value); - if (ret > MDM_HL7800_ICCID_MAX_STRLEN) { - LOG_WRN("ICCID too long: %d", ret); + iccid_len = strlen(value); + strncpy(iface_ctx.mdm_iccid, value, sizeof(iface_ctx.mdm_iccid)); + len = MIN(iccid_len, sizeof(iface_ctx.mdm_iccid) - 1); + iface_ctx.mdm_iccid[len] = '\0'; + + if (iccid_len > len) { + LOG_WRN("ICCID too long: %d (max %d)", iccid_len, len); } LOG_INF("ICCID: %s", iface_ctx.mdm_iccid);