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

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 11 13:48:54 PDT 2015


This revision was automatically updated to reflect the committed changes.
Closed by commit rL247471: [MC] Don't crash on division by zero. (authored by davide).

Changed prior to commit:
  http://reviews.llvm.org/D12776?vs=34492&id=34584#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D12776

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

Index: llvm/trunk/lib/MC/MCExpr.cpp
===================================================================
--- llvm/trunk/lib/MC/MCExpr.cpp
+++ llvm/trunk/lib/MC/MCExpr.cpp
@@ -739,7 +739,17 @@
     case MCBinaryExpr::AShr: Result = LHS >> RHS; break;
     case MCBinaryExpr::Add:  Result = LHS + RHS; break;
     case MCBinaryExpr::And:  Result = LHS & RHS; break;
-    case MCBinaryExpr::Div:  Result = LHS / RHS; break;
+    case MCBinaryExpr::Div: {
+      // Handle division by zero. gas just emits a warning and keeps going,
+      // we try to be stricter.
+      // FIXME: Currently the caller of this function has no way to understand
+      // we're bailing out because of 'division by zero'. Therefore, it will
+      // emit a 'expected relocatable expression' error. It would be nice to
+      // change this code to emit a better diagnostic.
+      if (RHS == 0)
+        return false;
+      Result = LHS / RHS; break;
+    }
     case MCBinaryExpr::EQ:   Result = LHS == RHS; break;
     case MCBinaryExpr::GT:   Result = LHS > RHS; break;
     case MCBinaryExpr::GTE:  Result = LHS >= RHS; break;
Index: llvm/trunk/test/MC/ELF/div-by-zero.s
===================================================================
--- llvm/trunk/test/MC/ELF/div-by-zero.s
+++ llvm/trunk/test/MC/ELF/div-by-zero.s
@@ -0,0 +1,6 @@
+// Check that llvm-mc doesn't crash on division by zero.
+// RUN: not llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s 2> %t
+// RUN: FileCheck -input-file %t %s
+
+// CHECK: expected relocatable expression
+.int 1/0


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


More information about the llvm-commits mailing list