upnpc.c: prevent warning with gcc 10 and 11

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
This commit is contained in:
Thomas Bernard 2023-01-26 23:44:26 +01:00
parent 98cc9f1b43
commit b10c247fb1
No known key found for this signature in database
GPG Key ID: DB511043A31ACAAF
1 changed files with 5 additions and 6 deletions

View File

@ -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,