Skip to content
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

tinystdio: Fix vfprintf to not cap the max precision to print a hex format #19

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions newlib/libc/tinystdio/vfprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -688,14 +688,24 @@ int vfprintf (FILE * stream, const CHAR *fmt, va_list ap_orig)
#ifdef _NEED_IO_C99_FORMATS
if (c == 'a') {

int init_prec = prec;

c = 'p';
flags |= FL_FLTEXP | FL_FLTHEX;

if (!(flags & FL_PREC))
prec = -1;

prec = __float_x_engine(fval, &dtoa, prec, case_convert);

if(prec < init_prec) /* if dtox engine capped the precision required */
prec = init_prec;

ndigs = prec + 1;

if(ndigs > 14) /* the max dtox ndigs */
ndigs = 14;

exp = dtoa.exp;
ndigs_exp = 1;
} else
Expand Down
1 change: 1 addition & 0 deletions test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ if (posix_io or not tinystdio) and tests_enable_posix_io
'test-fgetc',
'test-fgets-eof',
'test-wchar',
'test-vfprintf-hex-prec'
]
endif
endif
Expand Down
62 changes: 62 additions & 0 deletions test/test-vfprintf-hex-prec.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright © 2024, Synopsys Inc.
* Copyright © 2024, Solid Sands B.V.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>
#include <string.h>

int main(void) {
int res = 0;
int ret = 0;
size_t len = 0;
char strin[50];

res = sprintf(strin, "%.15A\n", 471.2853);

const char* ref_out = "0X1.D749096BB98C800P+8\n";
const int ref_len = 23;

ret = strcmp(strin, ref_out);
len = strlen(strin);

if ((res != ref_len) || (ret != 0) || (len != ref_len)) {
printf("Test Failed: Failed to read/write 15 digits after decimal point in hex format\n");
return 1;
}

printf("Test Passed\n");

return 0;
}
Loading