From 0ff2d9ef786d5748d40c06bba932133e65e77a3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Sat, 29 May 2021 15:24:09 +0200 Subject: [PATCH] strconv: fix memory corruption (#10250) --- vlib/strconv/format_mem.v | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/vlib/strconv/format_mem.v b/vlib/strconv/format_mem.v index 6948cc70cf..8b3804ff6f 100644 --- a/vlib/strconv/format_mem.v +++ b/vlib/strconv/format_mem.v @@ -445,7 +445,7 @@ pub fn remove_tail_zeros(s string) string { mut i_s := 0 // skip spaces - for i_s < s.len && s[i_s] !in [`-`,`+`] && s[i_s] >= `9` && s[i_s] <= `0`{ + for i_s < s.len && s[i_s] !in [`-`,`+`] && (s[i_s] > `9` || s[i_s] < `0`) { buf[i_d] = s[i_s] i_s++ i_d++ @@ -482,16 +482,19 @@ pub fn remove_tail_zeros(s string) string { i_s = i_s1 } - if s[i_s] != `.` { + if i_s < s.len && s[i_s] != `.` { // check exponent - for i_s < s.len { + for { buf[i_d] = s[i_s] i_s++ i_d++ + if i_s >= s.len { + break + } } } buf[i_d] = 0 - return tos(buf, i_d+1) + return tos(buf, i_d) } }