Skip to content

Commit

Permalink
Fixed issue when parsing multiple commands in a row - changelist buff…
Browse files Browse the repository at this point in the history
…er is now properly zeroed (#6)
  • Loading branch information
0xMMBD committed Oct 24, 2023
1 parent da4de46 commit 1df1703
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
7 changes: 6 additions & 1 deletion libsol/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ ifeq ($(COVERAGE),1)
CFLAGS += --coverage
endif

debug_CFLAGS = -g -fsanitize=address -fsanitize=undefined
# Temporary solution to ignore conditional compilation for nanos
ifeq ($(TEST_REDUCE_MEMORY),)
CFLAGS += -DTEST_MEMORY_EXTENDED
endif

debug_CFLAGS = -g -O0 -fsanitize=address -fsanitize=undefined
release_CFLAGS = -O2

libsol_source_files = $(filter-out %_test.c,$(wildcard *.c))
Expand Down
7 changes: 4 additions & 3 deletions libsol/include/sol/siws.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
#include <stdbool.h>
#include <stdint.h>
#include "parser.h"
#include "../util.h"

#if defined(SDK_TARGET_NANOX) || defined(SDK_TARGET_NANOS2) || defined(SDK_TARGET_STAX)
#if EXTENDED_MEMORY
//Arbitrary number - we don't expect more than 10 resources
// It is impossible to predict precisely how many user will use
#define RESOURCES_MAX_LENGTH 10
Expand Down Expand Up @@ -34,8 +35,8 @@ typedef struct SiwsInternalChangelistWrapper{

extern SiwsInternalChangelistWrapper G_changelist_wrapper;

//@TODO create better types for these fields
//For now we assume that every field is string-like

//For now, we assume that every field is string-like
typedef struct SiwsMessage {
const char* domain;
const char* address;
Expand Down
27 changes: 20 additions & 7 deletions libsol/siws.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ const char valid_chain_ids[7][15] = {
/**
* Append new modification to the changelist
*/
void siws_changelist_append(char delimiter_used, char* delimiter_ptr){
static int siws_changelist_append(char delimiter_used, char* delimiter_ptr){
SiwsInternalChangelistWrapper *changelist = &G_changelist_wrapper;
SiwsInternalChangelist* changeListItem = &changelist->changelist[changelist->number_of_changes++];
changeListItem->delimiter_replaced = delimiter_used;
changeListItem->parser_offset = (uint8_t*) delimiter_ptr;

if(changelist->number_of_changes >= (RESOURCES_MAX_LENGTH + SIWS_FIELDS_COUNT)){
return 1;
}

SiwsInternalChangelist* changelist_item = &changelist->changelist[changelist->number_of_changes++];
changelist_item->delimiter_replaced = delimiter_used;
changelist_item->parser_offset = (uint8_t*) delimiter_ptr;

return 0;
}

static int parse_to_char(Parser* parser, char delimiter_char, const char** value){
Expand All @@ -41,7 +48,7 @@ static int parse_to_char(Parser* parser, char delimiter_char, const char** value
//Delimiter char not found till the end of the buffer
if(delimiter_char_location != NULL){
*delimiter_char_location = '\0';//Mark string end
siws_changelist_append(delimiter_char, delimiter_char_location);
BAIL_IF(siws_changelist_append(delimiter_char, delimiter_char_location));
unsigned int advance_length = strlen(*value) + 1;
advance(parser, advance_length);
}else{
Expand All @@ -59,7 +66,10 @@ void siws_changelist_rollback(){
SiwsInternalChangelistWrapper *changelist = &G_changelist_wrapper;
for(int i = 0; i < changelist->number_of_changes; ++i){
SiwsInternalChangelist* changelist_item = &changelist->changelist[i];
*changelist_item->parser_offset = changelist_item->delimiter_replaced;
if(changelist_item->parser_offset == NULL){
continue;
}
*(changelist_item->parser_offset) = changelist_item->delimiter_replaced;
}
}

Expand Down Expand Up @@ -299,7 +309,7 @@ static int internal_parse_siws_message(Parser* parser, SiwsMessage* message){

//FIXME - remove this when memory issues will be resolved
//NanoS will not parse and display advanced fields (including statement)
#if !defined(SDK_TARGET_NANOS)
#if EXTENDED_MEMORY
//Minimal required message consists of domain and address. Check if further parsing is required
if(parser->buffer_length > 1){
//Skip empty line
Expand Down Expand Up @@ -334,5 +344,8 @@ int parse_siws_message(Parser* parser, SiwsMessage* message){
siws_changelist_rollback();
}

//Reset changelist state
explicit_bzero(&G_changelist_wrapper, sizeof(SiwsInternalChangelistWrapper));

return result;
}
15 changes: 8 additions & 7 deletions libsol/siws_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "include/sol/parser.h"
#include <assert.h>
#include <stdio.h>
#include "util.h"


void test_validate_chain_id_valid(){
const char* chain_id = "testnet";
Expand Down Expand Up @@ -170,7 +172,7 @@ void test_parse_siws_message_valid_resources_location(){
assert(strcmp(siws_msg.domain, "localhost:3001") == 0);
assert(strcmp(siws_msg.address, "9Pr7yXpVtAVVzEivf8aUNAPDLXXmiHFsMshgjJSySGpn") == 0);
assert(strcmp(siws_msg.statement, "Clicking Sign or Approve only means you have proved this wallet is owned by you. This request will not trigger any blockchain transaction or cost any gas fee.") == 0);
assert(*siws_msg.version == '1');
assert(siws_msg.version != NULL && *siws_msg.version == '1');
assert(strcmp(siws_msg.chain_id, "mainnet") == 0);
assert(strcmp(siws_msg.request_id, "2137") == 0);
assert(strcmp(siws_msg.not_before, "2023-10-13 13:33:00") == 0);
Expand Down Expand Up @@ -611,16 +613,16 @@ void test_parse_siws_message_invalid_required_fields_no_space(){


int main() {
test_validate_chain_id_valid();
test_is_alphanumeric_invalid();
test_validate_chain_id_invalid();

test_is_alphanumeric_valid();
test_is_alphanumeric_max_length();
test_is_alphanumeric_invalid();
test_validate_chain_id_valid();
test_validate_chain_id_invalid();

test_parse_siws_message_valid();
test_parse_siws_message_valid_resources_location();
test_parse_siws_message_valid_minimal_message();
test_parse_siws_message_valid_resources_location();
test_parse_siws_message_valid_full_resources_table();
test_parse_siws_message_invalid_minimal_message();
test_parse_siws_message_invalid_version_verify_rollback();
test_parse_siws_message_invalid_nonce_1();
Expand All @@ -633,7 +635,6 @@ int main() {
test_parse_siws_message_invalid_resources_prefix_1();
test_parse_siws_message_invalid_resources_prefix_2();
test_parse_siws_message_invalid_required_fields_no_space();
test_parse_siws_message_valid_full_resources_table();

printf("passed\n");
return 0;
Expand Down
9 changes: 8 additions & 1 deletion libsol/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@

#define assert_pubkey_equal(actual, expected) assert(memcmp(actual, expected, 32) == 0)

#if defined(SDK_TARGET_NANOX) || defined(SDK_TARGET_NANOS2) || defined(SDK_TARGET_STAX) || defined(TEST_MEMORY_EXTENDED)
#define EXTENDED_MEMORY 1
#else
#define EXTENDED_MEMORY 0
#endif


#ifndef UNUSED
#define UNUSED(x) (void) x
#define UNUSED(x) (void) x
#endif

0 comments on commit 1df1703

Please sign in to comment.