Skip to content

Commit

Permalink
Merge pull request #108 from LedgerHQ/assert
Browse files Browse the repository at this point in the history
Assert
  • Loading branch information
sgliner-ledger authored Dec 12, 2023
2 parents 58f7b5d + 82977fb commit e81bf62
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,7 @@ ENABLE_NBGL_QRCODE = 1
#DISABLE_STANDARD_USB = 1
#DISABLE_STANDARD_WEBUSB = 1
#DISABLE_STANDARD_BAGL_UX_FLOW = 1
#DISABLE_DEBUG_LEDGER_ASSERT = 1
#DISABLE_DEBUG_THROW = 1

include $(BOLOS_SDK)/Makefile.standard_app
3 changes: 3 additions & 0 deletions src/address.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "os.h"
#include "cx.h"
#include "ledger_assert.h"

#include "address.h"

Expand All @@ -31,6 +32,8 @@ bool address_from_pubkey(const uint8_t public_key[static 65], uint8_t *out, size
uint8_t address[32] = {0};
cx_sha3_t keccak256;

LEDGER_ASSERT(out != NULL, "NULL out");

if (out_len < ADDRESS_LEN) {
return false;
}
Expand Down
3 changes: 3 additions & 0 deletions src/apdu/dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "buffer.h"
#include "io.h"
#include "ledger_assert.h"

#include "dispatcher.h"
#include "../constants.h"
Expand All @@ -32,6 +33,8 @@
#include "../handler/sign_tx.h"

int apdu_dispatcher(const command_t *cmd) {
LEDGER_ASSERT(cmd != NULL, "NULL cmd");

if (cmd->cla != CLA) {
return io_send_sw(SW_CLA_NOT_SUPPORTED);
}
Expand Down
10 changes: 10 additions & 0 deletions src/transaction/deserialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@
#include "utils.h"
#include "types.h"

#if defined(TEST) || defined(FUZZ)
#include "assert.h"
#define LEDGER_ASSERT(x, y) assert(x)
#else
#include "ledger_assert.h"
#endif

parser_status_e transaction_deserialize(buffer_t *buf, transaction_t *tx) {
LEDGER_ASSERT(buf != NULL, "NULL buf");
LEDGER_ASSERT(tx != NULL, "NULL tx");

if (buf->size > MAX_TX_LEN) {
return WRONG_LENGTH_ERROR;
}
Expand Down
10 changes: 10 additions & 0 deletions src/transaction/serialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,19 @@

#include "serialize.h"

#if defined(TEST) || defined(FUZZ)
#include "assert.h"
#define LEDGER_ASSERT(x, y) assert(x)
#else
#include "ledger_assert.h"
#endif

int transaction_serialize(const transaction_t *tx, uint8_t *out, size_t out_len) {
size_t offset = 0;

LEDGER_ASSERT(tx != NULL, "NULL tx");
LEDGER_ASSERT(out != NULL, "NULL out");

if (8 + ADDRESS_LEN + 8 + varint_size(tx->memo_len) + tx->memo_len > out_len) {
return -1;
}
Expand Down
12 changes: 12 additions & 0 deletions src/transaction/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@
#include <stdbool.h> // bool
#include <string.h> // memmove

#if defined(TEST) || defined(FUZZ)
#include "assert.h"
#define LEDGER_ASSERT(x, y) assert(x)
#else
#include "ledger_assert.h"
#endif

#include "types.h"

bool transaction_utils_check_encoding(const uint8_t *memo, uint64_t memo_len) {
LEDGER_ASSERT(memo != NULL, "NULL memo");

for (uint64_t i = 0; i < memo_len; i++) {
if (memo[i] > 0x7F) {
return false;
Expand All @@ -35,6 +44,9 @@ bool transaction_utils_format_memo(const uint8_t *memo,
uint64_t memo_len,
char *dst,
uint64_t dst_len) {
LEDGER_ASSERT(memo != NULL, "NULL memo");
LEDGER_ASSERT(dst != NULL, "NULL dst");

if (memo_len > MAX_MEMO_LEN || dst_len < memo_len + 1) {
return false;
}
Expand Down

0 comments on commit e81bf62

Please sign in to comment.