From: Reid Spencer Date: Thu, 2 Dec 2004 09:09:48 +0000 (+0000) Subject: Fix seriously broken implementation of GetMagicNumber. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=86ac2dcda153cbf317fa6ea7c8ad97ec0f173c4c;p=oota-llvm.git Fix seriously broken implementation of GetMagicNumber. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@18422 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/System/Unix/Path.cpp b/lib/System/Unix/Path.cpp index 70cc4f01032..5d2a4b688c4 100644 --- a/lib/System/Unix/Path.cpp +++ b/lib/System/Unix/Path.cpp @@ -178,11 +178,13 @@ bool Path::getMagicNumber(std::string& Magic, unsigned len) const { int fd = ::open(path.c_str(),O_RDONLY); if (fd < 0) return false; - if (0 != ::read(fd, buf, len)) + ssize_t bytes_read = ::read(fd, buf, len); + ::close(fd); + if (ssize_t(len) != bytes_read) { + Magic.clear(); return false; - close(fd); - buf[len] = '\0'; - Magic = buf; + } + Magic.assign(buf,len); return true; } diff --git a/lib/System/Unix/Path.inc b/lib/System/Unix/Path.inc index 70cc4f01032..5d2a4b688c4 100644 --- a/lib/System/Unix/Path.inc +++ b/lib/System/Unix/Path.inc @@ -178,11 +178,13 @@ bool Path::getMagicNumber(std::string& Magic, unsigned len) const { int fd = ::open(path.c_str(),O_RDONLY); if (fd < 0) return false; - if (0 != ::read(fd, buf, len)) + ssize_t bytes_read = ::read(fd, buf, len); + ::close(fd); + if (ssize_t(len) != bytes_read) { + Magic.clear(); return false; - close(fd); - buf[len] = '\0'; - Magic = buf; + } + Magic.assign(buf,len); return true; }