From 67d9729311f698f78ec17631b3cdfa2d7e080b69 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Tue, 21 Jun 2022 15:18:58 -0700 Subject: [PATCH] Treat all units equally when converting to string Excessive trucation in mils can lead to data loss when round-tripping values. Better to keep decimal count consistent between units Fixes https://gitlab.com/kicad/code/kicad/issues/11878 --- common/base_units.cpp | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/common/base_units.cpp b/common/base_units.cpp index 275550fc44..35dde725b9 100644 --- a/common/base_units.cpp +++ b/common/base_units.cpp @@ -229,22 +229,17 @@ wxString StringFromValue( EDA_UNITS aUnits, double aValue, bool aAddUnitSymbol, if( value_to_print != 0.0 && fabs( value_to_print ) <= 0.0001 ) { - int len = sprintf( buf, "%.10f", value_to_print ); + int len = snprintf( buf, sizeof( buf ) - 1, "%.10f", value_to_print ); while( --len > 0 && buf[len] == '0' ) buf[len] = '\0'; - if( buf[len]=='.' || buf[len]==',' ) + if( len >= 0 && ( buf[len]=='.' || buf[len]==',' ) ) buf[len] = '\0'; - else - ++len; } else { - if( aUnits == EDA_UNITS::MILS ) - sprintf( buf, "%.7g", value_to_print ); - else - sprintf( buf, "%.10g", value_to_print ); + snprintf( buf, sizeof( buf ) - 1, "%.10g", value_to_print ); } wxString stringValue( buf, wxConvUTF8 );