Summary:
It looks like not having offset/size/filename information is way more
harmful, than storing filename just for the sake of this error message.
Reviewed By: yfeldblum
Differential Revision:
D6536616
fbshipit-source-id:
469fbdf1deedd76ebd79cf98716c2c269cb10e4d
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string>
#include <glog/logging.h>
#include <string>
#include <glog/logging.h>
bool readOnly,
const char** msg) noexcept {
FOLLY_SAFE_CHECK(fd_ == -1, "File already open");
bool readOnly,
const char** msg) noexcept {
FOLLY_SAFE_CHECK(fd_ == -1, "File already open");
+ strncat(filepath_, name, kFilepathMaxLen - 1);
fd_ = ::open(name, readOnly ? O_RDONLY : O_RDWR);
if (fd_ == -1) {
if (msg) {
fd_ = ::open(name, readOnly ? O_RDONLY : O_RDWR);
if (fd_ == -1) {
if (msg) {
file_(other.file_),
length_(other.length_),
baseAddress_(other.baseAddress_) {
file_(other.file_),
length_(other.length_),
baseAddress_(other.baseAddress_) {
+ // copy other.filepath_, leaving filepath_ zero-terminated, always.
+ strncat(filepath_, other.filepath_, kFilepathMaxLen - 1);
+ other.filepath_[0] = 0;
other.fd_ = -1;
other.file_ = static_cast<char*>(MAP_FAILED);
other.length_ = 0;
other.fd_ = -1;
other.file_ = static_cast<char*>(MAP_FAILED);
other.length_ = 0;
assert(this != &other);
reset();
assert(this != &other);
reset();
+ // copy other.filepath_, leaving filepath_ zero-terminated, always.
+ strncat(filepath_, other.filepath_, kFilepathMaxLen - 1);
fd_ = other.fd_;
file_ = other.file_;
length_ = other.length_;
baseAddress_ = other.baseAddress_;
fd_ = other.fd_;
file_ = other.file_;
length_ = other.length_;
baseAddress_ = other.baseAddress_;
+ other.filepath_[0] = 0;
other.fd_ = -1;
other.file_ = static_cast<char*>(MAP_FAILED);
other.length_ = 0;
other.fd_ = -1;
other.file_ = static_cast<char*>(MAP_FAILED);
other.length_ = 0;
}
void ElfFile::reset() {
}
void ElfFile::reset() {
if (file_ != MAP_FAILED) {
munmap(file_, length_);
file_ = static_cast<char*>(MAP_FAILED);
if (file_ != MAP_FAILED) {
munmap(file_, length_);
file_ = static_cast<char*>(MAP_FAILED);
template <class T>
const typename std::enable_if<std::is_pod<T>::value, T>::type& at(
ElfOff offset) const {
template <class T>
const typename std::enable_if<std::is_pod<T>::value, T>::type& at(
ElfOff offset) const {
- FOLLY_SAFE_CHECK(
- offset + sizeof(T) <= length_,
- "Offset is not contained within our mmapped file");
+ if (offset + sizeof(T) > length_) {
+ char msg[kFilepathMaxLen + 128];
+ snprintf(
+ msg,
+ sizeof(msg),
+ "Offset (%lu + %lu) is not contained within our mmapped"
+ " file (%s) of length %zu",
+ offset,
+ sizeof(T),
+ filepath_,
+ length_);
+ FOLLY_SAFE_CHECK(offset + sizeof(T) <= length_, msg);
+ }
return *reinterpret_cast<T*>(file_ + offset);
}
return *reinterpret_cast<T*>(file_ + offset);
}
return at<T>(section.sh_offset + (addr - section.sh_addr));
}
return at<T>(section.sh_offset + (addr - section.sh_addr));
}
+ static constexpr size_t kFilepathMaxLen = 512;
+ char filepath_[kFilepathMaxLen] = {};
int fd_;
char* file_; // mmap() location
size_t length_; // mmap() length
int fd_;
char* file_; // mmap() location
size_t length_; // mmap() length