encoding: append `0` to strings for compatibility (#10249)

pull/10261/head
Uwe Krüger 2021-05-29 15:31:52 +02:00 committed by GitHub
parent 0ff2d9ef78
commit bd467f94ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 3 deletions

View File

@ -35,7 +35,8 @@ pub fn decode_str(data string) string {
return '' return ''
} }
unsafe { unsafe {
buffer := malloc(size) buffer := malloc(size + 1)
buffer[size] = 0
return tos(buffer, decode_in_buffer(data, buffer)) return tos(buffer, decode_in_buffer(data, buffer))
} }
} }
@ -61,7 +62,8 @@ fn alloc_and_encode(src &byte, len int) string {
return '' return ''
} }
unsafe { unsafe {
buffer := malloc(size) buffer := malloc(size + 1)
buffer[size] = 0
return tos(buffer, encode_from_buffer(buffer, src, len)) return tos(buffer, encode_from_buffer(buffer, src, len))
} }
} }

View File

@ -197,12 +197,15 @@ pub fn string_from_set(charset string, len int) string {
if len == 0 { if len == 0 {
return '' return ''
} }
mut buf := unsafe { malloc(len) } mut buf := unsafe { malloc(len + 1) }
for i in 0 .. len { for i in 0 .. len {
unsafe { unsafe {
buf[i] = charset[intn(charset.len)] buf[i] = charset[intn(charset.len)]
} }
} }
unsafe {
buf[len] = 0
}
return unsafe { buf.vstring_with_len(len) } return unsafe { buf.vstring_with_len(len) }
} }