From b10c247fb1c81db1d7b09668ecd62cad6ca2e745 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Thu, 26 Jan 2023 23:44:26 +0100 Subject: [PATCH] upnpc.c: prevent warning with gcc 10 and 11 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit src/upnpc.c:153:37: warning: ā€˜%dā€™ directive output may be truncated writing between 1 and 10 bytes into a region of size 6 [-Wformat-truncation=] 153 | snprintf(index, 6, "%d", i); | ^~ src/upnpc.c:153:36: note: directive argument in the range [0, 2147483647] 153 | snprintf(index, 6, "%d", i); | ^~~~ Somehow the static analyser fails to recognize that 0 <= i < 65536 see #643 --- miniupnpc/src/upnpc.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/miniupnpc/src/upnpc.c b/miniupnpc/src/upnpc.c index 991e266..1a12cef 100644 --- a/miniupnpc/src/upnpc.c +++ b/miniupnpc/src/upnpc.c @@ -135,7 +135,7 @@ static void ListRedirections(struct UPNPUrls * urls, struct IGDdatas * data) { int r; - int i = 0; + unsigned short i = 0; char index[6]; char intClient[40]; char intPort[6]; @@ -150,7 +150,7 @@ static void ListRedirections(struct UPNPUrls * urls, printf("PortMappingNumberOfEntries : %u\n", num);*/ printf(" i protocol exPort->inAddr:inPort description remoteHost leaseTime\n"); do { - snprintf(index, 6, "%d", i); + snprintf(index, 6, "%hu", i); rHost[0] = '\0'; enabled[0] = '\0'; duration[0] = '\0'; desc[0] = '\0'; extPort[0] = '\0'; intPort[0] = '\0'; intClient[0] = '\0'; @@ -162,20 +162,19 @@ static void ListRedirections(struct UPNPUrls * urls, rHost, duration); if(r==0) /* - printf("%02d - %s %s->%s:%s\tenabled=%s leaseDuration=%s\n" + printf("%02hu - %s %s->%s:%s\tenabled=%s leaseDuration=%s\n" " desc='%s' rHost='%s'\n", i, protocol, extPort, intClient, intPort, enabled, duration, desc, rHost); */ - printf("%2d %s %5s->%s:%-5s '%s' '%s' %s\n", + printf("%2hu %s %5s->%s:%-5s '%s' '%s' %s\n", i, protocol, extPort, intClient, intPort, desc, rHost, duration); else printf("GetGenericPortMappingEntry() returned %d (%s)\n", r, strupnperror(r)); - i++; - } while(r == 0 && i < 65536); + } while(r == 0 && i++ < 65535); } static void NewListRedirections(struct UPNPUrls * urls,