From 02047e9c8858af7e5fc9f2684d272e65e264c078 Mon Sep 17 00:00:00 2001 From: Jouni Malinen Date: Fri, 27 May 2022 00:23:25 +0300 Subject: [PATCH] hs20-osu-client: Explicit checks for snprintf() result Get rid of the gcc warnings on potentially truncating the output buffer by explicitly checking that there was enough room to write the full string. Signed-off-by: Jouni Malinen --- hs20/client/osu_client.c | 78 +++++++++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 21 deletions(-) diff --git a/hs20/client/osu_client.c b/hs20/client/osu_client.c index 7b274dac9..01e7b7553 100644 --- a/hs20/client/osu_client.c +++ b/hs20/client/osu_client.c @@ -2018,6 +2018,7 @@ static struct osu_data * parse_osu_providers(const char *fname, size_t *count) struct osu_data *osu = NULL, *last = NULL; size_t osu_count = 0; char *pos, *end; + int res; f = fopen(fname, "r"); if (f == NULL) { @@ -2037,15 +2038,20 @@ static struct osu_data * parse_osu_providers(const char *fname, size_t *count) osu = last; last = &osu[osu_count++]; memset(last, 0, sizeof(*last)); - snprintf(last->bssid, sizeof(last->bssid), "%s", - buf + 13); + res = os_snprintf(last->bssid, sizeof(last->bssid), + "%s", buf + 13); + if (os_snprintf_error(sizeof(last->bssid), res)) + break; continue; } if (!last) continue; if (strncmp(buf, "uri=", 4) == 0) { - snprintf(last->url, sizeof(last->url), "%s", buf + 4); + res = os_snprintf(last->url, sizeof(last->url), + "%s", buf + 4); + if (os_snprintf_error(sizeof(last->url), res)) + break; continue; } @@ -2055,26 +2061,37 @@ static struct osu_data * parse_osu_providers(const char *fname, size_t *count) } if (strncmp(buf, "osu_ssid=", 9) == 0) { - snprintf(last->osu_ssid, sizeof(last->osu_ssid), - "%s", buf + 9); + res = os_snprintf(last->osu_ssid, + sizeof(last->osu_ssid), + "%s", buf + 9); + if (os_snprintf_error(sizeof(last->osu_ssid), res)) + break; continue; } if (strncmp(buf, "osu_ssid2=", 10) == 0) { - snprintf(last->osu_ssid2, sizeof(last->osu_ssid2), - "%s", buf + 10); + res = os_snprintf(last->osu_ssid2, + sizeof(last->osu_ssid2), + "%s", buf + 10); + if (os_snprintf_error(sizeof(last->osu_ssid2), res)) + break; continue; } if (os_strncmp(buf, "osu_nai=", 8) == 0) { - os_snprintf(last->osu_nai, sizeof(last->osu_nai), - "%s", buf + 8); + res = os_snprintf(last->osu_nai, sizeof(last->osu_nai), + "%s", buf + 8); + if (os_snprintf_error(sizeof(last->osu_nai), res)) + break; continue; } if (os_strncmp(buf, "osu_nai2=", 9) == 0) { - os_snprintf(last->osu_nai2, sizeof(last->osu_nai2), - "%s", buf + 9); + res = os_snprintf(last->osu_nai2, + sizeof(last->osu_nai2), + "%s", buf + 9); + if (os_snprintf_error(sizeof(last->osu_nai2), res)) + break; continue; } @@ -2087,8 +2104,14 @@ static struct osu_data * parse_osu_providers(const char *fname, size_t *count) continue; *pos++ = '\0'; txt = &last->friendly_name[last->friendly_name_count++]; - snprintf(txt->lang, sizeof(txt->lang), "%s", buf + 14); - snprintf(txt->text, sizeof(txt->text), "%s", pos); + res = os_snprintf(txt->lang, sizeof(txt->lang), + "%s", buf + 14); + if (os_snprintf_error(sizeof(txt->lang), res)) + break; + res = os_snprintf(txt->text, sizeof(txt->text), + "%s", pos); + if (os_snprintf_error(sizeof(txt->text), res)) + break; } if (strncmp(buf, "desc=", 5) == 0) { @@ -2100,8 +2123,14 @@ static struct osu_data * parse_osu_providers(const char *fname, size_t *count) continue; *pos++ = '\0'; txt = &last->serv_desc[last->serv_desc_count++]; - snprintf(txt->lang, sizeof(txt->lang), "%s", buf + 5); - snprintf(txt->text, sizeof(txt->text), "%s", pos); + res = os_snprintf(txt->lang, sizeof(txt->lang), + "%s", buf + 5); + if (os_snprintf_error(sizeof(txt->lang), res)) + break; + res = os_snprintf(txt->text, sizeof(txt->text), + "%s", pos); + if (os_snprintf_error(sizeof(txt->text), res)) + break; } if (strncmp(buf, "icon=", 5) == 0) { @@ -2124,23 +2153,30 @@ static struct osu_data * parse_osu_providers(const char *fname, size_t *count) if (!end) continue; *end = '\0'; - snprintf(icon->lang, sizeof(icon->lang), "%s", pos); + res = os_snprintf(icon->lang, sizeof(icon->lang), + "%s", pos); + if (os_snprintf_error(sizeof(icon->lang), res)) + break; pos = end + 1; end = strchr(pos, ':'); if (end) *end = '\0'; - snprintf(icon->mime_type, sizeof(icon->mime_type), - "%s", pos); - if (!pos) + res = os_snprintf(icon->mime_type, + sizeof(icon->mime_type), "%s", pos); + if (os_snprintf_error(sizeof(icon->mime_type), res)) + break; + if (!end) continue; pos = end + 1; end = strchr(pos, ':'); if (end) *end = '\0'; - snprintf(icon->filename, sizeof(icon->filename), - "%s", pos); + res = os_snprintf(icon->filename, + sizeof(icon->filename), "%s", pos); + if (os_snprintf_error(sizeof(icon->filename), res)) + break; continue; } }