Skip to content

Commit e2da164

Browse files
authored
examples,test: check that calls to 'sscanf' write to output vars
1 parent f9dabe8 commit e2da164

3 files changed

Lines changed: 11 additions & 5 deletions

File tree

examples/server/pnmshow.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ int main(int argc,char** argv)
5151
} while(buffer[0]=='#');
5252

5353
/* get width & height */
54-
sscanf(buffer,"%d %d",&width,&height);
54+
if(sscanf(buffer,"%d %d",&width,&height) != 2) {
55+
printf("Failed to get width or height.\n");
56+
exit(3);
57+
}
5558
rfbLog("Got width %d and height %d.\n",width,height);
5659
if(picType!=BW)
5760
fgets(buffer,1024,in);

examples/server/pnmshow24.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ int main(int argc,char** argv)
5050
} while(buffer[0]=='#');
5151

5252
/* get width & height */
53-
sscanf(buffer,"%d %d",&width,&height);
53+
if(sscanf(buffer,"%d %d",&width,&height) != 2) {
54+
printf("Failed to get width or height.\n");
55+
exit(3);
56+
}
5457
rfbLog("Got width %d and height %d.\n",width,height);
5558
fgets(buffer,1024,in);
5659

test/bmp.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ int loadppm(int *fd, unsigned char **buf, int *w, int *h,
122122
switch(totalread)
123123
{
124124
case 0:
125-
if((numread=sscanf(temps, "%d %d %d", w, h, &scalefactor))==EOF)
125+
if((numread=sscanf(temps, "%d %d %d", w, h, &scalefactor))!=3)
126126
_throw("Read error");
127127
break;
128128
case 1:
129-
if((numread=sscanf(temps, "%d %d", h, &scalefactor))==EOF)
129+
if((numread=sscanf(temps, "%d %d", h, &scalefactor))!=2)
130130
_throw("Read error");
131131
break;
132132
case 2:
133-
if((numread=sscanf(temps, "%d", &scalefactor))==EOF)
133+
if((numread=sscanf(temps, "%d", &scalefactor))!=1)
134134
_throw("Read error");
135135
break;
136136
}

0 commit comments

Comments
 (0)