mit-pdos / xv6-riscv

Xv6 for RISC-V
Other
6.6k stars 2.38k forks source link

There may have a bug in printf.c. #121

Closed jasonzhouy closed 1 year ago

jasonzhouy commented 2 years ago

the error function is :

printf.c -> void printf(char *fmt, ...)

i find printf(char *fmt, ...) function may drop the character '%' in the end of any string. Such as printf("i can print last %\n") can work as expected , but printf("i can print last %") can't. The error behavior is reason for
c = fmt[++i] & 0xff; if (c == 0) break; . I think it can be changed as

if (c == 0) { // when string is '\0' , this loop break at 'for' . So it is security using i - 1 in here. if (fmt[i - 1] == '%') consputc('%'); break; }

kaashoek commented 1 year ago

This is not a bug: % indicates the format for an argument. If you want to print '%', escape the % (i.e., type %%). xv6 printf could give a better error for % followed by a non-format character.