[PATCH] D12776: [MC] Don't crash on division by zero

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 10 14:31:07 PDT 2015


davide created this revision.
davide added reviewers: rafael, echristo, grosbach.
davide added a subscriber: llvm-commits.
davide set the repository for this revision to rL LLVM.

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)

Repository:
  rL LLVM

http://reviews.llvm.org/D12776

Files:
  lib/MC/MCExpr.cpp
  test/MC/ELF/div-by-zero.s

Index: test/MC/ELF/div-by-zero.s
===================================================================
--- test/MC/ELF/div-by-zero.s
+++ test/MC/ELF/div-by-zero.s
@@ -0,0 +1,4 @@
+// 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
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;
+
     int64_t Result = 0;
     switch (ABE->getOpcode()) {
     case MCBinaryExpr::AShr: Result = LHS >> RHS; break;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D12776.34492.patch
Type: text/x-patch
Size: 863 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150910/cea4aa6e/attachment.bin>


More information about the llvm-commits mailing list