comparison lwlib/lw_expr.c @ 434:052c5f335a92

Fix bug in like terms collection in expression simplification Like term collection would lose the actual "variable" part of the term if the second term collected happened to have no coefficient. This would cause the expression to take the value of the calculated coefficient which is obviously wrong. Thanks to hider <stego@satx.rr.com> for reporting the bug and providing a proper test case. Observation: this bug has been present since the first pre-release of lwtools 3.0 when the algebraic expression system was introduced. Apparently people tend not to create expressions that trigger the like terms handler. The specific conditions require the symbol to be undefined and the second operand to the addition has to have no coefficient so it's likely a fairly rare scenario. Still, it is somewhat surprising that nobody tripped on it before now.
author William Astle <lost@l-w.ca>
date Mon, 23 Jan 2017 22:58:36 -0700
parents 6153cb49403c
children 000381ee2d5c
comparison
equal deleted inserted replaced
433:b1adf549d181 434:052c5f335a92
619 if (E -> type == lw_expr_type_var && evaluate_var) 619 if (E -> type == lw_expr_type_var && evaluate_var)
620 { 620 {
621 lw_expr_t te; 621 lw_expr_t te;
622 622
623 te = evaluate_var(E -> value2, priv); 623 te = evaluate_var(E -> value2, priv);
624 if (!te)
625 return;
624 if (lw_expr_contains(te, E)) 626 if (lw_expr_contains(te, E))
625 lw_expr_destroy(te); 627 lw_expr_destroy(te);
626 else if (te) 628 else if (te)
627 { 629 {
628 for (o = E -> operands; o; o = o -> next) 630 for (o = E -> operands; o; o = o -> next)
944 lw_expr_add_operand(e1, e2); 946 lw_expr_add_operand(e1, e2);
945 lw_expr_destroy(e2); 947 lw_expr_destroy(e2);
946 } 948 }
947 lw_expr_destroy(o -> p); 949 lw_expr_destroy(o -> p);
948 o -> p = e1; 950 o -> p = e1;
949 for (o = o2 -> p -> operands; o; o = o -> next) 951 if (o2 -> p -> type == lw_expr_type_oper)
950 { 952 {
951 if (o -> p -> type == lw_expr_type_int) 953 for (o = o2 -> p -> operands; o; o = o -> next)
952 continue; 954 {
953 lw_expr_add_operand(e1, o -> p); 955 if (o -> p -> type == lw_expr_type_int)
956 continue;
957 lw_expr_add_operand(e1, o -> p);
958 }
959 }
960 else
961 {
962 lw_expr_add_operand(e1, o2 -> p);
954 } 963 }
955 lw_expr_destroy(o2 -> p); 964 lw_expr_destroy(o2 -> p);
956 o2 -> p = lw_expr_build(lw_expr_type_int, 0); 965 o2 -> p = lw_expr_build(lw_expr_type_int, 0);
957 goto again; 966 goto again;
958 } 967 }