From b9586a401742a437a766ae0fc98c6c86c0ad20e9 Mon Sep 17 00:00:00 2001 From: lemoncmd Date: Tue, 2 Jul 2019 17:13:18 +0900 Subject: [PATCH] builtin : fix buffer overflow and i64 issue with hex() --- vlib/builtin/int.v | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/vlib/builtin/int.v b/vlib/builtin/int.v index f5c304c984..ad08158df9 100644 --- a/vlib/builtin/int.v +++ b/vlib/builtin/int.v @@ -123,17 +123,25 @@ pub fn (b bool) str() string { } pub fn (n int) hex() string { - s := n.str() - hex := malloc(s.len + 3) // 0x + \n + len := if n >= 0 { + n.str().len + 3 + } else { + 11 + } + hex := malloc(len) // 0x + \n count := int(C.sprintf(hex, '0x%x', n)) return tos(hex, count) } pub fn (n i64) hex() string { - s := n.str() - hex := malloc(s.len + 3) - C.sprintf(hex, '0x%x', n) - return tos(hex, s.len + 3) + len := if n >= i64(0) { + n.str().len + 3 + } else { + 19 + } + hex := malloc(len) + count := int(C.sprintf(hex, '0x%x', n)) + return tos(hex, count) } pub fn (a []byte) contains(val byte) bool {