Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Berry avoid readbytes() from crashing when file is too large #22057

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ All notable changes to this project will be documented in this file.
- LVGL Added OpenHASP icons to font `montserrat-28`
- Matter fail to report Shutter status if no shutter is configured in Tasmota
- Matter fix Waterleak broken after Berry solidification optimisation #21885
- Berry avoid `readbytes()` from crashing when file is too large

### Removed
- Berry remove reuse of methods for interface-like code reuse #21500
Expand Down
12 changes: 12 additions & 0 deletions lib/libesp32/berry/src/be_filelib.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "be_sys.h"
#include "be_gc.h"
#include "be_bytecode.h"
#include "be_vm.h"
#include <string.h>

#define READLINE_STEP 100
Expand Down Expand Up @@ -71,12 +72,23 @@ static int i_readbytes(bvm *vm)
void *fh = be_tocomptr(vm, -1);
size_t size = readsize(vm, argc, fh);
if (size) {
if (size > vm->bytesmaxsize) {
be_raise(vm, "memory_error", "size exceeds maximum allowed for bytes");
}
/* avoid double allocation, using directly the internal buffer of bytes() */
be_getbuiltin(vm, "bytes");
be_pushint(vm, size);
be_call(vm, 1); /* call bytes() constructor with pre-sized buffer */
be_pop(vm, 1); /* bytes() instance is at top */

/* read back the actual buffer size */
be_getmember(vm, -1, ".size");
int32_t bytes_size = be_toint(vm, -1);
be_pop(vm, 1);
if (bytes_size < (int32_t)size) {
be_raise(vm, "memory_error", "could not allocated buffer");
}

be_getmember(vm, -1, "resize");
be_pushvalue(vm, -2);
be_pushint(vm, size);
Expand Down