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]>
---
v3: Fix the bug properly
add newline at end of testcase
don't regenerate any autogenerated files
mingw-w64-crt/stdio/mingw_pformat.c | 11 +++--------
mingw-w64-crt/testcases/t_printf_g_width.c | 18 ++++++++++++++++++
2 files changed, 21 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..10d9ece10 100644
--- a/mingw-w64-crt/stdio/mingw_pformat.c
+++ b/mingw-w64-crt/stdio/mingw_pformat.c
@@ -1881,14 +1881,9 @@ 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;
+ 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/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