freebsd-src
freebsd-src copied to clipboard
kvprintf(): Fix '+' conversion handling
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.
This looks like it changes the meaning of both '%-p' and '%+p'. before it would always set sign to 0. Now it doesn't which seems like it would be weird.
I have to admit that I did test the '%+p' using glibc. It seems that it is not defined by POSIX.
#include <stdio.h>
void print(void *p)
{
printf("'%20p' '%-20p' '%+20p'\n", p, p, p);
}
int main()
{
print(0);
print((void*)1L);
print((void*)-1L);
return 0;
}
On FreeBSD:
cc -Wall -Wextra -pedantic main.c
main.c:5:29: warning: flag '+' results in undefined behavior with 'p' conversion specifier [-Wformat]
printf("'%20p' '%-20p' '%+20p'\n", p, p, p);
~^~~~
1 warning generated.
./a.out
' 0x0' '0x0 ' ' 0x0'
' 0x1' '0x1 ' ' 0x1'
' 0xffffffffffffffff' '0xffffffffffffffff ' ' 0xffffffffffffffff'
On glibc:
cc -Wall -Wextra -pedantic main.c
main.c: In function ‘print’:
main.c:5:32: warning: '+' flag used with ‘%p’ gnu_printf format [-Wformat=]
printf("'%20p' '%-20p' '%+20p'\n", p, p, p);
^
./a.out
' (nil)' '(nil) ' ' (nil)'
' 0x1' '0x1 ' ' +0x1'
' 0xffffffffffffffff' '0xffffffffffffffff ' ' +0xffffffffffffffff'
Please let me know if you are in principle fine with this change. I can change the '%+p' behaviour to match with the FreeBSD user space.
I changed the patch to keep the current "%+p" behaviour.
Sorry missed these comments... I think I'm fine with the change (either way, but happier with it matching user space better). I started looking at this, then got distracted and didn't get back to it.
Anything left to do on my side?
Anything left to do on my side?
No. I think it's ready to go, but am not sure. Was going to look closely on my next pr landing day... but last friday and this have gotten away from me so it will be maybe monday or tiesday this next week when i hope to do a mini one.