From bd467f94ab81b1181738c4db52e8004c569b4caf 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:31:52 +0200 Subject: [PATCH] encoding: append `0` to strings for compatibility (#10249) --- vlib/encoding/base64/base64.v | 6 ++++-- vlib/rand/rand.v | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/vlib/encoding/base64/base64.v b/vlib/encoding/base64/base64.v index 55fbe67a01..8610471fc2 100644 --- a/vlib/encoding/base64/base64.v +++ b/vlib/encoding/base64/base64.v @@ -35,7 +35,8 @@ pub fn decode_str(data string) string { return '' } unsafe { - buffer := malloc(size) + buffer := malloc(size + 1) + buffer[size] = 0 return tos(buffer, decode_in_buffer(data, buffer)) } } @@ -61,7 +62,8 @@ fn alloc_and_encode(src &byte, len int) string { return '' } unsafe { - buffer := malloc(size) + buffer := malloc(size + 1) + buffer[size] = 0 return tos(buffer, encode_from_buffer(buffer, src, len)) } } diff --git a/vlib/rand/rand.v b/vlib/rand/rand.v index d9c40ed8b6..52dc0a3f86 100644 --- a/vlib/rand/rand.v +++ b/vlib/rand/rand.v @@ -197,12 +197,15 @@ pub fn string_from_set(charset string, len int) string { if len == 0 { return '' } - mut buf := unsafe { malloc(len) } + mut buf := unsafe { malloc(len + 1) } for i in 0 .. len { unsafe { buf[i] = charset[intn(charset.len)] } } + unsafe { + buf[len] = 0 + } return unsafe { buf.vstring_with_len(len) } }