Remove incorrect width adjustment that prevented proper padding
with spaces for %g format specifier when the value has trailing
zeros in the integer part.
The original code would reduce the field width when precision
became negative (e.g., for 100000.0, precision = strlen("1") - 6 = -5),
causing the output to lose its padding:
printf("(%10g)\n", 100000.0f); // was: (100000), expected: ( 100000)
The fix clamps negative precision to 0 instead of adjusting the
width, preserving the requested field width for proper padding.
Adds test case t_printf_g_width.
ref: https://github.com/skeeto/w64devkit/issues/269
Signed-off-by: Peter Damianov <[email protected]>
---
v4: restore changes to Makefile.am
add comment clarifying fix further
mingw-w64-crt/stdio/mingw_pformat.c | 16 ++++++++--------
mingw-w64-crt/testcases/Makefile.am | 1 +
mingw-w64-crt/testcases/t_printf_g_width.c | 18 ++++++++++++++++++
3 files changed, 27 insertions(+), 8 deletions(-)
create mode 100644 mingw-w64-crt/testcases/t_printf_g_width.c
diff --git a/mingw-w64-crt/stdio/mingw_pformat.c
b/mingw-w64-crt/stdio/mingw_pformat.c
index b7d3bfb41..3df14704b 100644
--- a/mingw-w64-crt/stdio/mingw_pformat.c
+++ b/mingw-w64-crt/stdio/mingw_pformat.c
@@ -1881,14 +1881,14 @@ void __pformat_gfloat( long double x, __pformat_t
*stream )
* precede the radix point, but we truncate any balance following
* it, to suppress output of non-significant trailing zeros...
*/
- if( ((stream->precision = strlen( value ) - intlen) < 0)
- /*
- * This may require a compensating adjustment to the field
- * width, to accommodate significant trailing zeros, which
- * precede the radix point...
- */
- && (stream->width > 0) )
- stream->width += stream->precision;
+ stream->precision = strlen( value ) - intlen;
+ /* When the mantissa is shorter than the number of integer digits
+ * (e.g., 100000 has mantissa "1" but requires 6 digit positions),
+ * precision becomes negative. Clamp to zero to represent no
+ * fractional digits.
+ */
+ if( stream->precision < 0 )
+ stream->precision = 0;
/* Now, we format the result as any other fixed point value.
*/
diff --git a/mingw-w64-crt/testcases/Makefile.am
b/mingw-w64-crt/testcases/Makefile.am
index b9184e284..61712229c 100644
--- a/mingw-w64-crt/testcases/Makefile.am
+++ b/mingw-w64-crt/testcases/Makefile.am
@@ -30,6 +30,7 @@ testcase_progs = \
t_mbrtowc \
t_mbsrtowcs \
t_nullptrexception \
+ t_printf_g_width \
t_readdir \
t_snprintf \
t_snprintf0 \
diff --git a/mingw-w64-crt/testcases/t_printf_g_width.c
b/mingw-w64-crt/testcases/t_printf_g_width.c
new file mode 100644
index 000000000..b4a32f981
--- /dev/null
+++ b/mingw-w64-crt/testcases/t_printf_g_width.c
@@ -0,0 +1,18 @@
+#define __USE_MINGW_ANSI_STDIO 1
+#include <stdio.h>
+#include <string.h>
+
+int main(void) {
+ char buffer[64];
+
+ // Test the %g width specifier
+ // This should output "( 100000)" with 4 leading spaces
+ int ret = snprintf(buffer, sizeof(buffer), "(%10g)", 100000.0f);
+
+ if (strcmp(buffer, "( 100000)") != 0) {
+ fprintf(stderr, "FAIL: Expected '( 100000)', got '%s'\n", buffer);
+ return 1;
+ }
+
+ return 0;
+}
--
2.39.5
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public