@@ -82,12 +82,15 @@ typedef struct state {
82
82
#define IS_CLOSURE (TYPE ) (((TYPE) & TE_CLOSURE0) != 0)
83
83
#define ARITY (TYPE ) ( ((TYPE) & (TE_FUNCTION0 | TE_CLOSURE0)) ? ((TYPE) & 0x00000007) : 0 )
84
84
#define NEW_EXPR (type , ...) new_expr((type), (const te_expr*[]){__VA_ARGS__})
85
+ #define CHECK_NULL (ptr , ...) if ((ptr) == NULL) { __VA_ARGS__; return NULL; }
85
86
86
87
static te_expr * new_expr (const int type , const te_expr * parameters []) {
87
88
const int arity = ARITY (type );
88
89
const int psize = sizeof (void * ) * arity ;
89
90
const int size = (sizeof (te_expr ) - sizeof (void * )) + psize + (IS_CLOSURE (type ) ? sizeof (void * ) : 0 );
90
91
te_expr * ret = malloc (size );
92
+ CHECK_NULL (ret );
93
+
91
94
memset (ret , 0 , size );
92
95
if (arity && parameters ) {
93
96
memcpy (ret -> parameters , parameters , psize );
@@ -312,19 +315,25 @@ static te_expr *base(state *s) {
312
315
switch (TYPE_MASK (s -> type )) {
313
316
case TOK_NUMBER :
314
317
ret = new_expr (TE_CONSTANT , 0 );
318
+ CHECK_NULL (ret );
319
+
315
320
ret -> value = s -> value ;
316
321
next_token (s );
317
322
break ;
318
323
319
324
case TOK_VARIABLE :
320
325
ret = new_expr (TE_VARIABLE , 0 );
326
+ CHECK_NULL (ret );
327
+
321
328
ret -> bound = s -> bound ;
322
329
next_token (s );
323
330
break ;
324
331
325
332
case TE_FUNCTION0 :
326
333
case TE_CLOSURE0 :
327
334
ret = new_expr (s -> type , 0 );
335
+ CHECK_NULL (ret );
336
+
328
337
ret -> function = s -> function ;
329
338
if (IS_CLOSURE (s -> type )) ret -> parameters [0 ] = s -> context ;
330
339
next_token (s );
@@ -341,10 +350,13 @@ static te_expr *base(state *s) {
341
350
case TE_FUNCTION1 :
342
351
case TE_CLOSURE1 :
343
352
ret = new_expr (s -> type , 0 );
353
+ CHECK_NULL (ret );
354
+
344
355
ret -> function = s -> function ;
345
356
if (IS_CLOSURE (s -> type )) ret -> parameters [1 ] = s -> context ;
346
357
next_token (s );
347
358
ret -> parameters [0 ] = power (s );
359
+ CHECK_NULL (ret -> parameters [0 ], te_free (ret ));
348
360
break ;
349
361
350
362
case TE_FUNCTION2 : case TE_FUNCTION3 : case TE_FUNCTION4 :
@@ -354,6 +366,8 @@ static te_expr *base(state *s) {
354
366
arity = ARITY (s -> type );
355
367
356
368
ret = new_expr (s -> type , 0 );
369
+ CHECK_NULL (ret );
370
+
357
371
ret -> function = s -> function ;
358
372
if (IS_CLOSURE (s -> type )) ret -> parameters [arity ] = s -> context ;
359
373
next_token (s );
@@ -365,6 +379,8 @@ static te_expr *base(state *s) {
365
379
for (i = 0 ; i < arity ; i ++ ) {
366
380
next_token (s );
367
381
ret -> parameters [i ] = expr (s );
382
+ CHECK_NULL (ret -> parameters [i ], te_free (ret ));
383
+
368
384
if (s -> type != TOK_SEP ) {
369
385
break ;
370
386
}
@@ -381,6 +397,8 @@ static te_expr *base(state *s) {
381
397
case TOK_OPEN :
382
398
next_token (s );
383
399
ret = list (s );
400
+ CHECK_NULL (ret );
401
+
384
402
if (s -> type != TOK_CLOSE ) {
385
403
s -> type = TOK_ERROR ;
386
404
} else {
@@ -390,6 +408,8 @@ static te_expr *base(state *s) {
390
408
391
409
default :
392
410
ret = new_expr (0 , 0 );
411
+ CHECK_NULL (ret );
412
+
393
413
s -> type = TOK_ERROR ;
394
414
ret -> value = NAN ;
395
415
break ;
@@ -412,7 +432,12 @@ static te_expr *power(state *s) {
412
432
if (sign == 1 ) {
413
433
ret = base (s );
414
434
} else {
415
- ret = NEW_EXPR (TE_FUNCTION1 | TE_FLAG_PURE , base (s ));
435
+ te_expr * b = base (s );
436
+ CHECK_NULL (b );
437
+
438
+ ret = NEW_EXPR (TE_FUNCTION1 | TE_FLAG_PURE , b );
439
+ CHECK_NULL (ret , te_free (b ));
440
+
416
441
ret -> function = negate ;
417
442
}
418
443
@@ -423,6 +448,7 @@ static te_expr *power(state *s) {
423
448
static te_expr * factor (state * s ) {
424
449
/* <factor> = <power> {"^" <power>} */
425
450
te_expr * ret = power (s );
451
+ CHECK_NULL (ret );
426
452
427
453
int neg = 0 ;
428
454
@@ -441,19 +467,33 @@ static te_expr *factor(state *s) {
441
467
442
468
if (insertion ) {
443
469
/* Make exponentiation go right-to-left. */
444
- te_expr * insert = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , insertion -> parameters [1 ], power (s ));
470
+ te_expr * p = power (s );
471
+ CHECK_NULL (p , te_free (ret ));
472
+
473
+ te_expr * insert = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , insertion -> parameters [1 ], p );
474
+ CHECK_NULL (insert , te_free (p ), te_free (ret ));
475
+
445
476
insert -> function = t ;
446
477
insertion -> parameters [1 ] = insert ;
447
478
insertion = insert ;
448
479
} else {
449
- ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , power (s ));
480
+ te_expr * p = power (s );
481
+ CHECK_NULL (p , te_free (ret ));
482
+
483
+ te_expr * prev = ret ;
484
+ ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , p );
485
+ CHECK_NULL (ret , te_free (p ), te_free (prev ));
486
+
450
487
ret -> function = t ;
451
488
insertion = ret ;
452
489
}
453
490
}
454
491
455
492
if (neg ) {
493
+ te_expr * prev = ret ;
456
494
ret = NEW_EXPR (TE_FUNCTION1 | TE_FLAG_PURE , ret );
495
+ CHECK_NULL (ret , te_free (prev ));
496
+
457
497
ret -> function = negate ;
458
498
}
459
499
@@ -463,11 +503,18 @@ static te_expr *factor(state *s) {
463
503
static te_expr * factor (state * s ) {
464
504
/* <factor> = <power> {"^" <power>} */
465
505
te_expr * ret = power (s );
506
+ CHECK_NULL (ret );
466
507
467
508
while (s -> type == TOK_INFIX && (s -> function == pow )) {
468
509
te_fun2 t = s -> function ;
469
510
next_token (s );
470
- ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , power (s ));
511
+ te_expr * p = power (s );
512
+ CHECK_NULL (p , te_free (ret ));
513
+
514
+ te_expr * prev = ret ;
515
+ ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , p );
516
+ CHECK_NULL (ret , te_free (p ), te_free (prev ));
517
+
471
518
ret -> function = t ;
472
519
}
473
520
@@ -480,11 +527,18 @@ static te_expr *factor(state *s) {
480
527
static te_expr * term (state * s ) {
481
528
/* <term> = <factor> {("*" | "/" | "%") <factor>} */
482
529
te_expr * ret = factor (s );
530
+ CHECK_NULL (ret );
483
531
484
532
while (s -> type == TOK_INFIX && (s -> function == mul || s -> function == divide || s -> function == fmod )) {
485
533
te_fun2 t = s -> function ;
486
534
next_token (s );
487
- ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , factor (s ));
535
+ te_expr * f = factor (s );
536
+ CHECK_NULL (f , te_free (ret ));
537
+
538
+ te_expr * prev = ret ;
539
+ ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , f );
540
+ CHECK_NULL (ret , te_free (f ), te_free (prev ));
541
+
488
542
ret -> function = t ;
489
543
}
490
544
@@ -495,11 +549,18 @@ static te_expr *term(state *s) {
495
549
static te_expr * expr (state * s ) {
496
550
/* <expr> = <term> {("+" | "-") <term>} */
497
551
te_expr * ret = term (s );
552
+ CHECK_NULL (ret );
498
553
499
554
while (s -> type == TOK_INFIX && (s -> function == add || s -> function == sub )) {
500
555
te_fun2 t = s -> function ;
501
556
next_token (s );
502
- ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , term (s ));
557
+ te_expr * te = term (s );
558
+ CHECK_NULL (te , te_free (ret ));
559
+
560
+ te_expr * prev = ret ;
561
+ ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , te );
562
+ CHECK_NULL (ret , te_free (te ), te_free (prev ));
563
+
503
564
ret -> function = t ;
504
565
}
505
566
@@ -510,10 +571,17 @@ static te_expr *expr(state *s) {
510
571
static te_expr * list (state * s ) {
511
572
/* <list> = <expr> {"," <expr>} */
512
573
te_expr * ret = expr (s );
574
+ CHECK_NULL (ret );
513
575
514
576
while (s -> type == TOK_SEP ) {
515
577
next_token (s );
516
- ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , expr (s ));
578
+ te_expr * e = expr (s );
579
+ CHECK_NULL (e , te_free (ret ));
580
+
581
+ te_expr * prev = ret ;
582
+ ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , e );
583
+ CHECK_NULL (ret , te_free (e ), te_free (prev ));
584
+
517
585
ret -> function = comma ;
518
586
}
519
587
@@ -602,6 +670,10 @@ te_expr *te_compile(const char *expression, const te_variable *variables, int va
602
670
603
671
next_token (& s );
604
672
te_expr * root = list (& s );
673
+ if (root == NULL ) {
674
+ if (error ) * error = -1 ;
675
+ return NULL ;
676
+ }
605
677
606
678
if (s .type != TOK_END ) {
607
679
te_free (root );
@@ -620,6 +692,10 @@ te_expr *te_compile(const char *expression, const te_variable *variables, int va
620
692
621
693
double te_interp (const char * expression , int * error ) {
622
694
te_expr * n = te_compile (expression , 0 , 0 , error );
695
+ if (n == NULL ) {
696
+ return NAN ;
697
+ }
698
+
623
699
double ret ;
624
700
if (n ) {
625
701
ret = te_eval (n );
0 commit comments