strconv: fix string builder leaks in format_dec_old, format_es_old, format_fl_old, v_sprintf

pull/12222/head
Delyan Angelov 2021-10-17 11:50:26 +03:00
parent 1313dcf601
commit 6e4bda3741
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 17 additions and 6 deletions

View File

@ -98,6 +98,9 @@ pub fn format_str(s string, p BF_param) string {
return s.clone()
}
mut res := strings.new_builder(s.len + dif)
defer {
unsafe { res.free() }
}
if p.allign == .right {
for i1 := 0; i1 < dif; i1++ {
res.write_b(p.pad_ch)

View File

@ -28,6 +28,9 @@ pub fn v_printf(str string, pt ...voidptr) {
pub fn v_sprintf(str string, pt ...voidptr) string {
mut res := strings.new_builder(pt.len * 16)
defer {
unsafe { res.free() }
}
mut i := 0 // main string index
mut p_index := 0 // parameter index
@ -531,6 +534,9 @@ pub fn format_fl_old(f f64, p BF_param) string {
tmp.free()
}
mut res := strings.new_builder(if p.len0 > fs.len { p.len0 } else { fs.len })
defer {
res.free()
}
mut sign_len_diff := 0
if p.pad_ch == `0` {
@ -580,9 +586,7 @@ pub fn format_fl_old(f f64, p BF_param) string {
s.free()
fs.free()
tmp_res := res.str()
res.free()
return tmp_res
return res.str()
}
}
@ -595,6 +599,9 @@ pub fn format_es_old(f f64, p BF_param) string {
fs = remove_tail_zeros_old(fs)
}
mut res := strings.new_builder(if p.len0 > fs.len { p.len0 } else { fs.len })
defer {
res.free()
}
mut sign_len_diff := 0
if p.pad_ch == `0` {
@ -642,9 +649,7 @@ pub fn format_es_old(f f64, p BF_param) string {
}
s.free()
fs.free()
tmp_res := res.str()
res.free()
return tmp_res
return res.str()
}
}
@ -692,6 +697,9 @@ pub fn remove_tail_zeros_old(s string) string {
pub fn format_dec_old(d u64, p BF_param) string {
mut s := ''
mut res := strings.new_builder(20)
defer {
unsafe { res.free() }
}
mut sign_len_diff := 0
if p.pad_ch == `0` {
if p.positive {