Berry alternative to strnlen (#23952)

This commit is contained in:
s-hadinger 2025-09-26 16:27:02 +02:00 committed by GitHub
parent e479eeff3d
commit fee21ba272
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -806,7 +806,10 @@ static int m_asstring(bvm *vm)
{
buf_impl attr = bytes_check_data(vm, 0);
check_ptr(vm, &attr);
size_t safe_len = strnlen((const char*) attr.bufptr, attr.len);
/* equivalent to strnlen() */
const char* str = (const char*) attr.bufptr;
const char* found = memchr(str, '\0', attr.len);
size_t safe_len = found ? (size_t)(found - str) : (size_t)attr.len;
be_pushnstring(vm, (const char*) attr.bufptr, safe_len);
be_return(vm);
}