mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-04 08:50:27 +00:00
Fix issues 774, 782 and 789 (printf precision bugs) (#790)
The C standard states that, within the context of a printf-family function, when specifying the precision of a conversion specification: > A negative precision argument is taken as if the precision were > omitted. - Quoth the C Standard, 7.23.6.1. The fprintf function Cosmopolitan instead treated negative precision arguments as though they had a value of 0, which was non-conforming. This change fixes that. Another issue we found relates to: > For o conversion, it increases the precision, if and only if > necessary, to force the first digit of the result to be a zero (if > the value and precision are both 0, a single 0 is printed). - Quoth the C standard, 7.23.6.1.6. The fprintf function When printing numbers in their alternative form, with a precision and with a conversion specifier of o (octal), Cosmopolitan wasn't following the standard in two ways: 1. When printing a value with a precision that results in 0-padding, cosmopolitan would still add an extra 0 even though this should be done "if and only if necessary" 2. When printing a value of 0 with a precision of 0, nothing is printed, even though the standard specifically states that a single 0 is printed in this case This change fixes those issues too. Furthermore, regression tests have been introduced to ensure Cosmopolitan continues to be conformant going forward. Fixes #774 Fixes #782 Fixes #789
This commit is contained in:
parent
2f4335e081
commit
7f925e6be9
3 changed files with 33 additions and 4 deletions
|
@ -16,10 +16,11 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/fmt/fmt.h"
|
||||
|
||||
#include "libc/assert.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/fmt/fmt.h"
|
||||
#include "libc/fmt/fmt.internal.h"
|
||||
#include "libc/fmt/internal.h"
|
||||
#include "libc/fmt/itoa.h"
|
||||
|
@ -255,6 +256,7 @@ _Hide int __fmt(void *fn, void *arg, const char *format, va_list va) {
|
|||
}
|
||||
if (prec < 0) {
|
||||
prec = 0;
|
||||
flags &= ~FLAGS_PRECISION;
|
||||
}
|
||||
|
||||
// evaluate length field
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue