From 481e445e8a11ebe8dd06d10d94c0ec97b897f606 Mon Sep 17 00:00:00 2001 From: Guillaume Beraudo Date: Thu, 19 Jun 2014 14:47:20 +0200 Subject: [PATCH] Fix printInt when value=INT_MIN (was overflowing) --- Util/StringUtil.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Util/StringUtil.h b/Util/StringUtil.h index 476e31cee..68a9ac991 100644 --- a/Util/StringUtil.h +++ b/Util/StringUtil.h @@ -40,18 +40,19 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // precision: position after decimal point // length: maximum number of digits including comma and decimals +// work with negative values to prevent overflowing when taking -value template static inline char *printInt(char *buffer, int value) { - bool minus = false; - if (value < 0) + bool minus = true; + if (value > 0) { - minus = true; + minus = false; value = -value; } buffer += length - 1; for (int i = 0; i < precision; i++) { - *buffer = '0' + (value % 10); + *buffer = '0' - (value % 10); value /= 10; buffer--; } @@ -59,7 +60,7 @@ template static inline char *printInt(char *buffer, buffer--; for (int i = precision + 1; i < length; i++) { - *buffer = '0' + (value % 10); + *buffer = '0' - (value % 10); value /= 10; if (value == 0) break;