Skip to content

Commit

Permalink
Solve stack overflows
Browse files Browse the repository at this point in the history
  • Loading branch information
0xPxt committed Aug 19, 2024
1 parent 363e499 commit 2be9fa0
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 139 deletions.
24 changes: 8 additions & 16 deletions app/src/items.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static items_error_t items_storeChainId(uint8_t *items_idx);
static items_error_t items_storeUsingGas(uint8_t *items_idx);
static items_error_t items_checkTxLengths(uint8_t *items_idx);
static items_error_t items_storeHash(uint8_t *items_idx);
static items_error_t items_storeSignature(uint8_t *items_idx);
static items_error_t items_storeSignForAddr(uint8_t *items_idx);
static items_error_t items_storeGasItem(uint16_t json_token_index, uint8_t items_idx, uint8_t *unknown_capabitilies);
static items_error_t items_storeTransferItem(parsed_json_t *json_all, uint16_t transfer_token_index, uint8_t items_idx, uint8_t *num_of_transfers, uint8_t *unknown_capabitilies);
static items_error_t items_storeCrossTransferItem(parsed_json_t *json_all, uint16_t transfer_token_index, uint8_t items_idx, uint8_t *num_of_transfers, uint8_t *unknown_capabitilies);
Expand All @@ -41,7 +41,7 @@ static items_error_t items_storeUnknownItem(parsed_json_t *json_all, uint16_t tr

#define MAX_ITEM_LENGTH_TO_DISPLAY 1000 // TODO : Check other apps to find this number

item_array_t item_array;
item_array_t item_array = {0};

uint8_t hash[BLAKE2B_HASH_SIZE] = {0};
char base64_hash[44];
Expand All @@ -61,7 +61,6 @@ item_array_t *items_getItemArray() {
void items_storeItems() {
uint8_t items_idx = 0;
uint8_t unknown_capabitilies = 1;
parsed_json_t json_all = parser_getParserTxObj()->tx_json.json;

items_storeSigningTransaction(&items_idx);

Expand Down Expand Up @@ -89,7 +88,7 @@ void items_storeItems() {

items_storeHash(&items_idx);

items_storeSignature(&items_idx);
items_storeSignForAddr(&items_idx);

item_array.numOfItems = items_idx;
}
Expand Down Expand Up @@ -324,16 +323,9 @@ static items_error_t items_storeHash(uint8_t *items_idx) {
return items_ok;
}

static items_error_t items_storeSignature(uint8_t *items_idx) {
static items_error_t items_storeSignForAddr(uint8_t *items_idx) {
strcpy(item_array.items[*items_idx].key, "Sign for Address");
/*
Currently launching cpp tests, so this is not available
uint8_t address[32];
uint16_t address_size;
CHECK_ERROR(crypto_fillAddress(address, sizeof(address), &address_size));
snprintf(outVal, address_size + 1, "%s", address);
*/
item_array.toString[*items_idx] = items_hashToDisplayString;
item_array.toString[*items_idx] = items_signForAddrToDisplayString;
(*items_idx)++;

return items_ok;
Expand All @@ -342,10 +334,10 @@ static items_error_t items_storeSignature(uint8_t *items_idx) {
static items_error_t items_storeGasItem(uint16_t json_token_index, uint8_t items_idx, uint8_t *unknown_capabitilies) {
uint16_t token_index = 0;
uint16_t args_count = 0;
parsed_json_t json_all = parser_getParserTxObj()->tx_json.json;
parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json);

object_get_value(&json_all, json_token_index, "args", &token_index);
array_get_element_count(&json_all, token_index, &args_count);
object_get_value(json_all, json_token_index, "args", &token_index);
array_get_element_count(json_all, token_index, &args_count);

if (args_count > 0) {
snprintf(item_array.items[items_idx].key, sizeof(item_array.items[items_idx].key), "Unknown Capability %d", *unknown_capabitilies);
Expand Down
2 changes: 1 addition & 1 deletion app/src/items.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#include <zxmacros.h>
#include "zxtypes.h"
#include "parser_common.h"
#include "json_parser.h"
#include "items_defs.h"
#include "json_parser.h"

void items_initItems();
void items_storeItems();
Expand Down
80 changes: 45 additions & 35 deletions app/src/items_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@

#include "items_format.h"
#include "parser.h"
#include "crypto.h"

extern char base64_hash[44];

items_error_t items_stdToDisplayString(item_t item, char *outVal, uint16_t *outValLen) {
parsed_json_t json_all = parser_getParserTxObj()->tx_json.json;
parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json);
uint16_t item_token_index = item.json_token_index;

*outValLen = json_all.tokens[item_token_index].end - json_all.tokens[item_token_index].start + 1;
snprintf(outVal, *outValLen, "%s", json_all.buffer + json_all.tokens[item_token_index].start);
*outValLen = json_all->tokens[item_token_index].end - json_all->tokens[item_token_index].start + 1;
snprintf(outVal, *outValLen, "%s", json_all->buffer + json_all->tokens[item_token_index].start);

return items_ok;
}
Expand Down Expand Up @@ -73,10 +74,10 @@ items_error_t items_transferToDisplayString(item_t item, char *outVal, uint16_t
char from[65];
uint8_t from_len = 0;
uint16_t token_index = 0;
parsed_json_t json_all = parser_getParserTxObj()->tx_json.json;
parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json);
uint16_t item_token_index = item.json_token_index;

object_get_value(&json_all, item_token_index, "args", &token_index);
object_get_value(json_all, item_token_index, "args", &token_index);

PARSER_TO_ITEMS_ERROR(parser_arrayElementToString(token_index, 0, from, &from_len));

Expand All @@ -100,10 +101,10 @@ items_error_t items_crossTransferToDisplayString(item_t item, char *outVal, uint
char chain[3];
uint8_t chain_len = 0;
uint16_t token_index = 0;
parsed_json_t json_all = parser_getParserTxObj()->tx_json.json;
parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json);
uint16_t item_token_index = item.json_token_index;

object_get_value(&json_all, item_token_index, "args", &token_index);
object_get_value(json_all, item_token_index, "args", &token_index);

PARSER_TO_ITEMS_ERROR(parser_arrayElementToString(token_index, 0, from, &from_len));

Expand All @@ -122,13 +123,13 @@ items_error_t items_crossTransferToDisplayString(item_t item, char *outVal, uint
items_error_t items_rotateToDisplayString(item_t item, char *outVal, uint16_t *outValLen) {
uint16_t token_index = 0;
uint16_t item_token_index = item.json_token_index;
parsed_json_t json_all = parser_getParserTxObj()->tx_json.json;
parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json);

object_get_value(&json_all, item_token_index, "args", &token_index);
array_get_nth_element(&json_all, token_index, 0, &token_index);
object_get_value(json_all, item_token_index, "args", &token_index);
array_get_nth_element(json_all, token_index, 0, &token_index);

*outValLen = json_all.tokens[token_index].end - json_all.tokens[token_index].start + sizeof("\"\"");
snprintf(outVal, *outValLen, "\"%s\"", json_all.buffer + json_all.tokens[token_index].start);
*outValLen = json_all->tokens[token_index].end - json_all->tokens[token_index].start + sizeof("\"\"");
snprintf(outVal, *outValLen, "\"%s\"", json_all->buffer + json_all->tokens[token_index].start);

return items_ok;
}
Expand All @@ -138,18 +139,18 @@ items_error_t items_gasToDisplayString(__Z_UNUSED item_t item, char *outVal, uin
uint8_t gasLimit_len = 0;
char gasPrice[64];
uint8_t gasPrice_len = 0;
parsed_json_t json_all = parser_getParserTxObj()->tx_json.json;
parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json);
uint16_t item_token_index = item.json_token_index;
uint16_t meta_token_index = item_token_index;

parser_getJsonValue(&item_token_index, JSON_GAS_LIMIT);
gasLimit_len = json_all.tokens[item_token_index].end - json_all.tokens[item_token_index].start + 1;
snprintf(gasLimit, gasLimit_len, "%s", json_all.buffer + json_all.tokens[item_token_index].start);
gasLimit_len = json_all->tokens[item_token_index].end - json_all->tokens[item_token_index].start + 1;
snprintf(gasLimit, gasLimit_len, "%s", json_all->buffer + json_all->tokens[item_token_index].start);

item_token_index = meta_token_index;
parser_getJsonValue(&item_token_index, JSON_GAS_PRICE);
gasPrice_len = json_all.tokens[item_token_index].end - json_all.tokens[item_token_index].start + 1;
snprintf(gasPrice, gasPrice_len, "%s", json_all.buffer + json_all.tokens[item_token_index].start);
gasPrice_len = json_all->tokens[item_token_index].end - json_all->tokens[item_token_index].start + 1;
snprintf(gasPrice, gasPrice_len, "%s", json_all->buffer + json_all->tokens[item_token_index].start);

*outValLen = gasLimit_len + gasPrice_len + sizeof("at most ") + sizeof(" at price ");
snprintf(outVal, *outValLen, "at most %s at price %s", gasLimit, gasPrice);
Expand All @@ -163,17 +164,26 @@ items_error_t items_hashToDisplayString(__Z_UNUSED item_t item, char *outVal, ui
return items_ok;
}

items_error_t items_signForAddrToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t *outValLen) {
uint8_t address[65];
uint16_t address_size;
CHECK_ERROR(crypto_fillAddress(address, sizeof(address), &address_size));
*outValLen = address_size;
snprintf(outVal, address_size, "%s", address);
return items_ok;
}

items_error_t items_unknownCapabilityToDisplayString(item_t item, char *outVal, uint16_t *outValLen) {
uint16_t token_index = 0;
uint16_t args_count = 0;
uint8_t len = 0;
uint8_t outVal_idx= 0;
parsed_json_t json_all = parser_getParserTxObj()->tx_json.json;
parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json);
uint16_t item_token_index = item.json_token_index;

object_get_value(&json_all, item_token_index, "name", &token_index);
len = json_all.tokens[token_index].end - json_all.tokens[token_index].start + sizeof("name: ");
snprintf(outVal, len, "name: %s", json_all.buffer + json_all.tokens[token_index].start);
object_get_value(json_all, item_token_index, "name", &token_index);
len = json_all->tokens[token_index].end - json_all->tokens[token_index].start + sizeof("name: ");
snprintf(outVal, len, "name: %s", json_all->buffer + json_all->tokens[token_index].start);
outVal_idx += len;

// Remove null terminator
Expand All @@ -190,21 +200,21 @@ items_error_t items_unknownCapabilityToDisplayString(item_t item, char *outVal,
return items_ok;
}

object_get_value(&json_all, item_token_index, "args", &token_index);
array_get_element_count(&json_all, token_index, &args_count);
object_get_value(json_all, item_token_index, "args", &token_index);
array_get_element_count(json_all, token_index, &args_count);


if (args_count) {
uint16_t args_token_index = 0;
for (uint8_t i = 0; i < args_count - 1; i++) {
array_get_nth_element(&json_all, token_index, i, &args_token_index);
if (json_all.tokens[args_token_index].type == JSMN_STRING) {
array_get_nth_element(json_all, token_index, i, &args_token_index);
if (json_all->tokens[args_token_index].type == JSMN_STRING) {
// Strings go in between double quotes
len = json_all.tokens[args_token_index].end - json_all.tokens[args_token_index].start + sizeof("arg X: \"\",");
snprintf(outVal + outVal_idx, len, "arg %d: \"%s\",", i + 1, json_all.buffer + json_all.tokens[args_token_index].start);
len = json_all->tokens[args_token_index].end - json_all->tokens[args_token_index].start + sizeof("arg X: \"\",");
snprintf(outVal + outVal_idx, len, "arg %d: \"%s\",", i + 1, json_all->buffer + json_all->tokens[args_token_index].start);
} else {
len = json_all.tokens[args_token_index].end - json_all.tokens[args_token_index].start + sizeof("arg X: ,");
snprintf(outVal + outVal_idx, len, "arg %d: %s,", i + 1, json_all.buffer + json_all.tokens[args_token_index].start);
len = json_all->tokens[args_token_index].end - json_all->tokens[args_token_index].start + sizeof("arg X: ,");
snprintf(outVal + outVal_idx, len, "arg %d: %s,", i + 1, json_all->buffer + json_all->tokens[args_token_index].start);
}
outVal_idx += len;

Expand All @@ -213,13 +223,13 @@ items_error_t items_unknownCapabilityToDisplayString(item_t item, char *outVal,
}

// Last arg (without comma)
array_get_nth_element(&json_all, token_index, args_count - 1, &args_token_index);
if (json_all.tokens[args_token_index].type == JSMN_STRING) {
len = json_all.tokens[args_token_index].end - json_all.tokens[args_token_index].start + sizeof("arg X: \"\"");
snprintf(outVal + outVal_idx, len, "arg %d: \"%s\"", args_count, json_all.buffer + json_all.tokens[args_token_index].start);
array_get_nth_element(json_all, token_index, args_count - 1, &args_token_index);
if (json_all->tokens[args_token_index].type == JSMN_STRING) {
len = json_all->tokens[args_token_index].end - json_all->tokens[args_token_index].start + sizeof("arg X: \"\"");
snprintf(outVal + outVal_idx, len, "arg %d: \"%s\"", args_count, json_all->buffer + json_all->tokens[args_token_index].start);
} else {
len = json_all.tokens[args_token_index].end - json_all.tokens[args_token_index].start + sizeof("arg X: ");
snprintf(outVal + outVal_idx, len, "arg %d: %s", args_count, json_all.buffer + json_all.tokens[args_token_index].start);
len = json_all->tokens[args_token_index].end - json_all->tokens[args_token_index].start + sizeof("arg X: ");
snprintf(outVal + outVal_idx, len, "arg %d: %s", args_count, json_all->buffer + json_all->tokens[args_token_index].start);
}
outVal_idx += len;
} else {
Expand Down
1 change: 1 addition & 0 deletions app/src/items_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ items_error_t items_crossTransferToDisplayString(item_t item, char *outVal, uint
items_error_t items_rotateToDisplayString(item_t item, char *outVal, uint16_t *outValLen);
items_error_t items_gasToDisplayString(item_t item, char *outVal, uint16_t *outValLen);
items_error_t items_hashToDisplayString(item_t item, char *outVal, uint16_t *outValLen);
items_error_t items_signForAddrToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t *outValLen);
items_error_t items_unknownCapabilityToDisplayString(item_t item, char *outVal, uint16_t *outValLen);
37 changes: 14 additions & 23 deletions app/src/jsmn/jsmn.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser, jsmntok_t *tokens,
return NULL;
}
tok = &tokens[parser->toknext++];
tok->start = tok->end = -1;
tok->size = 0;
tok->start = tok->end = 0xFFFF;
#ifdef JSMN_PARENT_LINKS
tok->parent = -1;
tok->parent = 0xFFFF;
#endif
return tok;
}
Expand All @@ -49,7 +48,6 @@ static void jsmn_fill_token(jsmntok_t *token, const jsmntype_t type,
token->type = type;
token->start = start;
token->end = end;
token->size = 0;
}

/**
Expand Down Expand Up @@ -211,15 +209,14 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
if (token == NULL) {
return JSMN_ERROR_NOMEM;
}
if (parser->toksuper != -1) {
jsmntok_t *t = &tokens[parser->toksuper];
if (parser->toksuper != 0xFFFF) {
#ifdef JSMN_STRICT
/* In strict mode an object or array can't become a key */
jsmntok_t *t = &tokens[parser->toksuper];
if (t->type == JSMN_OBJECT) {
return JSMN_ERROR_INVAL;
}
#endif
t->size++;
#ifdef JSMN_PARENT_LINKS
token->parent = parser->toksuper;
#endif
Expand All @@ -240,16 +237,16 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
}
token = &tokens[parser->toknext - 1];
for (;;) {
if (token->start != -1 && token->end == -1) {
if (token->start != 0xFFFF && token->end == 0xFFFF) {
if (token->type != type) {
return JSMN_ERROR_INVAL;
}
token->end = parser->pos + 1;
parser->toksuper = token->parent;
break;
}
if (token->parent == -1) {
if (token->type != type || parser->toksuper == -1) {
if (token->parent == 0xFFFF) {
if (token->type != type || parser->toksuper == 0xFFFF) {
return JSMN_ERROR_INVAL;
}
break;
Expand All @@ -259,7 +256,7 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
#else
for (i = parser->toknext - 1; i >= 0; i--) {
token = &tokens[i];
if (token->start != -1 && token->end == -1) {
if (token->start != 0xFFFF && token->end == 0xFFFF) {
if (token->type != type) {
return JSMN_ERROR_INVAL;
}
Expand All @@ -274,7 +271,7 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
}
for (; i >= 0; i--) {
token = &tokens[i];
if (token->start != -1 && token->end == -1) {
if (token->start != 0xFFFF && token->end == 0xFFFF) {
parser->toksuper = i;
break;
}
Expand All @@ -287,9 +284,6 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
return r;
}
count++;
if (parser->toksuper != -1 && tokens != NULL) {
tokens[parser->toksuper].size++;
}
break;
case '\t':
case '\r':
Expand All @@ -300,15 +294,15 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
parser->toksuper = parser->toknext - 1;
break;
case ',':
if (tokens != NULL && parser->toksuper != -1 &&
if (tokens != NULL && parser->toksuper != 0xFFFF &&
tokens[parser->toksuper].type != JSMN_ARRAY &&
tokens[parser->toksuper].type != JSMN_OBJECT) {
#ifdef JSMN_PARENT_LINKS
parser->toksuper = tokens[parser->toksuper].parent;
#else
for (i = parser->toknext - 1; i >= 0; i--) {
if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) {
if (tokens[i].start != -1 && tokens[i].end == -1) {
if (tokens[i].start != 0xFFFF && tokens[i].end == 0xFFFF) {
parser->toksuper = i;
break;
}
Expand All @@ -334,10 +328,10 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
case 'f':
case 'n':
/* And they must not be keys of the object */
if (tokens != NULL && parser->toksuper != -1) {
if (tokens != NULL && parser->toksuper != 0xFFFF) {
const jsmntok_t *t = &tokens[parser->toksuper];
if (t->type == JSMN_OBJECT ||
(t->type == JSMN_STRING && t->size != 0)) {
(t->type == JSMN_STRING && (t->end - t->start) != 0)) {
return JSMN_ERROR_INVAL;
}
}
Expand All @@ -350,9 +344,6 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
return r;
}
count++;
if (parser->toksuper != -1 && tokens != NULL) {
tokens[parser->toksuper].size++;
}
break;

#ifdef JSMN_STRICT
Expand All @@ -366,7 +357,7 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
if (tokens != NULL) {
for (i = parser->toknext - 1; i >= 0; i--) {
/* Unmatched opened object or array */
if (tokens[i].start != -1 && tokens[i].end == -1) {
if (tokens[i].start != 0xFFFF && tokens[i].end == 0xFFFF) {
return JSMN_ERROR_PART;
}
}
Expand Down
Loading

1 comment on commit 2be9fa0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-format reports: 15 file(s) not formatted
  • app/src/items_defs.h
  • app/src/coin.h
  • app/src/items_format.h
  • app/src/items.h
  • app/src/parser_txdef.h
  • app/src/parser_impl.h
  • app/src/jsmn/jsmn.h
  • app/src/common/parser_common.h
  • app/src/json/json_parser.h
  • app/src/items_format.c
  • app/src/parser_impl.c
  • app/src/items.c
  • app/src/parser.c
  • app/src/jsmn/jsmn.c
  • app/src/json/json_parser.c

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.