/*
** Splint - annotation-assisted static program checker
-** Copyright (C) 1994-2002 University of Virginia,
+** Copyright (C) 1994-2003 University of Virginia,
** Massachusetts Institute of Technology
**
** This program is free software; you can redistribute it and/or modify it
# include "exprChecks.h"
# include "exprNodeSList.h"
-
/*@access constraint, exprNode @*/ /*!!! NO! Don't do this so recklessly - design your code more carefully so you don't need to! */
-/*@-nullderef@*/ /* !!! DRL needs to fix this code! */
-/*@-nullstate@*/ /* !!! DRL needs to fix this code! */
-/*@-nullpass@*/ /* !!! DRL needs to fix this code! */
-/*@-temptrans@*/ /* !!! DRL needs to fix this code! */
-
-
-
-static constraint inequalitySubstitute (/*@returned@*/ constraint p_c, constraintList p_p);
-
-
+static constraint inequalitySubstitute (/*@returned@*/ constraint p_c, constraintList p_p);
static bool rangeCheck (arithType p_ar1, /*@observer@*/ constraintExpr p_expr1, arithType p_ar2, /*@observer@*/ constraintExpr p_expr2);
-static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint p_c, constraintList p_p);
-
-static constraint inequalitySubstituteStrong (/*@returned@*/ constraint p_c, constraintList p_p);
+static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint p_c, constraintList p_p);
+static constraint inequalitySubstituteStrong (/*@returned@*/ constraint p_c, constraintList p_p);
static constraint constraint_searchandreplace (/*@returned@*/ constraint p_c, constraintExpr p_old, constraintExpr p_newExpr);
-
static constraint constraint_addOr (/*@returned@*/ constraint p_orig, /*@observer@*/ constraint p_orConstr);
static bool resolveOr (/*@temp@*/constraint p_c, /*@observer@*/ /*@temp@*/ constraintList p_list);
static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList p_pre2, constraintList p_post1);
-/*********************************************/
-
-
-
/*@only@*/ constraintList constraintList_mergeEnsuresFreeFirst (constraintList list1, constraintList list2)
{
DPRINTF((message (" children: %s and %s", exprNode_unparse (child1), exprNode_unparse(child2) ) ) );
+
+ if (exprNode_isUndefined(parent) )
+ {
+ llassert (exprNode_isDefined(parent) );
+ return;
+ }
+
+
if (exprNode_isError (child1) || exprNode_isError(child2) )
{
if (exprNode_isError (child1) && !exprNode_isError(child2) )
) ) );
}
-
-
-
/*@only@*/ constraintList constraintList_subsumeEnsures (constraintList list1, constraintList list2)
{
return ret;
}
-
-
/* tries to resolve constraints in list pre2 using post1 */
static /*@only@*/ constraintList reflectChangesNoOr (/*@observer@*/ /*@temp@*/ constraintList pre2, /*@observer@*/ /*@temp@*/ constraintList post1)
static constraint constraint_addOr (/*@returned@*/ constraint orig, /*@observer@*/ constraint orConstr)
{
constraint c;
+
+ llassert(constraint_isDefined(orig) );
+
c = orig;
DPRINTF((message("constraint_addor: oring %s onto %s", constraint_printOr(orConstr), constraint_printOr(orig) ) ));
int numberOr;
numberOr = 0;
+
+ llassert(constraint_isDefined(c) );
+
DPRINTF(( message("resolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(list) ) ));
+
temp = c;
do
{
constraint temp;
- llassert(constraint_isUndefined (c->or ) );
+ llassert(constraint_isDefined (c ) );
DPRINTF((message("doResolve:: call on constraint c = : %q and constraintList %q",
constraint_printOr(c), constraintList_print(post1)
}
constraint_free(c);
- /*drl bee: pbr*/ *resolved = TRUE;
+ *resolved = TRUE;
return NULL;
}
constraint ret;
constraint next;
constraint curr;
-
DPRINTF(( message("doResolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(post1) ) ));
-
+ *resolved = FALSE;
- /*drl bee: pbr*/ *resolved = FALSE;
-
+ llassert(constraint_isDefined(c) );
ret = constraint_copy(c);
+ llassert(constraint_isDefined(ret) );
+
if (constraintList_isEmpty(post1) )
{
return ret;
curr = doResolve (curr, post1, resolved);
- /*drl bee: pbr*/ if (*resolved)
+ if (*resolved)
{
/* curr is null so we don't try to free it*/
llassert(curr == NULL);
static bool constraint_conflict (constraint c1, constraint c2)
{
-
- if (constraintExpr_similar(c1->lexpr, c2->lexpr) )
+ if (!constraint_isDefined(c1) || !constraint_isDefined(c2))
+ {
+ return FALSE;
+ }
+
+ if (constraintExpr_similar (c1->lexpr, c2->lexpr))
{
if (c1->ar == EQ)
if (c1->ar == c2->ar)
{
- DPRINTF ((message ("%s conflicts with %s ", constraint_print (c1), constraint_print(c2) ) ) );
+ DPRINTF (("%s conflicts with %s", constraint_unparse (c1), constraint_unparse (c2)));
return TRUE;
}
}
- /* This is a slight kludg to prevent circular constraints like
+ /* This is a slight kludge to prevent circular constraints like
strlen(str) == maxRead(s) + strlen(str);
*/
- /*@i324234*/ /* clean this up */
+ /*this code is functional but it may be worth cleaning up at some point. */
if (c1->ar == EQ)
if (c1->ar == c2->ar)
static void constraint_fixConflict (/*@temp@*/ constraint good, /*@temp@*/ /*@observer@*/ constraint conflicting) /*@modifies good@*/
{
- if (conflicting->ar ==EQ )
+ llassert(constraint_isDefined(conflicting) );
+
+ if (conflicting->ar == EQ)
{
+ llassert (constraint_isDefined(good));
+ DPRINTF (("Replacing here!"));
good->expr = constraintExpr_searchandreplace (good->expr, conflicting->lexpr, conflicting->expr);
good = constraint_simplify (good);
}
return ret;
}
-/*returns true if constraint post satifies cosntriant pre */
-static bool satifies (constraint pre, constraint post)
+/*returns true if constraint post satisfies cosntriant pre */
+
+static bool constraintResolve_satisfies (constraint pre, constraint post)
{
- if (constraint_isAlwaysTrue (pre) )
+ if (!constraint_isDefined (pre))
+ {
+ return TRUE;
+ }
+
+ if (!constraint_isDefined(post))
+ {
+ return FALSE;
+ }
+
+ if (constraint_isAlwaysTrue (pre))
return TRUE;
if (!constraintExpr_similar (pre->lexpr, post->lexpr) )
{
return FALSE;
}
+
if (constraintExpr_isUndefined(post->expr))
{
llassert(FALSE);
return FALSE;
}
-
+
return rangeCheck (pre->ar, pre->expr, post->ar, post->expr);
}
-bool constraintList_resolve (/*@temp@*/ /*@observer@*/ constraint c, /*@temp@*/ /*@observer@*/ constraintList p)
+bool constraintList_resolve (/*@temp@*/ /*@observer@*/ constraint c,
+ /*@temp@*/ /*@observer@*/ constraintList p)
{
+ DPRINTF (("[resolve] Trying to resolve constraint: %s using %s",
+ constraint_unparse (c),
+ constraintList_unparse (p)));
+
constraintList_elements (p, el)
{
- if ( satifies (c, el) )
+ if (constraintResolve_satisfies (c, el))
{
- DPRINTF ((message ("\n%s Satifies %s\n ", constraint_print(el), constraint_print(c) ) ) );
+ DPRINTF (("constraintList_resolve: %s satifies %s",
+ constraint_unparse (el), constraint_unparse (c)));
return TRUE;
}
- DPRINTF ((message ("\n%s does not satify %s\n ", constraint_print(el), constraint_print(c) ) ) );
+
+ DPRINTF (("constraintList_resolve: %s does not satify %s\n ",
+ constraint_unparse (el), constraint_unparse (c)));
}
end_constraintList_elements;
- DPRINTF ((message ("no constraints satify %s", constraint_print(c) ) ));
+
+ DPRINTF (("No constraints satify: %s", constraint_unparse (c)));
return FALSE;
}
{
case GTE:
case GT:
- if ((ar2 == GT) || (ar2 == GTE) || (ar2 == EQ) )
+ if ((ar2 == GT) || (ar2 == GTE) || (ar2 == EQ))
{
return TRUE;
}
case LT:
case LTE:
- if ((ar2 == LT) || (ar2 == LTE) || (ar2 == EQ) )
+ if ((ar2 == LT) || (ar2 == LTE) || (ar2 == EQ))
return TRUE;
break;
default:
}
/*checks for the case expr2 == sizeof buf1 and buf1 is a fixed array*/
-static bool sizeofBufComp(constraintExpr buf1, constraintExpr expr2)
+static bool sizeofBufComp(constraintExpr buf1, constraintExpr expr2)
{
constraintTerm ct;
exprNode e, t;
sRef s1, s2;
+
+ llassert(constraintExpr_isDefined(buf1) && constraintExpr_isDefined(expr2) );
+
/*@access constraintExpr@*/
if ((expr2->kind != term) && (buf1->kind != term) )
e = constraintTerm_getExprNode(ct);
+ llassert (exprNode_isDefined(e));
+
+ if (! (exprNode_isDefined(e)))
+ return FALSE;
+
if (e->kind != XPR_SIZEOF)
return FALSE;
s2 = constraintTerm_getsRef(constraintExprData_termGetTerm(buf1->data) );
- /*@i223@*/ /*this may be the wronge thing to test for */
+ /*drl this may be the wronge thing to test for but this
+ seems to work correctly*/
if (sRef_similarRelaxed(s1, s2) || sRef_sameName (s1, s2) )
{
- /*@i22*/ /* get rid of this test of now */
- /* if (ctype_isFixedArray (sRef_getType (s2) ) ) */
+ /* origly checked that ctype_isFixedArray(sRef_getType(s2)) but
+ removed that test */
return TRUE;
}
return FALSE;
maxSet(buf) >= sizeof(buf) - 1
*/
-/*@i223@*/ /*need to add some type checking */
+/*drl eventually it would be good to check that
+ buf is of type char.*/
+
static bool sizeOfMaxSet( /*@observer@*/ /*@temp@*/ constraint c)
{
constraintExpr l, r, buf1, buf2, con;
DPRINTF(( message("sizeOfMaxSet: checking %s ", constraint_print(c) )
));
-
+
+ llassert (constraint_isDefined(c) );
+
l = c->lexpr;
r = c->expr;
if (!((c->ar == EQ) || (c->ar == GTE) || (c->ar == LTE) ) )
return FALSE;
+ llassert (constraintExpr_isDefined(l) );
+ llassert (constraintExpr_isDefined(r) );
+
/*check if the constraintExpr is MaxSet(buf) */
if (l->kind == unaryExpr)
{
{
return FALSE;
}
-
-
}
/*@noaccess constraintExpr@*/
constraintExpr l, r;
bool rHasConstant;
int rConstant;
+
+
+ llassert (constraint_isDefined(c) );
l = c->lexpr;
r = c->expr;
}
}
- if (constraintExpr_similar (l,r) )
+ if (constraintExpr_similar (l,r))
{
switch (c->ar)
{
if (constraintExpr_similar (l,r) && (rHasConstant ) )
{
- DPRINTF(( message("constraint_IsAlwaysTrue: after removing constants %s and %s are similar", constraintExpr_unparse(l), constraintExpr_unparse(r) ) ));
- DPRINTF(( message("constraint_IsAlwaysTrue: rconstant is %d", rConstant ) ));
+ DPRINTF(( message("constraint_IsAlwaysTrue: after removing constants %s and %s are similar", constraintExpr_unparse(l), constraintExpr_unparse(r) ) ));
+ DPRINTF(( message("constraint_IsAlwaysTrue: rconstant is %d", rConstant ) ));
constraintExpr_free(l);
constraintExpr_free(r);
static bool rangeCheck (arithType ar1, /*@observer@*/ constraintExpr expr1, arithType ar2, /*@observer@*/ constraintExpr expr2)
{
- DPRINTF ((message ("Doing Range CHECK %s and %s", constraintExpr_unparse(expr1), constraintExpr_unparse(expr2) ) ));
+ DPRINTF (("Doing range check %s and %s",
+ constraintExpr_unparse (expr1), constraintExpr_unparse (expr2)));
- if (! arithType_canResolve (ar1, ar2) )
+ if (!arithType_canResolve (ar1, ar2))
return FALSE;
switch (ar1)
- {
- case GTE:
- if (constraintExpr_similar (expr1, expr2) )
- return TRUE;
- /*@fallthrough@*/
- case GT:
- if (! (constraintExpr_canGetValue (expr1) &&
- constraintExpr_canGetValue (expr2) ) )
- {
- constraintExpr e1, e2;
- bool p1, p2;
- int const1, const2;
-
- e1 = constraintExpr_copy(expr1);
- e2 = constraintExpr_copy(expr2);
-
- e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
-
- e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
-
- if (p1 || p2)
- {
- if (!p1)
- const1 = 0;
-
- if (!p2)
- const2 = 0;
-
- if (const1 <= const2)
- if (constraintExpr_similar (e1, e2) )
- {
- constraintExpr_free(e1);
- constraintExpr_free(e2);
- return TRUE;
- }
- }
- DPRINTF(("Can't Get value"));
-
- constraintExpr_free(e1);
- constraintExpr_free(e2);
- return FALSE;
- }
-
- if (constraintExpr_compare (expr2, expr1) >= 0)
- return TRUE;
-
- return FALSE;
- case EQ:
- if (constraintExpr_similar (expr1, expr2) )
- return TRUE;
-
- return FALSE;
- case LTE:
- if (constraintExpr_similar (expr1, expr2) )
- return TRUE;
- /*@fallthrough@*/
- case LT:
- if (! (constraintExpr_canGetValue (expr1) &&
- constraintExpr_canGetValue (expr2) ) )
- {
- constraintExpr e1, e2;
- bool p1, p2;
- int const1, const2;
-
- e1 = constraintExpr_copy(expr1);
- e2 = constraintExpr_copy(expr2);
-
- e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
-
- e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
-
- if (p1 || p2)
- {
- if (!p1)
- const1 = 0;
-
- if (!p2)
- const2 = 0;
-
- if (const1 >= const2)
- if (constraintExpr_similar (e1, e2) )
- {
- constraintExpr_free(e1);
- constraintExpr_free(e2);
- return TRUE;
- }
- }
- constraintExpr_free(e1);
- constraintExpr_free(e2);
-
- DPRINTF(("Can't Get value"));
- return FALSE;
- }
-
- if (constraintExpr_compare (expr2, expr1) <= 0)
- return TRUE;
-
- return FALSE;
-
- default:
+ {
+ case GTE:
+ if (constraintExpr_similar (expr1, expr2) )
+ return TRUE;
+ /*@fallthrough@*/
+ case GT:
+ if (! (constraintExpr_canGetValue (expr1) &&
+ constraintExpr_canGetValue (expr2) ) )
+ {
+ constraintExpr e1, e2;
+ bool p1, p2;
+ int const1, const2;
+
+ e1 = constraintExpr_copy(expr1);
+ e2 = constraintExpr_copy(expr2);
+
+ e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
+ e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
+
+ if (p1 || p2)
+ {
+ if (!p1)
+ const1 = 0;
+
+ if (!p2)
+ const2 = 0;
+
+ if (const1 <= const2)
+ if (constraintExpr_similar (e1, e2) )
+ {
+ constraintExpr_free(e1);
+ constraintExpr_free(e2);
+ return TRUE;
+ }
+ }
+ DPRINTF(("Can't Get value"));
+
+ constraintExpr_free(e1);
+ constraintExpr_free(e2);
+ return FALSE;
+ }
+
+ if (constraintExpr_compare (expr2, expr1) >= 0)
+ return TRUE;
+
+ return FALSE;
+ case EQ:
+ if (constraintExpr_similar (expr1, expr2) )
+ return TRUE;
+
+ return FALSE;
+ case LTE:
+ if (constraintExpr_similar (expr1, expr2) )
+ return TRUE;
+ /*@fallthrough@*/
+ case LT:
+ if (! (constraintExpr_canGetValue (expr1) &&
+ constraintExpr_canGetValue (expr2) ) )
+ {
+ constraintExpr e1, e2;
+ bool p1, p2;
+ int const1, const2;
+
+ e1 = constraintExpr_copy(expr1);
+ e2 = constraintExpr_copy(expr2);
+
+ e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
+
+ e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
+
+ if (p1 || p2)
+ {
+ if (!p1)
+ const1 = 0;
+
+ if (!p2)
+ const2 = 0;
+
+ if (const1 >= const2)
+ if (constraintExpr_similar (e1, e2) )
+ {
+ constraintExpr_free(e1);
+ constraintExpr_free(e2);
+ return TRUE;
+ }
+ }
+ constraintExpr_free(e1);
+ constraintExpr_free(e2);
+
+ DPRINTF(("Can't Get value"));
+ return FALSE;
+ }
+
+ if (constraintExpr_compare (expr2, expr1) <= 0)
+ return TRUE;
+
+ return FALSE;
+
+ default:
llcontbug((message("Unhandled case in switch: %q", arithType_print(ar1) ) ) );
- }
+ }
BADEXIT;
}
static constraint constraint_searchandreplace (/*@returned@*/ constraint c, constraintExpr old, constraintExpr newExpr)
{
- DPRINTF (("Doing replace for lexpr") );
+ llassert (constraint_isDefined(c));
+
+ DPRINTF (("Starting replace lexpr [%p]: %s < %s ==> %s > in %s", c,
+ constraintExpr_unparse (c->lexpr),
+ constraintExpr_unparse (old), constraintExpr_unparse (newExpr),
+ constraint_unparse (c)));
c->lexpr = constraintExpr_searchandreplace (c->lexpr, old, newExpr);
- DPRINTF (("Doing replace for expr") );
+ DPRINTF (("Finished replace lexpr [%p]: %s", c, constraintExpr_unparse (c->lexpr)));
c->expr = constraintExpr_searchandreplace (c->expr, old, newExpr);
return c;
}
{
bool ret;
ret = FALSE;
-
+
+ llassert (constraint_isDefined (c));
+
ret = constraintExpr_search (c->lexpr, old);
ret = ret || constraintExpr_search (c->expr, old);
return ret;
constraint_print(old))
));
+ llassert(constraint_isDefined(substitute));
+ llassert(constraint_isDefined(old));
+
loc1 = constraint_getFileloc (old);
- loc2 = constraintExpr_getFileloc (substitute->lexpr);
- loc3 = constraintExpr_getFileloc (substitute->expr);
+ loc2 = constraintExpr_loc (substitute->lexpr);
+ loc3 = constraintExpr_loc (substitute->expr);
/* special case of an equality that "contains itself" */
if (constraintExpr_search (substitute->expr, substitute->lexpr) )
constraint inequalitySubstitute (/*@returned@*/ constraint c, constraintList p)
{
+ llassert(constraint_isDefined(c) );
+
if (c->ar != GTE)
return c;
constraintList_elements (p, el)
{
+
+ llassert(constraint_isDefined(el) );
+
if ((el->ar == LT ) )
- /* if (!constraint_conflict (c, el) ) */ /*@i523 explain this! */
{
constraintExpr temp2;
- /*@i22*/
-
if (constraintExpr_same (el->expr, c->expr) )
{
DPRINTF((message ("inequalitySubstitute Replacing %q in %q with %q",
{
DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q", constraint_print(c) ) ));
+ llassert(constraint_isDefined(c) );
+
+ if (! (constraint_isDefined(c) ) )
+ {
+ return c;
+ }
+
if (c->ar != GTE)
return c;
constraint_print(c), constraintList_print(p) ) ));
constraintList_elements (p, el)
{
+
DPRINTF (( message ("inequalitySubstituteStrong examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
+ llassert(constraint_isDefined(el) );
if ((el->ar == LT ) || (el->ar == LTE ) )
- /* if (!constraint_conflict (c, el) ) */ /*@i523@*/
{
constraintExpr temp2;
- /*@i22*/
-
if (constraintExpr_same (el->lexpr, c->expr) )
{
DPRINTF((message ("inequalitySubstitute Replacing %s in %s with %s",
static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint c, constraintList p)
{
DPRINTF (( message ("Doing inequalitySubstituteUnsound " ) ));
+
+ llassert(constraint_isDefined(c) );
if (c->ar != GTE)
return c;
constraintList_elements (p, el)
{
- DPRINTF (( message ("inequalitySubstituteUnsound examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
+
+ llassert(constraint_isDefined(el) );
+
+ DPRINTF (( message ("inequalitySubstituteUnsound examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
if (( el->ar == LTE) || (el->ar == LT) )
- /* if (!constraint_conflict (c, el) ) */ /*@i532@*/
{
constraintExpr temp2;
/*@only@*/ constraint constraint_substitute (/*@observer@*/ /*@temp@*/ constraint c, constraintList p)
{
- constraint ret;
+ constraint ret = constraint_copy (c);
- ret = constraint_copy(c);
constraintList_elements (p, el)
{
- if ( el->ar == EQ)
- if (!constraint_conflict (ret, el) )
-
- {
- constraint temp;
-
- temp = constraint_copy(el);
-
- temp = constraint_adjust(temp, ret);
-
- DPRINTF((message ("constraint_substitute :: Substituting in %s using %s",
- constraint_print (ret), constraint_print (temp)
- ) ) );
-
+ if (constraint_isDefined (el))
+ {
+ if ( el->ar == EQ)
+ if (!constraint_conflict (ret, el))
+ {
+ constraint temp = constraint_copy(el);
+ temp = constraint_adjust(temp, ret);
+
+ llassert(constraint_isDefined(temp) );
+
+
+ DPRINTF (("constraint_substitute :: Substituting in %s using %s",
+ constraint_print (ret), constraint_print (temp)));
- ret = constraint_searchandreplace (ret, temp->lexpr, temp->expr);
- DPRINTF(( message (" constraint_substitute :: The new constraint is %s", constraint_print (ret) ) ));
- constraint_free(temp);
- }
+ ret = constraint_searchandreplace (ret, temp->lexpr, temp->expr);
+ DPRINTF (("constraint_substitute :: The new constraint is %s", constraint_print (ret)));;
+ constraint_free(temp);
+ }
+ }
}
end_constraintList_elements;
- ret = constraint_simplify(ret);
-
+ ret = constraint_simplify (ret);
DPRINTF(( message (" constraint_substitute :: The final new constraint is %s", constraint_print (ret) ) ));
-
return ret;
}
static constraint constraint_solve (/*@returned@*/ constraint c)
{
+
+ llassert(constraint_isDefined(c) );
+
DPRINTF((message ("Solving %s\n", constraint_print(c) ) ) );
c->expr = constraintExpr_solveBinaryExpr (c->lexpr, c->expr);
DPRINTF((message ("Solved and got %s\n", constraint_print(c) ) ) );
static constraint constraint_swapLeftRight (/*@returned@*/ constraint c)
{
constraintExpr temp;
+
+ llassert(constraint_isDefined(c) );
+
c->ar = flipAr (c->ar);
temp = c->lexpr;
c->lexpr = c->expr;
constraint constraint_simplify ( /*@returned@*/ constraint c)
{
-
+
+ llassert(constraint_isDefined(c) );
+
DPRINTF(( message("constraint_simplify on %q ", constraint_print(c) ) ));
if (constraint_tooDeep(c))