cf-natali commented on a change in pull request #384:
URL: https://github.com/apache/mesos/pull/384#discussion_r642112754



##########
File path: src/linux/ldcache.cpp
##########
@@ -146,33 +162,48 @@ Try<vector<Entry>> parse(const string& path)
 
   const char* data = buffer->data();
 
-  // Grab a pointer to the old format header (for verification of
-  // HEADER_MAGIC_OLD later on). Then jump forward to the location of
-  // the new format header (it is the only format we support).
-  HeaderOld* headerOld = (HeaderOld*)data;
-  data += sizeof(HeaderOld);
-  if (data >= buffer->data() + buffer->size()) {
+  HeaderNew* headerNew = (HeaderNew*)data;
+  if (data + sizeof(HeaderNew) >= buffer->data() + buffer->size()) {
     return Error("Invalid format");
   }
 
-  data += headerOld->libraryCount * sizeof(EntryOld);
-  if (data >= buffer->data() + buffer->size()) {
-    return Error("Invalid format");
-  }
+  if (strncmp(headerNew->magic,
+      HEADER_MAGIC_NEW,
+      sizeof(HEADER_MAGIC_NEW) - 1) != 0) {
+    // If the data doesn't start with the new header, it must be a
+    // compat format, therefore we expect an old header.
+    HeaderOld* headerOld = (HeaderOld*)data;
+    data += sizeof(HeaderOld);
+    if (data >= buffer->data() + buffer->size()) {
+      return Error("Invalid format");
+    }
 
-  // The new format header and all of its library entries are embedded
-  // in the old format's string table (the current location of data).
-  // However, the header is aligned on an 8 byte boundary, so we
-  // need to align 'data' to get it to point to the new header.
-  data = align(data, alignof(HeaderNew));
-  if (data >= buffer->data() + buffer->size()) {
-    return Error("Invalid format");
+    // Validate our header magic.
+    if (strncmp(headerOld->magic,
+        HEADER_MAGIC_OLD,
+        sizeof(HEADER_MAGIC_OLD) - 1) != 0) {
+      return Error("Invalid format");
+    }
+
+    data += headerOld->libraryCount * sizeof(EntryOld);
+    if (data >= buffer->data() + buffer->size()) {
+      return Error("Invalid format");
+    }
+
+    // The new format header and all of its library entries are embedded
+    // in the old format's string table (the current location of data).
+    // However, the header is aligned on an 8 byte boundary, so we
+    // need to align 'data' to get it to point to the new header.
+    data = align(data, alignof(HeaderNew));
+    if (data >= buffer->data() + buffer->size()) {
+      return Error("Invalid format");
+    }
+    headerNew = (HeaderNew*)data;

Review comment:
       Done!




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to