System: Fix getMagicNumber on windows.
authorMichael J. Spencer <bigcheesegs@gmail.com>
Tue, 31 Aug 2010 06:36:33 +0000 (06:36 +0000)
committerMichael J. Spencer <bigcheesegs@gmail.com>
Tue, 31 Aug 2010 06:36:33 +0000 (06:36 +0000)
getMagicNumber was treating the _binary_ data it read in as a
null terminated string. This resulted in the std::string
calculating the length, and causing an assert in other code that
assumed that the length it passed was the same as the length of
the string it would get back.

llvm-svn: 112586

llvm/lib/System/Win32/Path.inc

index 42e58a4708501e415a57d3ad5e5b3f63f53477aa..4a6dbd3ddf29999aa41c9e1fbe1613afdc3ee9f6 100644 (file)
@@ -722,7 +722,7 @@ Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
 
 bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
   assert(len < 1024 && "Request for magic string too long");
-  char* buf = (char*) alloca(1 + len);
+  char* buf = reinterpret_cast<char*>(alloca(len));
 
   HANDLE h = CreateFile(path.c_str(),
                         GENERIC_READ,
@@ -741,8 +741,7 @@ bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
   if (!ret || nRead != len)
     return false;
 
-  buf[len] = '\0';
-  Magic = buf;
+  Magic = std::string(buf, len);
   return true;
 }