[cfe-dev] Constant C string fix

David Chisnall csdavec at swansea.ac.uk
Tue Dec 9 11:05:43 PST 2008


The constant C string implementation in CodeGenModule is incorrect.   
When you do str + "\0", you are appending a C string to the string,  
and so it inserts all of the characters before the first NULL byte  
into the C++ string.  Since \0 is the first byte, (str + "\0") ==  
str.  This means anything calling GetAddrOfConstantCString() is  
getting the wrong result.  I found this due to the ObjC class lookup  
function being passed unterminated strings - it also explains the  
random crashes on Linux in the ObjC load function.  I haven't checked  
if this pattern is being used anywhere else.


Index: CodeGenModule.cpp
===================================================================
--- CodeGenModule.cpp	(revision 60768)
+++ CodeGenModule.cpp	(working copy)
@@ -959,7 +959,7 @@
  /// character. The result has pointer to array type.
  llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const  
std::string &str,
                                                          const char  
*GlobalName){
-  return GetAddrOfConstantString(str + "\0", GlobalName);
+  return GetAddrOfConstantString(str + '\0', GlobalName);
  }

  /// EmitObjCPropertyImplementations - Emit information for synthesized





More information about the cfe-dev mailing list