From c4a7117704e6548a880b50fbc499e524bfed5823 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 22 Aug 2018 17:45:45 -0700 Subject: [PATCH] Ensure operator does not try to take ownership of string. --- include/custom.h | 3 ++- src/custom.c | 11 ++++++++--- src/libabacus.c | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/include/custom.h b/include/custom.h index 60ef731..1e6aafb 100644 --- a/include/custom.h +++ b/include/custom.h @@ -135,8 +135,9 @@ void libab_behavior_free(libab_behavior* behavior); * @param associativity the associativity (left = -1, right = 1) of the * operator. * @param function the function this operator represents. + * @result the result of the initialization. */ -void libab_operator_init(libab_operator* op, libab_operator_variant variant, +libab_result libab_operator_init(libab_operator* op, libab_operator_variant variant, int precedence, int associativity, const char* function); /** * Frees the given operator. diff --git a/src/custom.c b/src/custom.c index de1ce1f..01882a3 100644 --- a/src/custom.c +++ b/src/custom.c @@ -1,4 +1,5 @@ #include "custom.h" +#include "util.h" void libab_behavior_init_internal(libab_behavior* behavior, libab_function_ptr func) { @@ -26,16 +27,20 @@ void libab_behavior_free(libab_behavior* behavior) { } } -void libab_operator_init(libab_operator* op, libab_operator_variant variant, +libab_result libab_operator_init(libab_operator* op, libab_operator_variant variant, int precedence, int associativity, const char* function) { + libab_result result = LIBAB_SUCCESS; + char* into; op->variant = variant; op->precedence = precedence; op->associativity = associativity; - op->function = function; + result = libab_copy_string(&into, function); + op->function = into; + return result; } void libab_operator_free(libab_operator* op) { - + free((char*) op->function); } libab_result _function_init(libab_function* function, libab_ref* scope) { diff --git a/src/libabacus.c b/src/libabacus.c index f0a95a9..58029aa 100644 --- a/src/libabacus.c +++ b/src/libabacus.c @@ -102,7 +102,7 @@ libab_result _register_operator(libab* ab, const char* op, if ((new_entry = malloc(sizeof(*new_entry)))) { new_entry->variant = ENTRY_OP; new_operator = &(new_entry->data_u.op); - libab_operator_init(new_operator, token_type, precedence, associativity, + result = libab_operator_init(new_operator, token_type, precedence, associativity, function); } else { result = LIBAB_MALLOC;