If errno is zero strerror_r does not modify the buffer, leaving it unterminated.
authorChris Lattner <sabre@nondot.org>
Sun, 13 Feb 2005 22:46:37 +0000 (22:46 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 13 Feb 2005 22:46:37 +0000 (22:46 +0000)
This causes garbage to be printed out after error messages.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@20165 91177308-0d34-0410-b5e6-96231b3b80d8

lib/System/Unix/MappedFile.inc
lib/System/Unix/Unix.h

index 07683c1439270d16527f6bbefd971168b5f17905..409cea7e822c2ec7bb9fb266b943581f7b2b505e 100644 (file)
@@ -51,6 +51,7 @@ void MappedFile::initialize() {
     else if (options_&WRITE_ACCESS)
       mode = O_WRONLY;
     info_->fd_ = ::open(path_.c_str(),mode);
+
     if (info_->fd_ < 0) {
       delete info_;
       info_ = 0;
index 6dc75545b66aadb1bbd7aba18babcd51a934c91e..75009d0746e5f1d031165397d5bae644881c4a72 100644 (file)
 
 inline void ThrowErrno(const std::string& prefix) {
     char buffer[MAXPATHLEN];
+    buffer[0] = 0;
 #ifdef HAVE_STRERROR_R
     // strerror_r is thread-safe.
-    strerror_r(errno,buffer,MAXPATHLEN-1);
+    if (errno)
+      strerror_r(errno,buffer,MAXPATHLEN-1);
 #elif HAVE_STRERROR
     // Copy the thread un-safe result of strerror into
     // the buffer as fast as possible to minimize impact
     // of collision of strerror in multiple threads.
-    strncpy(buffer,strerror(errno),MAXPATHLEN-1);
+    if (Errno)
+      strncpy(buffer,strerror(errno),MAXPATHLEN-1);
     buffer[MAXPATHLEN-1] = 0;
 #else
     // Strange that this system doesn't even have strerror