Skip to content

Commit

Permalink
stdio: fix buffer over-read in scanf
Browse files Browse the repository at this point in the history
Over-read was possible if width was greater than remaining input length
when using %c format.

JIRA: RTOS-868
  • Loading branch information
jmaksymowicz committed Aug 2, 2024
1 parent 54ea61e commit 216557c
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions stdio/scanf.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,36 +328,26 @@ static int scanf_parse(char *ccltab, const char *inp, int *inr, char const *fmt0
*/
switch (c) {
case CT_CHAR:
if (width == 0)
if (width == 0) {
width = 1;
if (flags & SUPPRESS) {
size_t sum = 0;
for (;;) {
n = *inr;
if (n < (int)width) {
sum += n;
width -= n;
inp += n;
if (sum == 0)
return (nconversions != 0 ? nassigned : -1);
break;
}
else {
sum += width;
*inr -= width;
inp += width;
break;
}
}
nread += sum;
}
else {

if (*inr <= 0) {
return (nconversions != 0 ? nassigned : -1);
}

if (width > *inr) {
width = *inr;
}

if ((flags & SUPPRESS) == 0) {
memcpy(va_arg(ap, char *), inp, width);
*inr -= width;
inp += width;
nread += width;
nassigned++;
}

*inr -= width;
inp += width;
nread += width;
nconversions++;
break;

Expand Down

0 comments on commit 216557c

Please sign in to comment.