Browse code

Errors are treated as false during the expression evaluation unless the operator is DIFF_OP.

Miklos Tirpak authored on 23/04/2008 09:31:44
Showing 1 changed files
... ...
@@ -46,6 +46,8 @@
46 46
  *  2006-02-06  added named route tables (andrei)
47 47
  *  2008-04-14  (expr1 != expr2) is evaluated true if at least one of
48 48
  *		the expressions does not exist (Miklos)
49
+ *  2008-04-23  errors are treated as false during expression evaluation
50
+ *  		unless the operator is DIFF_OP (Miklos)
49 51
  */
50 52
 
51 53
 
... ...
@@ -620,8 +622,7 @@ inline static int comp_str(int op, str* left, int rtype, union exp_op* r, struct
620 620
 		else return (op == DIFF_OP);
621 621
 	} else if (rtype == SELECT_ST) {
622 622
 		ret = run_select(&v, r->select, msg);
623
-		if (ret > 0) return (op == DIFF_OP); /* Not found */
624
-		else if (ret < 0) goto error; /* Error */
623
+		if (ret != 0) return (op == DIFF_OP); /* Not found or error */
625 624
 		right = &v;
626 625
 	} else if ((op == MATCH_OP && rtype == RE_ST)) {
627 626
 	} else if (op != MATCH_OP && rtype == STRING_ST) {
... ...
@@ -698,7 +699,7 @@ inline static int comp_str(int op, str* left, int rtype, union exp_op* r, struct
698 698
 	return ret;
699 699
 
700 700
 error:
701
-	return -1;
701
+	return (op == DIFF_OP) ? 1 : -1;
702 702
 }
703 703
 
704 704
 
... ...
@@ -799,26 +800,28 @@ inline static int comp_avp(int op, avp_spec_t* spec, int rtype, union exp_op* r,
799 799
 				tmp.s=r->string;
800 800
 				tmp.len=strlen(r->string);
801 801
 				if (str2int(&tmp, &uval)<0){
802
-					LOG(L_ERR, "ERROR: comp_avp: cannot convert string value"
802
+					LOG(L_WARN, "WARNING: comp_avp: cannot convert string value"
803 803
 								" to int (%s)\n", ZSW(r->string));
804
-					return -1;
804
+					goto error;
805 805
 				}
806 806
 				num_val.numval=uval;
807 807
 				return comp_num(op, val.n, NUMBER_ST, &num_val);
808 808
 			case STR_ST:
809 809
 				if (str2int(&r->str, &uval)<0){
810
-					LOG(L_ERR, "ERROR: comp_avp: cannot convert str value"
810
+					LOG(L_WARN, "WARNING: comp_avp: cannot convert str value"
811 811
 								" to int (%.*s)\n", r->str.len, ZSW(r->str.s));
812
-					return -1;
812
+					goto error;
813 813
 				}
814 814
 				num_val.numval=uval;
815 815
 				return comp_num(op, val.n, NUMBER_ST, &num_val);
816 816
 			default:
817 817
 				LOG(L_CRIT, "BUG: comp_avp: invalid type for numeric avp "
818 818
 							"comparison (%d)\n", rtype);
819
-				return -1;
819
+				goto error;
820 820
 		}
821 821
 	}
822
+error:
823
+	return (op == DIFF_OP) ? 1 : -1;
822 824
 }
823 825
 
824 826
 /*
... ...
@@ -831,8 +834,7 @@ inline static int comp_select(int op, select_t* sel, int rtype, union exp_op* r,
831 831
 	char empty_str=0;
832 832
 
833 833
 	ret = run_select(&val, sel, msg);
834
-	if (ret < 0) return -1;
835
-	if (ret > 0) return (op == DIFF_OP);
834
+	if (ret != 0) return (op == DIFF_OP);
836 835
 
837 836
 	switch(op) {
838 837
 	case NO_OP: return (val.len>0);
... ...
@@ -860,7 +862,7 @@ inline static int check_self_op(int op, str* s, unsigned short p)
860 860
 		case EQUAL_OP:
861 861
 			break;
862 862
 		case DIFF_OP:
863
-			if (ret>=0) ret=!ret;
863
+			ret=(ret > 0) ? 0 : 1;
864 864
 			break;
865 865
 		default:
866 866
 			LOG(L_CRIT, "BUG: check_self_op: invalid operator %d\n", op);
... ...
@@ -930,8 +932,7 @@ inline static int comp_ip(int op, struct ip_addr* ip, int rtype, union exp_op* r
930 930
 					}
931 931
 					break;
932 932
 				case DIFF_OP:
933
-					ret=comp_ip(EQUAL_OP, ip, rtype, r);
934
-					if (ret>=0) ret=!ret;
933
+					ret=(comp_ip(EQUAL_OP, ip, rtype, r) > 0) ? 0 : 1;
935 934
 					break;
936 935
 				default:
937 936
 					goto error_op;
... ...
@@ -1165,12 +1166,12 @@ inline static int eval_elem(struct run_act_ctx* h, struct expr* e,
1165 1165
 	}
1166 1166
 	return ret;
1167 1167
 error:
1168
-	return -1;
1168
+	return (e->op == DIFF_OP) ? 1 : -1;
1169 1169
 }
1170 1170
 
1171 1171
 
1172 1172
 
1173
-/* ret= 0/1 (true/false) ,  -1 on error */
1173
+/* ret= 1/0 (true/false) ,  -1 on error (evaluates as false)*/
1174 1174
 int eval_expr(struct run_act_ctx* h, struct expr* e, struct sip_msg* msg)
1175 1175
 {
1176 1176
 	int ret;
... ...
@@ -1182,19 +1183,18 @@ int eval_expr(struct run_act_ctx* h, struct expr* e, struct sip_msg* msg)
1182 1182
 			case LOGAND_OP:
1183 1183
 				ret=eval_expr(h, e->l.expr, msg);
1184 1184
 				/* if error or false stop evaluating the rest */
1185
-				if (ret!=1) break;
1185
+				if (ret <= 0) break;
1186 1186
 				ret=eval_expr(h, e->r.expr, msg); /*ret1 is 1*/
1187 1187
 				break;
1188 1188
 			case LOGOR_OP:
1189 1189
 				ret=eval_expr(h, e->l.expr, msg);
1190
-				/* if true or error stop evaluating the rest */
1191
-				if (ret!=0) break;
1190
+				/* if true stop evaluating the rest */
1191
+				if (ret > 0) break;
1192 1192
 				ret=eval_expr(h, e->r.expr, msg); /* ret1 is 0 */
1193 1193
 				break;
1194 1194
 			case NOT_OP:
1195 1195
 				ret=eval_expr(h, e->l.expr, msg);
1196
-				if (ret<0) break;
1197
-				ret= ! ret;
1196
+				ret=(ret > 0) ? 0 : 1;
1198 1197
 				break;
1199 1198
 			default:
1200 1199
 				LOG(L_CRIT, "BUG: eval_expr: unknown op %d\n", e->op);