From 922696f03ec9637449e2cba260493808b4977cd3 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Mon, 19 May 2008 17:51:16 +0000 Subject: Fix the emission of expressions like char a[10] = "asdf"; previously, they were causing bad code to be emitted. There are two fixes here: one makes sure we emit a string that is long enough, and one makes sure we properly handle string initialization in init lists. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@51259 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGExpr.cpp | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'lib/CodeGen/CGExpr.cpp') diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index 840ed4314f..8d36c8e26d 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -395,9 +395,16 @@ LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) { LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) { assert(!E->isWide() && "FIXME: Wide strings not supported yet!"); + // Get the string data const char *StrData = E->getStrData(); unsigned Len = E->getByteLength(); std::string StringLiteral(StrData, StrData+Len); + + // Resize the string to the right size + const ConstantArrayType *CAT = E->getType()->getAsConstantArrayType(); + uint64_t RealLen = CAT->getSize().getZExtValue(); + StringLiteral.resize(RealLen, '\0'); + return LValue::MakeAddr(CGM.GetAddrOfConstantString(StringLiteral)); } -- cgit v1.2.3