From 52091a1af02fa7151c2bb6b542e51068ce20c525 Mon Sep 17 00:00:00 2001 From: Denis Kenzior Date: Tue, 8 Sep 2020 11:19:54 -0500 Subject: [PATCH] qmimodem: Fix format warning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ../../drivers/qmimodem/network-registration.c: In function ‘extract_ss_info’: ../../drivers/qmimodem/network-registration.c:131:54: warning: ‘%03d’ directive output may be truncated writing between 3 and 5 bytes into a region of size 4 [-Wformat-truncation=] 131 | snprintf(operator->mcc, OFONO_MAX_MCC_LENGTH + 1, "%03d", | ^~~~ ../../drivers/qmimodem/network-registration.c:131:53: note: directive argument in the range [0, 65535] 131 | snprintf(operator->mcc, OFONO_MAX_MCC_LENGTH + 1, "%03d", | ^~~~~~ The MCC/MNC fields are limited to three digits. Clamp the input to 999 to avoid the warning. --- drivers/qmimodem/network-registration.c | 30 ++++++++++++++++++------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/drivers/qmimodem/network-registration.c b/drivers/qmimodem/network-registration.c index 04f20c66..ecdc6054 100644 --- a/drivers/qmimodem/network-registration.c +++ b/drivers/qmimodem/network-registration.c @@ -128,10 +128,18 @@ static bool extract_ss_info(struct qmi_result *result, int *status, plmn = qmi_result_get(result, QMI_NAS_RESULT_CURRENT_PLMN, &len); if (plmn) { - snprintf(operator->mcc, OFONO_MAX_MCC_LENGTH + 1, "%03d", - GUINT16_FROM_LE(plmn->mcc)); - snprintf(operator->mnc, OFONO_MAX_MNC_LENGTH + 1, "%02d", - GUINT16_FROM_LE(plmn->mnc)); + uint16_t mcc = GUINT16_FROM_LE(plmn->mcc); + uint16_t mnc = GUINT16_FROM_LE(plmn->mnc); + + if (mcc > 999) + mcc = 999; + + if (mnc > 999) + mnc = 999; + + snprintf(operator->mcc, OFONO_MAX_MCC_LENGTH + 1, "%03d", mcc); + snprintf(operator->mnc, OFONO_MAX_MNC_LENGTH + 1, "%03d", mnc); + opname_len = plmn->desc_len; if (opname_len > OFONO_MAX_OPERATOR_NAME_LENGTH) opname_len = OFONO_MAX_OPERATOR_NAME_LENGTH; @@ -311,11 +319,17 @@ static void scan_nets_cb(struct qmi_result *result, void *user_data) for (i = 0; i < num; i++) { const struct qmi_nas_network_info *netinfo = ptr + offset; + uint16_t mcc = GUINT16_FROM_LE(netinfo->mcc); + uint16_t mnc = GUINT16_FROM_LE(netinfo->mnc); - snprintf(list[i].mcc, OFONO_MAX_MCC_LENGTH + 1, "%03d", - GUINT16_FROM_LE(netinfo->mcc)); - snprintf(list[i].mnc, OFONO_MAX_MNC_LENGTH + 1, "%02d", - GUINT16_FROM_LE(netinfo->mnc)); + if (mcc > 999) + mcc = 999; + + if (mnc > 999) + mnc = 999; + + snprintf(list[i].mcc, OFONO_MAX_MCC_LENGTH + 1, "%03d", mcc); + snprintf(list[i].mnc, OFONO_MAX_MNC_LENGTH + 1, "%03d", mnc); strncpy(list[i].name, netinfo->desc, netinfo->desc_len); list[i].name[netinfo->desc_len] = '\0';