Make ElfFile not crash on invalid ELF files
[folly.git] / folly / experimental / symbolizer / Elf.h
index f1c4933431f83f77f3bd9e3e3798a151dc3cbbc2..6214a4d566b0bf9bf0a17bd849eb252799123542 100644 (file)
@@ -49,8 +49,15 @@ class ElfFile {
   explicit ElfFile(const char* name, bool readOnly=true);
 
   // Open the ELF file.
-  // Returns 0 on success, -1 (and sets errno) on failure and (if msg is not
-  // NULL) sets *msg to a static string indicating what failed.
+  // Returns 0 on success, kSystemError (guaranteed to be -1) (and sets errno)
+  // on IO error, kInvalidElfFile (and sets errno to EINVAL) for an invalid
+  // Elf file. On error, if msg is not NULL, sets *msg to a static string
+  // indicating what failed.
+  enum {
+    kSuccess = 0,
+    kSystemError = -1,
+    kInvalidElfFile = -2,
+  };
   int openNoThrow(const char* name, bool readOnly=true,
                   const char** msg=nullptr) noexcept;
 
@@ -59,7 +66,7 @@ class ElfFile {
 
   ~ElfFile();
 
-  ElfFile(ElfFile&& other);
+  ElfFile(ElfFile&& other) noexcept;
   ElfFile& operator=(ElfFile&& other);
 
   /** Retrieve the ELF header */
@@ -185,7 +192,7 @@ class ElfFile {
   const ElfW(Shdr)* getSectionContainingAddress(ElfW(Addr) addr) const;
 
  private:
-  void init();
+  bool init(const char** msg);
   void destroy();
   ElfFile(const ElfFile&) = delete;
   ElfFile& operator=(const ElfFile&) = delete;