ref: 928a5bb801e95e2c8d1c3153427c74ddf908649b
parent: 39887219c3ec14f8316cb4d3e259c3c4bfade700
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Mon Jan 27 13:56:38 EST 2025
snprint_real: ensure there is no truncation
--- a/print.c
+++ b/print.c
@@ -554,16 +554,13 @@
// recommend 10
int max_digs_lf)
{
- int mag;
- double fpart, temp;
- char format[8];
- char num_format[3];
- int sz, keepz = 0;
+ bool keepz = false;
+ int mag, sz;
s[0] = '\0';
if(width == -1){
width = 0;
- keepz = 1;
+ keepz = true;
}
if(isinf(r)){
strncpy(s, signbit(r) ? "-inf" : "inf", cnt);
@@ -578,15 +575,16 @@
return;
}
+ char num_format[4];
num_format[0] = 'l';
num_format[2] = '\0';
mag = double_exponent(r);
-
mag = (int)(((double)mag)/LOG2_10 + 0.5);
if(r == 0)
mag = 0;
- if((mag > max_digs_lf-1) || (mag < -max_digs_rt)){
+ double fpart, temp;
+ if(mag > max_digs_lf-1 || mag < -max_digs_rt){
num_format[1] = 'e';
temp = r/pow(10, mag); /* see if number will have a decimal */
fpart = temp - floor(temp); /* when written in scientific notation */
@@ -596,6 +594,8 @@
}
if(fpart == 0)
dec = 0;
+
+ char format[28];
if(width == 0)
snprintf(format, sizeof(format), "%%.%d%s", dec, num_format);
else