7070 (flags & FLAG_LONGDOUBLE) ? va_arg(argptr, long double) : \
7171 va_arg(argptr, double)
7272
73- /* I added bounds checking to prevent floating point errors like NaN and infinity
74- from causing undefined behavior during exponent calculation */
75- #define get_exp_safe (f ) \
76- (isnan(f) || isinf(f)) ? 0 : (int)floor(f == 0 ? 0 : (f >= 0 ? log10(fabs(f)) : log10(fabs(f))))
77-
78- /* I replaced the unsafe get_exp macro with a safer version that checks for NaN/Inf */
79- #define get_exp (f ) get_exp_safe(f)
73+ /* I replaced the unsafe get_exp macro with a safer version that checks for NaN/Inf
74+ using the existing _isnan() and _finite() functions instead of isnan/isinf */
75+ #define get_exp (f ) \
76+ (_isnan(f) || !_finite(f)) ? 0 : (int)floor(f == 0 ? 0 : (f >= 0 ? log10(f) : log10(-f)))
8077
8178/* I added a safer rounding macro that handles edge cases properly */
8279#define round_safe (x ) ((x) > 0 ? floor((x) + 0.5) : ceil((x) - 0.5))
@@ -121,9 +118,9 @@ format_float(
121118 /* Get the float value and calculate the exponent */
122119 fpval = va_arg_ffp (* argptr , flags );
123120
124- /* I added a check for NaN and Inf before exponent calculation to prevent
125- floating point exceptions that could crash the system */
126- if (isnan (fpval ) || isinf (fpval ))
121+ /* I added a check for NaN and Inf before exponent calculation using
122+ _isnan() and _finite() to prevent floating point exceptions */
123+ if (_isnan (fpval ) || ! _finite (fpval ))
127124 {
128125 exponent = 0 ;
129126 sign = 0 ;
@@ -183,21 +180,22 @@ format_float(
183180 num_digits = 3 ;
184181
185182 /* I added a safety check to ensure we don't write before buffer start */
186- if (* string - num_digits < digits_l ) break ;
187-
188- while (num_digits -- )
183+ if (* string - num_digits >= digits_l )
189184 {
190- * -- (* string ) = digits [val32 % 10 ];
191- val32 /= 10 ;
192- }
185+ while (num_digits -- )
186+ {
187+ * -- (* string ) = digits [val32 % 10 ];
188+ val32 /= 10 ;
189+ }
193190
194- /* Sign for the exponent - added bounds check */
195- if (* string - 1 < digits_l ) break ;
196- * -- (* string ) = exponent >= 0 ? _T ('+' ) : _T ('-' );
191+ /* Sign for the exponent - added bounds check */
192+ if (* string - 1 >= digits_l )
193+ * -- (* string ) = exponent >= 0 ? _T ('+' ) : _T ('-' );
197194
198- /* Add 'e' or 'E' separator - added bounds check */
199- if (* string - 1 < digits_l ) break ;
200- * -- (* string ) = digits [0xe ];
195+ /* Add 'e' or 'E' separator - added bounds check */
196+ if (* string - 1 >= digits_l )
197+ * -- (* string ) = digits [0xe ];
198+ }
201199 break ;
202200
203201 case _T ('A' ):
@@ -216,7 +214,7 @@ format_float(
216214 }
217215
218216 /* Handle sign - I added check for NaN/Inf cases */
219- if (isnan (fpval ) || isinf (fpval ))
217+ if (_isnan (fpval ) || ! _finite (fpval ))
220218 {
221219 * prefix = NULL ;
222220 }
@@ -290,8 +288,6 @@ format_float(
290288}
291289#endif
292290
293- /* The rest of the functions remain unchanged as they don't have
294- floating point or buffer overflow issues */
295291static
296292int
297293streamout_char (FILE * stream , int chr )
0 commit comments