@cryptotaxi247 / netdata-1 / commits / 1864746e2

detect excess characters at expression

Costa Tsaousis committed Aug 11, 2016 at 16:36 UTC 1864746e20a8695ca86bf105dc037709730240d0
3 files changed +20 -8
profile/test-eval.c
+1 -1
@@ -270,7 +270,7 @@ int main(int argc, char **argv) {
270
271 EVAL_EXPRESSION *exp = expression_parse(argv[1], &failed_at, &error);
272 if(!exp)
273 - printf("\nFAILED\nExpression: '%s'\nParsing stopped at: '%s'\nError code: %d (%s)\n", argv[1], (failed_at)?failed_at:"<NONE>", error, expression_strerror(error));
273 + printf("\nFAILED\nExpression: '%s'\nParsing stopped at: '%s'\nError code: %d (%s)\n", argv[1], (failed_at)?((*failed_at)?failed_at:"<END OF EXPRESSION>"):"<NONE>", error, expression_strerror(error));
274
275 else {
276 printf("\nOK\nExpression: '%s'\nParsed as : '%s'\nError code: %d (%s)\n", argv[1], exp->parsed_as, error, expression_strerror(error));
src/eval.c
+13 -2
@@ -695,7 +695,7 @@ static inline EVAL_NODE *parse_one_full_operand(const char **string, int *error)
695 op1->operator = EVAL_OPERATOR_VALUE;
696 eval_node_set_value_to_constant(op1, 0, number);
697 }
698 - else if(*string)
698 + else if(**string)
699 *error = EVAL_ERROR_UNKNOWN_OPERAND;
700 else
701 *error = EVAL_ERROR_MISSING_OPERAND;
@@ -741,7 +741,7 @@ static inline EVAL_NODE *parse_rest_of_expression(const char **string, int *erro
741
742 return parse_rest_of_expression(string, error, op);
743 }
744 - else if(**string == EVAL_OPERATOR_EXPRESSION_CLOSE) {
744 + else if(**string == ')') {
745 ;
746 }
747 else if(**string) {
@@ -794,6 +794,14 @@ EVAL_EXPRESSION *expression_parse(const char *string, const char **failed_at, in
794 s = string;
795 EVAL_NODE *op = parse_full_expression(&s, &err);
796
797 + if(*s) {
798 + if(op) {
799 + eval_node_free(op);
800 + op = NULL;
801 + }
802 + err = EVAL_ERROR_REMAINING_GARBAGE;
803 + }
804 +
805 if (failed_at) *failed_at = s;
806 if (error) *error = err;
807
@@ -852,6 +860,9 @@ const char *expression_strerror(int error) {
860 case EVAL_ERROR_MISSING_OPERATOR:
861 return "expected operator";
862
863 + case EVAL_ERROR_REMAINING_GARBAGE:
864 + return "remaining characters after expression";
865 +
866 case EVAL_ERROR_INVALID_VALUE:
867 return "invalid value structure - internal error";
868
src/eval.h
+6 -5
@@ -35,13 +35,14 @@ typedef struct eval_expression {
35 #define EVAL_ERROR_UNKNOWN_OPERAND 2
36 #define EVAL_ERROR_MISSING_OPERAND 3
37 #define EVAL_ERROR_MISSING_OPERATOR 4
38 +#define EVAL_ERROR_REMAINING_GARBAGE 5
39
40 // evaluation errors
40 -#define EVAL_ERROR_INVALID_VALUE 5
41 -#define EVAL_ERROR_INVALID_NUMBER_OF_OPERANDS 6
42 -#define EVAL_ERROR_VALUE_IS_NAN 7
43 -#define EVAL_ERROR_VALUE_IS_INFINITE 8
44 -#define EVAL_ERROR_UNKNOWN_VARIABLE 9
41 +#define EVAL_ERROR_INVALID_VALUE 11
42 +#define EVAL_ERROR_INVALID_NUMBER_OF_OPERANDS 12
43 +#define EVAL_ERROR_VALUE_IS_NAN 13
44 +#define EVAL_ERROR_VALUE_IS_INFINITE 14
45 +#define EVAL_ERROR_UNKNOWN_VARIABLE 15
46
47 // parse the given string as an expression and return:
48 // a pointer to an expression if it parsed OK