From b6c488a8839ce261773724a16a1ef3131af88423 Mon Sep 17 00:00:00 2001 From: s-hadinger <49731213+s-hadinger@users.noreply.github.com> Date: Mon, 1 Jul 2024 22:53:49 +0200 Subject: [PATCH] Berry `bytes.resize()` for large sizes (#21716) --- CHANGELOG.md | 1 + lib/libesp32/berry/src/be_byteslib.c | 2 +- lib/libesp32/berry/src/be_filelib.c | 18 ++++++++++-------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f3a04621..0c897b183 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ All notable changes to this project will be documented in this file. - ESP32 TM1621 number overflow from "9999" to "12E3" (#21131) ### Fixed +- Berry `bytes.resize()` for large sizes ### Removed diff --git a/lib/libesp32/berry/src/be_byteslib.c b/lib/libesp32/berry/src/be_byteslib.c index 5c50a36ba..f40bc7f43 100644 --- a/lib/libesp32/berry/src/be_byteslib.c +++ b/lib/libesp32/berry/src/be_byteslib.c @@ -223,7 +223,7 @@ static unsigned int decode_base64(unsigned char input[], unsigned char output[]) // shrink or increase. If increase, fill with zeores. Cannot go beyond `size` static void buf_set_len(buf_impl* attr, const size_t len) { - uint16_t old_len = attr->len; + int32_t old_len = attr->len; attr->len = ((int32_t)len <= attr->size) ? (int32_t)len : attr->size; if (old_len < attr->len) { memset((void*) &attr->bufptr[old_len], 0, attr->len - old_len); diff --git a/lib/libesp32/berry/src/be_filelib.c b/lib/libesp32/berry/src/be_filelib.c index 87691809b..f4f760b1e 100644 --- a/lib/libesp32/berry/src/be_filelib.c +++ b/lib/libesp32/berry/src/be_filelib.c @@ -79,15 +79,17 @@ static int i_readbytes(bvm *vm) be_call(vm, 2); /* call b.resize(size) */ be_pop(vm, 3); /* bytes() instance is at top */ - char *buffer = (char*) be_tobytes(vm, -1, NULL); /* we get the address of the internam buffer of size 'size' */ - size = be_fread(fh, buffer, size); + char *buffer = (char*) be_tobytes(vm, -1, NULL); /* we get the address of the internal buffer of size 'size' */ + size_t read_size = be_fread(fh, buffer, size); - /* resize if something went wrong */ - be_getmember(vm, -1, "resize"); - be_pushvalue(vm, -2); - be_pushint(vm, size); - be_call(vm, 2); /* call b.resize(size) */ - be_pop(vm, 3); /* bytes() instance is at top */ + if (size != read_size) { + /* resize if something went wrong */ + be_getmember(vm, -1, "resize"); + be_pushvalue(vm, -2); + be_pushint(vm, read_size); + be_call(vm, 2); /* call b.resize(size) */ + be_pop(vm, 3); /* bytes() instance is at top */ + } } else { be_pushbytes(vm, NULL, 0); }