fix some double free, memory leaks, and seg faults
This commit is contained in:
@@ -1,21 +1,21 @@
|
||||
#include "operations.h"
|
||||
#include "../parser.h"
|
||||
#include "../../memory.h"
|
||||
#include "../parser.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
ParsedValue *convert_to_operation(DArray * to_operate_on, DArray * operations) {
|
||||
ParsedValue *convert_to_operation(DArray *to_operate_on, DArray *operations) {
|
||||
if (to_operate_on->size == 1) {
|
||||
return darray_get(to_operate_on, 0);
|
||||
}
|
||||
TokenType operation = 0;
|
||||
DArray positions;
|
||||
for (size_t i = 0; i<operations->size;i++) {
|
||||
TokenType * current_operation = darray_get(operations, i);
|
||||
for (size_t i = 0; i < operations->size; i++) {
|
||||
TokenType *current_operation = darray_get(operations, i);
|
||||
if (operation < *current_operation) {
|
||||
if (operation!=0) {
|
||||
if (operation != 0) {
|
||||
darray_free(&positions, NULL);
|
||||
}
|
||||
operation = *current_operation;
|
||||
@@ -23,22 +23,30 @@ ParsedValue *convert_to_operation(DArray * to_operate_on, DArray * operations) {
|
||||
}
|
||||
darray_push(&positions, &i);
|
||||
}
|
||||
size_t last_position = operations->size-1;
|
||||
darray_push(&positions, &last_position);
|
||||
ParsedValue * parsedValue = checked_malloc(sizeof(ParsedValue));
|
||||
parsedValue->type = AST_OPERATION;
|
||||
ParsedOperation * operationStruct = checked_malloc(sizeof(ParsedOperation));
|
||||
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
||||
parsedValue->type = AST_OPERATION;
|
||||
ParsedOperation *operationStruct = checked_malloc(sizeof(ParsedOperation));
|
||||
parsedValue->data = operationStruct;
|
||||
operationStruct->operation = operation;
|
||||
darray_init(&operationStruct->to_operate_on, sizeof(ParsedValue));
|
||||
last_position = 0;
|
||||
for (size_t i = 0; i<positions.size;i++) {
|
||||
size_t last_position = 0;
|
||||
size_t to_operate_on_last_position = 0;
|
||||
for (size_t i = 0; i < positions.size; i++) {
|
||||
size_t *position = darray_get(&positions, i);
|
||||
DArray to_operate_on_slice = darray_slice(to_operate_on, last_position, *position+1);
|
||||
DArray operations_slice = darray_slice(operations, last_position, *position);
|
||||
darray_push(&operationStruct->to_operate_on, convert_to_operation(&to_operate_on_slice, &operations_slice));
|
||||
last_position = *position;
|
||||
DArray to_operate_on_slice = darray_slice(
|
||||
to_operate_on, to_operate_on_last_position, (*position) + 1);
|
||||
DArray operations_slice =
|
||||
darray_slice(operations, last_position, *position);
|
||||
darray_push(&operationStruct->to_operate_on,
|
||||
convert_to_operation(&to_operate_on_slice, &operations_slice));
|
||||
last_position = (*position);
|
||||
to_operate_on_last_position = (*position) + 1;
|
||||
}
|
||||
DArray to_operate_on_slice =
|
||||
darray_slice(to_operate_on, to_operate_on_last_position, to_operate_on->size);
|
||||
DArray operations_slice = darray_slice(operations, last_position, operations->size);
|
||||
darray_push(&operationStruct->to_operate_on,
|
||||
convert_to_operation(&to_operate_on_slice, &operations_slice));
|
||||
darray_free(&positions, NULL);
|
||||
return parsedValue;
|
||||
}
|
||||
@@ -48,6 +56,7 @@ ParsedValue *parse_operations(char *file, DArray *tokens, size_t *index,
|
||||
DArray to_operate_on;
|
||||
darray_init(&to_operate_on, sizeof(ParsedValue));
|
||||
darray_push(&to_operate_on, first_parsed_value);
|
||||
free(first_parsed_value);
|
||||
|
||||
DArray operations;
|
||||
darray_init(&operations, sizeof(TokenType));
|
||||
@@ -66,7 +75,10 @@ ParsedValue *parse_operations(char *file, DArray *tokens, size_t *index,
|
||||
darray_push(&operations, &token->type);
|
||||
(*index)++;
|
||||
error_if_finished(file, tokens, index);
|
||||
darray_push(&to_operate_on, parse_token_full(file, tokens, index, true, false));
|
||||
ParsedValue *parsedValue =
|
||||
parse_token_full(file, tokens, index, true, false);
|
||||
darray_push(&to_operate_on, parsedValue);
|
||||
free(parsedValue);
|
||||
}
|
||||
ParsedValue *output = convert_to_operation(&to_operate_on, &operations);
|
||||
darray_free(&to_operate_on, NULL);
|
||||
|
||||
Reference in New Issue
Block a user