Skip to content

Commit

Permalink
fixed crash in rwbuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
ypopovych committed May 1, 2024
1 parent 74a6dff commit c00229c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"env": {
"BOLOS_SDK": "~/.ledger/sdk",
"BOLOS_SDK": "~/.ledger/nanos-sdk",
"ARM_GCC": "~/.ledger/gcc-arm-none-eabi-13.2-2023.10"
},
"configurations": [
Expand Down
27 changes: 27 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "gdb",
"request": "attach",
"name": "Attach to gdbserver",
"executable": "${workspaceFolder}/bin/app.elf",
"target": ":1234",
"remote": true,
"cwd": "${workspaceFolder}",
"valuesFormatting": "parseText",
"gdbpath": "gdb-multiarch",
"autorun": [
"set substitute-path /project ${workspaceFolder}",
"set architecture arm",
"set backtrace limit 15",
"handle SIGILL nostop pass noprint",
"add-symbol-file ${workspaceFolder}/bin/app.elf 0x40000000",
"b *0x40000000"
]
}
]
}
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ VARIANT_VALUES = ERGO
# Enabling DEBUG flag will enable PRINTF and disable optimizations
#DEBUG = 1

# Enabling stack canary
HAVE_BOLOS_APP_STACK_CANARY = 1

########################################
# Application custom permissions #
########################################
Expand Down
9 changes: 9 additions & 0 deletions src/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ void app_main() {
continue;
}

PRINTF("=> CLA=%02X | INS=%02X | P1=%02X | P2=%02X | Lc=%02X | CData=%.*H\n",
cmd.cla,
cmd.ins,
cmd.p1,
cmd.p2,
cmd.lc,
cmd.lc,
cmd.data);

// Dispatch structured APDU command to handler
if (apdu_dispatcher(&cmd) < 0) {
PRINTF("=> apdu_dispatcher failure\n");
Expand Down
18 changes: 9 additions & 9 deletions src/common/rwbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ bool rw_buffer_write_u32(rw_buffer_t *buffer, uint32_t value, endianness_t endia
}

if (endianness == BE) {
write_u32_be((uint8_t *) buffer->read.ptr, buffer->size, value);
write_u32_be((uint8_t *) buffer->read.ptr, buffer->read.size, value);
} else {
write_u32_le((uint8_t *) buffer->read.ptr, buffer->size, value);
write_u32_le((uint8_t *) buffer->read.ptr, buffer->read.size, value);
}

rw_buffer_seek_write_cur(buffer, 4);
Expand All @@ -80,9 +80,9 @@ bool rw_buffer_write_u64(rw_buffer_t *buffer, uint64_t value, endianness_t endia
}

if (endianness == BE) {
write_u64_be((uint8_t *) buffer->read.ptr, buffer->size, value);
write_u64_be((uint8_t *) buffer->read.ptr, buffer->read.size, value);
} else {
write_u64_le((uint8_t *) buffer->read.ptr, buffer->size, value);
write_u64_le((uint8_t *) buffer->read.ptr, buffer->read.size, value);
}

rw_buffer_seek_write_cur(buffer, 8);
Expand All @@ -95,17 +95,17 @@ bool rw_buffer_write_bytes(rw_buffer_t *buffer, const uint8_t *from, size_t from
return false;
}

memmove((uint8_t *) buffer->read.ptr + buffer->size, from, from_len);
memmove(rw_buffer_write_ptr(buffer), from, from_len);

rw_buffer_seek_write_cur(buffer, from_len);

return true;
}

void rw_buffer_shift_data(rw_buffer_t *buffer) {
if (buffer->read.offset == 0) return;
if (rw_buffer_read_position(buffer) == 0) return;
size_t data_len = rw_buffer_data_len(buffer);
memmove((uint8_t *) buffer->read.ptr, buffer->read.ptr + buffer->read.offset, data_len);
buffer->read.size = data_len;
buffer->read.offset = 0;
memmove((uint8_t *) buffer->read.ptr, rw_buffer_read_ptr(buffer), data_len);
rw_buffer_seek_read_set(buffer, 0);
rw_buffer_seek_write_set(buffer, data_len);
}

0 comments on commit c00229c

Please sign in to comment.