Skip to content

Commit

Permalink
Merge pull request #438 from s-hadinger/fix_bytes_resize
Browse files Browse the repository at this point in the history
Fix bytes.resize()
  • Loading branch information
skiars authored Aug 23, 2024
2 parents d9b67c0 + d82adf4 commit 7819c5e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/be_byteslib.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,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);
Expand Down
20 changes: 11 additions & 9 deletions src/be_filelib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/* 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 */
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);

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);
}
Expand Down

0 comments on commit 7819c5e

Please sign in to comment.