This is an archive of the discontinued LLVM Phabricator instance.

Fix PR47973: Addressing integer division edge case with INT_MIN
ClosedPublic

Authored by akuran on Oct 27 2020, 4:16 AM.

Details

Summary

Adjustment to integer division in int_div_impl.inc to avoid undefined behaviour that can occur as a result of having INT_MIN as one of the parameters.

Diff Detail

Event Timeline

akuran created this revision.Oct 27 2020, 4:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 27 2020, 4:16 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
akuran retitled this revision from Addressing integer division edge case with INT_MIN to Addressing integer division edge case with INT_MIN Fix PR47973.Oct 27 2020, 4:24 AM
akuran edited the summary of this revision. (Show Details)
akuran retitled this revision from Addressing integer division edge case with INT_MIN Fix PR47973 to Fix PR47973: Addressing integer division edge case with INT_MIN.
akuran published this revision for review.Oct 27 2020, 4:28 AM
thopre added a subscriber: thopre.
akuran updated this revision to Diff 301222.Oct 28 2020, 3:53 AM

Restart build

sepavloff added inline comments.Nov 9 2020, 11:06 PM
compiler-rt/lib/builtins/int_div_impl.inc
77

What is the purpose of using addition+negation instead of subtraction?

akuran added inline comments.Nov 10 2020, 2:40 AM
compiler-rt/lib/builtins/int_div_impl.inc
77

(fixuint_t)a^s_a will always be some unsigned value, and with just subtraction we have <unsigned> - s_a. And when s_a = -1, s_a will get promoted to an unsigned value UINT_MAX, resulting in <unsigned> - UINT_MAX which can result in undefined behaviour. By negating s_a first we'd ensure that the operation there is always just either +0 or +1.

This revision is now accepted and ready to land.Nov 10 2020, 4:22 AM
This revision was landed with ongoing or failed builds.Nov 10 2020, 7:57 AM
This revision was automatically updated to reflect the committed changes.