Skip to content

Commit

Permalink
kvprintf(): Fix '+' conversion handling
Browse files Browse the repository at this point in the history
For example, printf("%+i", 1) prints "+1".  However, kvprintf() did print just
"1" for this example.  According to PRINTF(3):

  A sign must always be placed before a number produced by a signed conversion.

For "%+r" radix conversions, keep the "+" handling as it is, since this is a
non-standard conversion.

This change allows to support the ' ' conversion modifier in the future.
  • Loading branch information
sebhub committed Jul 1, 2024
1 parent 4ce4a0f commit dfe8b20
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions sys/kern/subr_prf.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,9 +660,9 @@ kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_lis
char *d;
const char *p, *percent, *q;
u_char *up;
int ch, n;
int ch, n, sign;
uintmax_t num;
int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
int base, lflag, qflag, tmp, width, ladjust, sharpflag, dot;
int cflag, hflag, jflag, tflag, zflag;
int bconv, dwidth, upper;
char padc;
Expand Down Expand Up @@ -690,7 +690,7 @@ kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_lis
PCHAR(ch);
}
percent = fmt - 1;
qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0;
sign = 0; dot = 0; bconv = 0; dwidth = 0; upper = 0;
cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
reswitch: switch (ch = (u_char)*fmt++) {
Expand All @@ -701,7 +701,7 @@ reswitch: switch (ch = (u_char)*fmt++) {
sharpflag = 1;
goto reswitch;
case '+':
sign = 1;
sign = '+';
goto reswitch;
case '-':
ladjust = 1;
Expand Down Expand Up @@ -771,7 +771,6 @@ reswitch: switch (ch = (u_char)*fmt++) {
case 'd':
case 'i':
base = 10;
sign = 1;
goto handle_sign;
case 'h':
if (hflag) {
Expand Down Expand Up @@ -816,16 +815,17 @@ reswitch: switch (ch = (u_char)*fmt++) {
case 'p':
base = 16;
sharpflag = (width == 0);
sign = 0;
num = (uintptr_t)va_arg(ap, void *);
goto number;
case 'q':
qflag = 1;
goto reswitch;
case 'r':
base = radix;
if (sign)
if (sign) {
sign = 0;
goto handle_sign;
}
goto handle_nosign;
case 's':
p = va_arg(ap, char *);
Expand Down Expand Up @@ -862,13 +862,11 @@ reswitch: switch (ch = (u_char)*fmt++) {
goto handle_nosign;
case 'y':
base = 16;
sign = 1;
goto handle_sign;
case 'z':
zflag = 1;
goto reswitch;
handle_nosign:
sign = 0;
if (jflag)
num = va_arg(ap, uintmax_t);
else if (qflag)
Expand Down Expand Up @@ -907,11 +905,11 @@ reswitch: switch (ch = (u_char)*fmt++) {
num = (signed char)va_arg(ap, int);
else
num = va_arg(ap, int);
number:
if (sign && (intmax_t)num < 0) {
neg = 1;
if ((intmax_t)num < 0) {
sign = '-';
num = -(intmax_t)num;
}
number:
p = ksprintn(nbuf, num, base, &n, upper);
tmp = 0;
if (sharpflag && num != 0) {
Expand All @@ -920,7 +918,7 @@ reswitch: switch (ch = (u_char)*fmt++) {
else if (base == 16)
tmp += 2;
}
if (neg)
if (sign)
tmp++;

if (!ladjust && padc == '0')
Expand All @@ -930,8 +928,8 @@ reswitch: switch (ch = (u_char)*fmt++) {
if (!ladjust)
while (width-- > 0)
PCHAR(' ');
if (neg)
PCHAR('-');
if (sign)
PCHAR(sign);
if (sharpflag && num != 0) {
if (base == 8) {
PCHAR('0');

Check warning on line 935 in sys/kern/subr_prf.c

View workflow job for this annotation

GitHub Actions / Style Checker

Missing Signed-off-by: line
Expand Down

0 comments on commit dfe8b20

Please sign in to comment.