From a328bc00bfa3e3574cc995c9fd80717f74c9cb4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20L=2E=20=C5=A0ijanec?= Date: Sat, 16 May 2020 21:23:54 +0200 Subject: critical OOM bug fix --- a.out | Bin 27096 -> 27104 bytes src/bvrcommands.c | 30 +++++++++++++++++++++++------- src/tape.c | 2 +- test/tape-test.bvr | 5 ++++- tmp/output.htm | 4 +++- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/a.out b/a.out index 44004e8..108e083 100755 Binary files a/a.out and b/a.out differ diff --git a/src/bvrcommands.c b/src/bvrcommands.c index 3d54cd0..fbb476b 100644 --- a/src/bvrcommands.c +++ b/src/bvrcommands.c @@ -7,27 +7,40 @@ #include #include #include -int bvr_commands_check_for_command(char * input_char, char ** value, int *i, FILE * input) { +int bvr_commands_check_for_command(char * input_char, char ** value, int *i, FILE * input, int * value_size) { if((*input_char) == LINE_COMMAND_CHAR) { FILE * command_return; size_t buf_size; - command_return = open_memstream(&(*value)+((*i)++), &buf_size); // i bajtov smo že napisali. + char * command_output; + command_return = open_memstream(&command_output, &buf_size); if(bvr_command_processor(input, command_return) != SUCCESS) { fprintf(stderr, "[bvrcommands.c] bvr_commands_check_for_command: command, passed as argument, didn't return success. Going on.\n"); } fflush(command_return); - (*i) = (*i)+buf_size; + while((*value_size) < ((*i)+buf_size)) { // make room in chunks for command_output + (*value) = realloc((*value), (*value_size)+BVR_VALUE_CHUNK_SIZE); + if((*value) == NULL) { + fprintf(stderr, "[bvrcommands.c] bvr_commands_check_for_command: CRITICAL OUT-OF-MEMORY.\n"); + } + (*value_size) = (*value_size)+BVR_VALUE_CHUNK_SIZE; + } + for(int j = 0; j < buf_size; j++) { + /// fprintf(stderr, "debug: \"%c\", %ld\n", command_output[j], buf_size); + (*value)[(*i)++] = command_output[j]; + } + (*input_char) = CLOSING_COMMAND_TAG_CHAR_1; // da zaključimo loop (drugače ostane notri ?) //\\ zelo slabo. znak, ki ga najde izveden ukaz, se izgubi. rešitev bi bila dobiti zadnji input char, ki triggera break // prejšnjega ukaza, kar pa je nemogoče. fflush(command_return); fclose(command_return); - // fprintf(stderr, "debug: \"%s\"\n", (*value)); + free(command_output); + command_output == NULL; // the address/pointer, lost for ever! return BVR_CONTINUE; } return BVR_KEEPGOING; } -char bvr_var_skip_separator_chars(FILE * input) { +char bvr_commands_skip_separator_chars(FILE * input) { char input_char = fgetc(input); while(input_char == ' ' || input_char == CLOSING_COMMAND_TAG_CHAR_1 || input_char == ',' || input_char == ';' || input_char == EOF || input_char == '\0' || input_char == '\n') { @@ -38,19 +51,22 @@ char bvr_var_skip_separator_chars(FILE * input) { char * bvr_commands_get_value(FILE * input, char * yeetus_chars) { int value_size = BVR_VALUE_CHUNK_SIZE; char * value = (char*) malloc(value_size); - char input_char = bvr_var_skip_separator_chars(input); + char input_char = bvr_commands_skip_separator_chars(input); int i = 0; while(1) { // i == napisali smo že toliko znakov if(i >= value_size) { // <-- todo: uncomment after done debugging value = realloc(value, (value_size) * BVR_VALUE_CHUNK_SIZE); + if(value == NULL) { + fprintf(stderr, "[bvrcommands.c] bvr_commands_get_value: CRITICAL OUT-OF-MEMORY, FUCK!\n"); + } value_size = value_size + BVR_VALUE_CHUNK_SIZE; } if(char_in_array(input_char, yeetus_chars)) { value[i++] = '\0'; return value; // or yeet! } - if(bvr_commands_check_for_command(&(input_char), &(value), &(i), input) == BVR_CONTINUE) { + if(bvr_commands_check_for_command(&(input_char), &(value), &(i), input, &(value_size)) == BVR_CONTINUE) { continue; } (value)[(i)++] = input_char; diff --git a/src/tape.c b/src/tape.c index e405001..eba1478 100644 --- a/src/tape.c +++ b/src/tape.c @@ -37,7 +37,7 @@ int bvr_command_processor(FILE * page_source_file, FILE * temp_output_file) { command_handler_output = bvr_handle_info(page_source_file, temp_output_file); break; default: - fprintf(stderr, "[tape.c] bvr_command_processor: unknown command %c\n", command_entered); + fprintf(stderr, "[tape.c] bvr_command_processor: unknown command %c (dec: %d)\n", command_entered, command_entered); fprintf(temp_output_file, "\nbVerbose unknown command %c\n", command_entered); } if(command_handler_output != SUCCESS) { diff --git a/test/tape-test.bvr b/test/tape-test.bvr index 85e9f58..a89012c 100644 --- a/test/tape-test.bvr +++ b/test/tape-test.bvr @@ -1,3 +1,6 @@ <@?i assets/content/global.bvr @> <@?s testing krneki@> -<@?u -2 -1 ?g testing @> +# <@?u -2 -1 ?g testing @> +<@?s branch_name .git/refs/heads/master@> +<@?s latest_commit ?u 0 -1 ?i ?g branch_name@> +<@?g latest_commit@> diff --git a/tmp/output.htm b/tmp/output.htm index 57a8f14..ebeb0c0 100644 --- a/tmp/output.htm +++ b/tmp/output.htm @@ -17,4 +17,6 @@ -k + + +be2a485d3240007afe26c1335b56a4f3cbb91841 -- cgit v1.2.3