checkedc / checkedc-clang

This repo contains a version of clang that is being modified to support Checked C. Checked C is an extension to C that lets programmers write C code that is guaranteed by the compiler to be type-safe.
https://www.checkedc.org
496 stars 72 forks source link

Don't record temporary equality between expression such as x and x + 1 in TargetSrcEquality #1162

Closed kkjeer closed 3 years ago

kkjeer commented 3 years ago

This PR fixes a bug with the way equivalent expressions were being recorded in RecordEqualityWithTarget.

In an assignment such as x++, x += 1, x = x + 1, etc., SameValue is empty after calling UpdateSameValueAfterAssignment (since the RHS x + 1 of the assignment uses the value of the LHS x). This meant that SrcAllowedInEquivExprs was false in RecordEqualityWithTarget, so the mapping x => x + 1 was added to TargetSrcEquality. The information in TargetSrcEquality is added to EquivExprs in ValidateBoundsContext, so EquivExprs would contain a set that contained both x and x + 1 only while checking bounds after the current top-level statement.

This PR adds an AllowTempEquality argument to RecordEqualityWithTarget that controls whether the mapping Target => Src is permitted to be added to TargetSrcEquality. UpdateSameValueAfterAssignment now returns true if State.SameValue was unchanged by the assignment (if the RHS of the assignment uses the value of the LHS, then at least one expression will be removed from State.SameValue if State.SameValue was initially nonempty). If State.SameValue was unchanged by the assignment, then temporary equality is allowed to be recorded between Target and Src.