Berry 'bytes().asstring()' now truncates a string if buffer contains NULL (#23311)

This commit is contained in:
s-hadinger 2025-04-17 21:51:03 +02:00 committed by GitHub
parent 2bc5f682b0
commit c2628c95f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 9 additions and 1 deletions

View File

@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file.
### Changed
### Fixed
- Berry `bytes().asstring()` now truncates a string if buffer contains NULL
### Removed

View File

@ -806,7 +806,8 @@ static int m_asstring(bvm *vm)
{
buf_impl attr = bytes_check_data(vm, 0);
check_ptr(vm, &attr);
be_pushnstring(vm, (const char*) attr.bufptr, attr.len);
size_t safe_len = strnlen((const char*) attr.bufptr, attr.len);
be_pushnstring(vm, (const char*) attr.bufptr, safe_len);
be_return(vm);
}

View File

@ -354,3 +354,9 @@ assert(b.appendb64(c, 2) == bytes("AABBCC49673D3D"))
b = bytes("AABBCC")
assert(bytes().fromstring(bytes("11").tob64()) == bytes('45513D3D'))
assert(b.appendb64(c, 1, 1) == bytes("AABBCC45513D3D"))
#- asstring truncates if NULL is present -#
s=bytes("414243").asstring()
assert(size(s) == 3)
s=bytes("410000").asstring()
assert(size(s) == 1)