From 6e4bda374189a7b238519219b5f1c99fc8104392 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 17 Oct 2021 11:50:26 +0300 Subject: [PATCH] strconv: fix string builder leaks in format_dec_old, format_es_old, format_fl_old, v_sprintf --- vlib/strconv/format.v | 3 +++ vlib/strconv/vprintf.c.v | 20 ++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/vlib/strconv/format.v b/vlib/strconv/format.v index e11a604207..c4c3bf5b11 100644 --- a/vlib/strconv/format.v +++ b/vlib/strconv/format.v @@ -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) diff --git a/vlib/strconv/vprintf.c.v b/vlib/strconv/vprintf.c.v index 7347d55791..bbf78179c2 100644 --- a/vlib/strconv/vprintf.c.v +++ b/vlib/strconv/vprintf.c.v @@ -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 {