Martinez, Javier E
2010-Jul-16 22:20 UTC
[LLVMdev] Strange behavior when converting arrays to strings
Hello, I found saw some strange behavior (to me) when converting constant arrays to strings. Consider the following example: std::string Text = "HelloWorld"; unsigned TextLengthBefore = Text.length(); ConstantArray *pArray = dyn_cast<ConstantArray>(llvm::ConstantArray::get(pModule->getContext(), Text, true)); unsigned NumElements = pArray->getNumOperands(); Text = pArray->getAsString(); unsigned TextLengthAfter = Text.length(); After running this example here are the values in each variable: TextLengthBefore = 10 NumElements = 11 TextLengthAfter = 11 In the conversion from constant array to a string the null terminating character is added as part of the string and becomes the 11th character. This becomes a problem when the data is streamed out to a buffer because a NULL is inserted in the middle. Below is the code for getAsString: 1: std::string ConstantArray::getAsString() const { 2: assert(isString() && "Not a string!"); 3: std::string Result; 4: Result.reserve(getNumOperands()); 5: for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 6: Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue()); 7: return Result; 8: } I think that the loop terminating condition in line 5 should be changed from != to <. Does this look right? Thanks, Javier -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100716/68b81a5f/attachment.html>
Martinez, Javier E
2010-Jul-28 00:19 UTC
[LLVMdev] Strange behavior when converting arrays to strings
Hi, I haven't seen a response and I'm curious if I should submit a patch for this. Thanks, Javier From: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu] On Behalf Of Martinez, Javier E Sent: Friday, July 16, 2010 3:20 PM To: llvmdev at cs.uiuc.edu Subject: [LLVMdev] Strange behavior when converting arrays to strings Hello, I found saw some strange behavior (to me) when converting constant arrays to strings. Consider the following example: std::string Text = "HelloWorld"; unsigned TextLengthBefore = Text.length(); ConstantArray *pArray = dyn_cast<ConstantArray>(llvm::ConstantArray::get(pModule->getContext(), Text, true)); unsigned NumElements = pArray->getNumOperands(); Text = pArray->getAsString(); unsigned TextLengthAfter = Text.length(); After running this example here are the values in each variable: TextLengthBefore = 10 NumElements = 11 TextLengthAfter = 11 In the conversion from constant array to a string the null terminating character is added as part of the string and becomes the 11th character. This becomes a problem when the data is streamed out to a buffer because a NULL is inserted in the middle. Below is the code for getAsString: 1: std::string ConstantArray::getAsString() const { 2: assert(isString() && "Not a string!"); 3: std::string Result; 4: Result.reserve(getNumOperands()); 5: for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 6: Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue()); 7: return Result; 8: } I think that the loop terminating condition in line 5 should be changed from != to <. Does this look right? Thanks, Javier -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100727/ea19f6c4/attachment.html>
Duncan Sands
2010-Jul-28 07:37 UTC
[LLVMdev] Strange behavior when converting arrays to strings
Hi Javier,> I found saw some strange behavior (to me) when converting constant > arrays to strings. Consider the following example: > > std::string Text = "HelloWorld"; > > unsigned TextLengthBefore = Text.length(); > > ConstantArray *pArray > dyn_cast<ConstantArray>(llvm::ConstantArray::get(pModule->getContext(), > Text, true));from Constants.h: /// This method constructs a ConstantArray and initializes it with a text /// string. The default behavior (AddNull==true) causes a null terminator to /// be placed at the end of the array. This effectively increases the length /// of the array by one (you've been warned). However, in some situations /// this is not desired so if AddNull==false then the string is copied without /// null termination. static Constant *get(LLVMContext &Context, StringRef Initializer, bool AddNull = true); Ciao, Duncan.> > unsigned NumElements = pArray->getNumOperands(); > > Text = pArray->getAsString(); > > unsigned TextLengthAfter = Text.length(); > > After running this example here are the values in each variable: > > TextLengthBefore = 10 > > NumElements = 11 > > TextLengthAfter = 11 > > In the conversion from constant array to a string the null terminating > character is added as part of the string and becomes the 11^th > character. This becomes a problem when the data is streamed out to a > buffer because a NULL is inserted in the middle. Below is the code for > getAsString: > > 1: std::string ConstantArray::getAsString() const { > > 2: assert(isString() && "Not a string!"); > > 3: std::string Result; > > 4: Result.reserve(getNumOperands()); > > 5: for (unsigned i = 0, e = getNumOperands(); i != e; ++i) > > 6: > Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue()); > > 7: return Result; > > 8: } > > I think that the loop terminating condition in line 5 should be changed > from != to <. Does this look right? > > Thanks, > > Javier > > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reasonably Related Threads
- [LLVMdev] Strange behavior when converting arrays to strings
- [LLVMdev] Strange behavior when converting arrays to strings
- [LLVMdev] could you give me some advice ?
- [LLVMdev] llvm-gcc compilation and ConstantArray::getAsString
- [LLVMdev] Question about insert call func with pionter parameter