-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
libc/stdio: Increase BUF in vfprintf.c and vfwprintf.c #1400
base: main
Are you sure you want to change the base?
Conversation
Thinking about this some more, we should just be able to remove the whole conditional and make BUF |
With the %b format specifier we need enough space to write a uintmax_t in binary.
@@ -289,13 +289,9 @@ vfprintf(FILE * __restrict fp, const char * __restrict fmt0, va_list ap) | |||
/* | |||
* The size of the buffer we use as scratch space for integer | |||
* conversions, among other things. We need enough space to | |||
* write a uintmax_t in octal (plus one byte). | |||
* write a uintmax_t in binary. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you need +2 for the 0b?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I didn't think of that, but it looks like we print it separately:
freebsd-src/lib/libc/stdio/vfprintf.c
Line 1022 in 6e41473
if (ox[1]) { /* ox[1] is either x, X, or \0 */ |
freebsd-src/lib/libc/stdio/vfwprintf.c
Line 1077 in 6e41473
if (ox[1]) { /* ox[1] is either x, X, or \0 */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then don't we need +1 for NUL?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell, we don't.
In the case of %c, we write to it manually (or in the case of a wide char through wcrtomb
). Otherwise we pass it to __ujtoa
or __ultoa
which both stop when they're done writing the number.
After that we just print with size
which is calculated using buf + BUF - cp
where cp
is buf
plus some offset.
When calling grouping_print
, to print a number with separators, we pass a start pointer and end pointer (cp
and buf+BUF
), which are used to call io_print
(through io_printandpad
) with the correct size.
Overall the code looks like it doesn't expect a null terminator. However, I think that the comment I removed was hinting that we did, otherwise I'm not sure what that +1 would be for. Unless there's some edge case I'm missing...
FWIW, the patch also survived the cirrus-ci boot smoke test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the 0b that's prented isn't accounted for, but isn't now.
With the new %b format specifier, we need at most 64 characters.
Also, the comment says that we need enough space to write a uintmax_t in octal plus one byte, I'm not sure what that's for. I guess its implying that we need a null terminator(?), but it doesn't look like we do.
This can be easily tested by doing: