Skip to content

Handle malloc failures #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 83 additions & 7 deletions tinyexpr.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,15 @@ typedef struct state {
#define IS_CLOSURE(TYPE) (((TYPE) & TE_CLOSURE0) != 0)
#define ARITY(TYPE) ( ((TYPE) & (TE_FUNCTION0 | TE_CLOSURE0)) ? ((TYPE) & 0x00000007) : 0 )
#define NEW_EXPR(type, ...) new_expr((type), (const te_expr*[]){__VA_ARGS__})
#define CHECK_NULL(ptr, ...) if ((ptr) == NULL) { __VA_ARGS__; return NULL; }

static te_expr *new_expr(const int type, const te_expr *parameters[]) {
const int arity = ARITY(type);
const int psize = sizeof(void*) * arity;
const int size = (sizeof(te_expr) - sizeof(void*)) + psize + (IS_CLOSURE(type) ? sizeof(void*) : 0);
te_expr *ret = malloc(size);
CHECK_NULL(ret);

memset(ret, 0, size);
if (arity && parameters) {
memcpy(ret->parameters, parameters, psize);
Expand Down Expand Up @@ -305,19 +308,25 @@ static te_expr *base(state *s) {
switch (TYPE_MASK(s->type)) {
case TOK_NUMBER:
ret = new_expr(TE_CONSTANT, 0);
CHECK_NULL(ret);

ret->value = s->value;
next_token(s);
break;

case TOK_VARIABLE:
ret = new_expr(TE_VARIABLE, 0);
CHECK_NULL(ret);

ret->bound = s->bound;
next_token(s);
break;

case TE_FUNCTION0:
case TE_CLOSURE0:
ret = new_expr(s->type, 0);
CHECK_NULL(ret);

ret->function = s->function;
if (IS_CLOSURE(s->type)) ret->parameters[0] = s->context;
next_token(s);
Expand All @@ -334,10 +343,13 @@ static te_expr *base(state *s) {
case TE_FUNCTION1:
case TE_CLOSURE1:
ret = new_expr(s->type, 0);
CHECK_NULL(ret);

ret->function = s->function;
if (IS_CLOSURE(s->type)) ret->parameters[1] = s->context;
next_token(s);
ret->parameters[0] = power(s);
CHECK_NULL(ret->parameters[0], te_free(ret));
break;

case TE_FUNCTION2: case TE_FUNCTION3: case TE_FUNCTION4:
Expand All @@ -347,6 +359,8 @@ static te_expr *base(state *s) {
arity = ARITY(s->type);

ret = new_expr(s->type, 0);
CHECK_NULL(ret);

ret->function = s->function;
if (IS_CLOSURE(s->type)) ret->parameters[arity] = s->context;
next_token(s);
Expand All @@ -358,6 +372,8 @@ static te_expr *base(state *s) {
for(i = 0; i < arity; i++) {
next_token(s);
ret->parameters[i] = expr(s);
CHECK_NULL(ret->parameters[i], te_free(ret));

if(s->type != TOK_SEP) {
break;
}
Expand All @@ -374,6 +390,8 @@ static te_expr *base(state *s) {
case TOK_OPEN:
next_token(s);
ret = list(s);
CHECK_NULL(ret);

if (s->type != TOK_CLOSE) {
s->type = TOK_ERROR;
} else {
Expand All @@ -383,6 +401,8 @@ static te_expr *base(state *s) {

default:
ret = new_expr(0, 0);
CHECK_NULL(ret);

s->type = TOK_ERROR;
ret->value = NAN;
break;
Expand All @@ -405,7 +425,12 @@ static te_expr *power(state *s) {
if (sign == 1) {
ret = base(s);
} else {
ret = NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, base(s));
te_expr *b = base(s);
CHECK_NULL(b);

ret = NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, b);
CHECK_NULL(ret, te_free(b));

ret->function = negate;
}

Expand All @@ -416,6 +441,7 @@ static te_expr *power(state *s) {
static te_expr *factor(state *s) {
/* <factor> = <power> {"^" <power>} */
te_expr *ret = power(s);
CHECK_NULL(ret);

int neg = 0;

Expand All @@ -434,19 +460,33 @@ static te_expr *factor(state *s) {

if (insertion) {
/* Make exponentiation go right-to-left. */
te_expr *insert = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, insertion->parameters[1], power(s));
te_expr *p = power(s);
CHECK_NULL(p, te_free(ret));

te_expr *insert = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, insertion->parameters[1], p);
CHECK_NULL(insert, te_free(p), te_free(ret));

insert->function = t;
insertion->parameters[1] = insert;
insertion = insert;
} else {
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, power(s));
te_expr *p = power(s);
CHECK_NULL(p, te_free(ret));

te_expr *prev = ret;
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, p);
CHECK_NULL(ret, te_free(p), te_free(prev));

ret->function = t;
insertion = ret;
}
}

if (neg) {
te_expr *prev = ret;
ret = NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, ret);
CHECK_NULL(ret, te_free(prev));

ret->function = negate;
}

Expand All @@ -456,11 +496,18 @@ static te_expr *factor(state *s) {
static te_expr *factor(state *s) {
/* <factor> = <power> {"^" <power>} */
te_expr *ret = power(s);
CHECK_NULL(ret);

while (s->type == TOK_INFIX && (s->function == pow)) {
te_fun2 t = s->function;
next_token(s);
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, power(s));
te_expr *p = power(s);
CHECK_NULL(p, te_free(ret));

te_expr *prev = ret;
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, p);
CHECK_NULL(ret, te_free(p), te_free(prev));

ret->function = t;
}

Expand All @@ -473,11 +520,18 @@ static te_expr *factor(state *s) {
static te_expr *term(state *s) {
/* <term> = <factor> {("*" | "/" | "%") <factor>} */
te_expr *ret = factor(s);
CHECK_NULL(ret);

while (s->type == TOK_INFIX && (s->function == mul || s->function == divide || s->function == fmod)) {
te_fun2 t = s->function;
next_token(s);
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, factor(s));
te_expr *f = factor(s);
CHECK_NULL(f, te_free(ret));

te_expr *prev = ret;
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, f);
CHECK_NULL(ret, te_free(f), te_free(prev));

ret->function = t;
}

Expand All @@ -488,11 +542,18 @@ static te_expr *term(state *s) {
static te_expr *expr(state *s) {
/* <expr> = <term> {("+" | "-") <term>} */
te_expr *ret = term(s);
CHECK_NULL(ret);

while (s->type == TOK_INFIX && (s->function == add || s->function == sub)) {
te_fun2 t = s->function;
next_token(s);
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, term(s));
te_expr *te = term(s);
CHECK_NULL(te, te_free(ret));

te_expr *prev = ret;
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, te);
CHECK_NULL(ret, te_free(te), te_free(prev));

ret->function = t;
}

Expand All @@ -503,10 +564,17 @@ static te_expr *expr(state *s) {
static te_expr *list(state *s) {
/* <list> = <expr> {"," <expr>} */
te_expr *ret = expr(s);
CHECK_NULL(ret);

while (s->type == TOK_SEP) {
next_token(s);
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, expr(s));
te_expr *e = expr(s);
CHECK_NULL(e, te_free(ret));

te_expr *prev = ret;
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, e);
CHECK_NULL(ret, te_free(e), te_free(prev));

ret->function = comma;
}

Expand Down Expand Up @@ -595,6 +663,10 @@ te_expr *te_compile(const char *expression, const te_variable *variables, int va

next_token(&s);
te_expr *root = list(&s);
if (root == NULL) {
if (error) *error = -1;
return NULL;
}

if (s.type != TOK_END) {
te_free(root);
Expand All @@ -613,6 +685,10 @@ te_expr *te_compile(const char *expression, const te_variable *variables, int va

double te_interp(const char *expression, int *error) {
te_expr *n = te_compile(expression, 0, 0, error);
if (n == NULL) {
return NAN;
}

double ret;
if (n) {
ret = te_eval(n);
Expand Down