Skip to content

Commit bd467f9

Browse files
authored
encoding: append 0 to strings for compatibility (#10249)
1 parent 0ff2d9e commit bd467f9

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

vlib/encoding/base64/base64.v

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ pub fn decode_str(data string) string {
3535
return ''
3636
}
3737
unsafe {
38-
buffer := malloc(size)
38+
buffer := malloc(size + 1)
39+
buffer[size] = 0
3940
return tos(buffer, decode_in_buffer(data, buffer))
4041
}
4142
}
@@ -61,7 +62,8 @@ fn alloc_and_encode(src &byte, len int) string {
6162
return ''
6263
}
6364
unsafe {
64-
buffer := malloc(size)
65+
buffer := malloc(size + 1)
66+
buffer[size] = 0
6567
return tos(buffer, encode_from_buffer(buffer, src, len))
6668
}
6769
}

vlib/rand/rand.v

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,15 @@ pub fn string_from_set(charset string, len int) string {
197197
if len == 0 {
198198
return ''
199199
}
200-
mut buf := unsafe { malloc(len) }
200+
mut buf := unsafe { malloc(len + 1) }
201201
for i in 0 .. len {
202202
unsafe {
203203
buf[i] = charset[intn(charset.len)]
204204
}
205205
}
206+
unsafe {
207+
buf[len] = 0
208+
}
206209
return unsafe { buf.vstring_with_len(len) }
207210
}
208211

0 commit comments

Comments
 (0)