[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[creduce-bugs] Fix for bug in UnionToStruct



Hi,
I manage to trigger a bug in UnionToStruct. If a union variable was initialized by copying another varable of the same type, as in the attached example, the union-to-struct transform would crash as it assumed that all union variables were initialized using an initializer list. I attached a patch that (hopefully) fixes the problem.

Sincerely,
Johan Bengtsson

>From a3afda212c088978d03a152a6788f7b9f32d0d7a Mon Sep 17 00:00:00 2001
From: Johan Bengtsson <johan.bengtsson@iar.com>
Date: Wed, 12 Aug 2015 16:56:07 +0200
Subject: [PATCH 3/3] Avoid crash in UnionToStruct transform

In rewriteOneVarDecl the code assumed that the new variable was
initialized using an initializer list. If the variable was initialized
by copying another variable of the same type the transform would crash.
---
 clang_delta/UnionToStruct.cpp | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/clang_delta/UnionToStruct.cpp b/clang_delta/UnionToStruct.cpp
index 54333d7..b7c39e5 100644
--- a/clang_delta/UnionToStruct.cpp
+++ b/clang_delta/UnionToStruct.cpp
@@ -345,26 +345,26 @@ void UnionToStruct::rewriteOneVarDecl(const VarDecl *VD)
     return;
   }
 
-  const InitListExpr *ILE = dyn_cast<InitListExpr>(IE);
-  TransAssert(ILE && "Bad InitListExpr!");
+  if (const InitListExpr *ILE = dyn_cast<InitListExpr>(IE)) {
 
-  if (ILE->getNumInits() != 1) {
-    RewriteHelper->removeVarInitExpr(VD);
-    return;
-  }
+    if (ILE->getNumInits() != 1) {
+      RewriteHelper->removeVarInitExpr(VD);
+      return;
+    }
 
-  const Expr *FirstE = ILE->getInit(0);
-  const Type *ExprTy = FirstE->getType().getTypePtr();
-  std::string NewInitStr;
+    const Expr *FirstE = ILE->getInit(0);
+    const Type *ExprTy = FirstE->getType().getTypePtr();
+    std::string NewInitStr;
 
-  if (ExprTy->isPointerType()) {
-    getInitStrWithPointerType(FirstE, NewInitStr);
-  }
-  else {
-    getInitStrWithNonPointerType(FirstE, NewInitStr);
-  }
+    if (ExprTy->isPointerType()) {
+      getInitStrWithPointerType(FirstE, NewInitStr);
+    }
+    else {
+      getInitStrWithNonPointerType(FirstE, NewInitStr);
+    }
 
-  RewriteHelper->replaceExpr(FirstE, NewInitStr);
+    RewriteHelper->replaceExpr(FirstE, NewInitStr);
+  }
 }
 
 void UnionToStruct::rewriteOneFunctionDecl(const FunctionDecl *FD)
-- 
1.9.1

union U1 { int f1; char f2; } g[3];

void f()
{
  union U1 l = g[0];
}