Ensure operator does not try to take ownership of string.

This commit is contained in:
Danila Fedorin 2018-08-22 17:45:45 -07:00
parent 899ac31210
commit c4a7117704
3 changed files with 11 additions and 5 deletions

View File

@ -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.

View File

@ -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) {

View File

@ -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;