This is an archive of the discontinued LLVM Phabricator instance.

[MC] Don't crash on division by zero
ClosedPublic

Authored by davide on Sep 10 2015, 2:31 PM.

Details

Summary

Maybe there's a better way to handle this case (in the Parser?), but at least we now don't crash with FPE on such cases. Also, FWIW, gas seems to handle this in the same exact way (+ emitting a warning)

Diff Detail

Event Timeline

davide updated this revision to Diff 34492.Sep 10 2015, 2:31 PM
davide retitled this revision from to [MC] Don't crash on division by zero.
davide updated this object.
davide added reviewers: rafael, echristo, grosbach.
davide set the repository for this revision to rL LLVM.
davide added a subscriber: llvm-commits.
rafael edited edge metadata.Sep 10 2015, 3:00 PM
rafael added a subscriber: rafael.

+ Check that llvm-mc doesn't crash on division by zero.
+
RUN: llvm-mc -triple x86_64-pc-linux-gnu %s -o - 2>&1
+
+.int 1/0

Does it work with -filetype=obj? What is output?

Index: lib/MC/MCExpr.cpp

  • lib/MC/MCExpr.cpp

+++ lib/MC/MCExpr.cpp
@@ -734,6 +734,11 @@

// width, and gas defines the result of comparisons differently from
// Apple as.
int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();

+
+ // Handle division by zero.
+ if (ABE->getOpcode() == MCBinaryExpr::Div && RHS == 0)
+ return false;
+

Move the check to the "case MCBinaryExpr::Div:" bellow.

int64_t Result = 0;
switch (ABE->getOpcode()) {
case MCBinaryExpr::AShr: Result = LHS >> RHS; break;

Cheers,
Rafael

This revision was automatically updated to reflect the committed changes.