newlib: fix uninitialized character count being used when printing float without "_printf_float" being linked

Message ID 20250313191649.270884-1-igor.petrov.dev@pm.me
State New
Headers
Series newlib: fix uninitialized character count being used when printing float without "_printf_float" being linked |

Commit Message

Igor Petrov March 13, 2025, 7:23 p.m. UTC
  Patch fixes wrong number of written characters being returend from 'printf'
family of functionsx when '_printf_float' is not linked (nano.specs). If user
tries to print a floating point number anyway, returned number of written
characters is not correct. For example in
    printf("%d%f", 1, 1.0);
should return 1, but actaully returns 2.

---
 newlib/libc/machine/msp430/tiny-printf.c | 1 +
 newlib/libc/stdio/nano-vfprintf.c        | 1 +
 2 files changed, 2 insertions(+)
  

Comments

Corinna Vinschen March 14, 2025, 4:06 p.m. UTC | #1
On Mar 13 19:23, Igor Petrov wrote:
> Patch fixes wrong number of written characters being returend from 'printf'
> family of functionsx when '_printf_float' is not linked (nano.specs). If user
> tries to print a floating point number anyway, returned number of written
> characters is not correct. For example in
>     printf("%d%f", 1, 1.0);
> should return 1, but actaully returns 2.
> 
> ---
>  newlib/libc/machine/msp430/tiny-printf.c | 1 +
>  newlib/libc/stdio/nano-vfprintf.c        | 1 +
>  2 files changed, 2 insertions(+)

Pushed.


Thanks,
Corinna
  

Patch

diff --git a/newlib/libc/machine/msp430/tiny-printf.c b/newlib/libc/machine/msp430/tiny-printf.c
index f2412a0e6..b5ad5284c 100644
--- a/newlib/libc/machine/msp430/tiny-printf.c
+++ b/newlib/libc/machine/msp430/tiny-printf.c
@@ -237,6 +237,7 @@  __tiny_vfprintf_r (struct _reent *data,
 		GET_ARG (N, ap_copy, _LONG_DOUBLE);
 	      else
 		GET_ARG (N, ap_copy, double);
+	      n = 0;
 	    }
 	  else
             n = _printf_float (data, &prt_data, fp, pfunc, &ap_copy);
diff --git a/newlib/libc/stdio/nano-vfprintf.c b/newlib/libc/stdio/nano-vfprintf.c
index 0d42a940f..bdcc9b218 100644
--- a/newlib/libc/stdio/nano-vfprintf.c
+++ b/newlib/libc/stdio/nano-vfprintf.c
@@ -627,6 +627,7 @@  _VFPRINTF_R (struct _reent *data,
 		GET_ARG (N, ap_copy, _LONG_DOUBLE);
 	      else
 		GET_ARG (N, ap_copy, double);
+	      n = 0;
 	    }
 	  else
             n = _printf_float (data, &prt_data, fp, pfunc, &ap_copy);