From 79d5ddb77bd498e2975ceed8360c06cf709ff618 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Thu, 23 May 2013 22:59:40 +0200 Subject: [PATCH] ReaderImplBase: Verify that the attribute type matches ... the one expected for the respective attribute. Before it was possible that e.g. a uint was read and then interpreted as a const char*, if a string was expected for that attribute. --- headers/private/package/hpkg/ReaderImplBase.h | 7 +++-- src/kits/package/hpkg/PackageReaderImpl.cpp | 4 +-- src/kits/package/hpkg/ReaderImplBase.cpp | 28 ++++++++++++++----- .../package/hpkg/RepositoryReaderImpl.cpp | 2 +- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/headers/private/package/hpkg/ReaderImplBase.h b/headers/private/package/hpkg/ReaderImplBase.h index 7072daddc8..465631bfd5 100644 --- a/headers/private/package/hpkg/ReaderImplBase.h +++ b/headers/private/package/hpkg/ReaderImplBase.h @@ -110,7 +110,8 @@ protected: typedef SinglyLinkedList AttributeHandlerList; protected: - template + template status_t Init(int fd, bool keepFD, Header& header, uint32 flags); status_t InitHeapReader(uint32 compression, @@ -182,6 +183,7 @@ private: int fFD; bool fOwnsFD; uint16 fMinorFormatVersion; + uint16 fCurrentMinorFormatVersion; PackageFileHeapReader* fRawHeapReader; BAbstractBufferedDataReader* fHeapReader; @@ -378,7 +380,7 @@ private: // #pragma mark - template and inline methods -template +template status_t ReaderImplBase::Init(int fd, bool keepFD, Header& header, uint32 flags) { @@ -418,6 +420,7 @@ ReaderImplBase::Init(int fd, bool keepFD, Header& header, uint32 flags) } fMinorFormatVersion = B_BENDIAN_TO_HOST_INT16(header.minor_version); + fCurrentMinorFormatVersion = kMinorVersion; // header size uint64 heapOffset = B_BENDIAN_TO_HOST_INT16(header.header_size); diff --git a/src/kits/package/hpkg/PackageReaderImpl.cpp b/src/kits/package/hpkg/PackageReaderImpl.cpp index 370a89ae4d..68425989c6 100644 --- a/src/kits/package/hpkg/PackageReaderImpl.cpp +++ b/src/kits/package/hpkg/PackageReaderImpl.cpp @@ -335,8 +335,8 @@ status_t PackageReaderImpl::Init(int fd, bool keepFD, uint32 flags) { hpkg_header header; - status_t error = inherited::Init( - fd, keepFD, header, flags); + status_t error = inherited::Init(fd, keepFD, header, flags); if (error != B_OK) return error; fHeapSize = UncompressedHeapSize(); diff --git a/src/kits/package/hpkg/ReaderImplBase.cpp b/src/kits/package/hpkg/ReaderImplBase.cpp index 08f2787f32..3270b3b426 100644 --- a/src/kits/package/hpkg/ReaderImplBase.cpp +++ b/src/kits/package/hpkg/ReaderImplBase.cpp @@ -33,6 +33,12 @@ namespace BPrivate { static const size_t kScratchBufferSize = 64 * 1024; +static const uint16 kAttributeTypes[B_HPKG_ATTRIBUTE_ID_ENUM_COUNT] = { + #define B_DEFINE_HPKG_ATTRIBUTE(id, type, name, constant) \ + B_HPKG_ATTRIBUTE_TYPE_##type, + #include + #undef B_DEFINE_HPKG_ATTRIBUTE +}; // #pragma mark - AttributeHandlerContext @@ -1035,18 +1041,26 @@ ReaderImplBase::_ReadAttribute(uint8& _id, AttributeValue& _value, return B_BAD_DATA; } + // get the ID + _id = attribute_tag_id(tag); + if (_id < B_HPKG_ATTRIBUTE_ID_ENUM_COUNT) { + if (type != kAttributeTypes[_id]) { + fErrorOutput->PrintError("Error: Invalid %s section: " + "unexpected type %d for attribute id %d (expected %d)!\n", + fCurrentSection->name, type, _id, kAttributeTypes[_id]); + return B_BAD_DATA; + } + } else if (fMinorFormatVersion <= fCurrentMinorFormatVersion) { + fErrorOutput->PrintError("Error: Invalid %s section: " + "attribute id %d not supported!\n", fCurrentSection->name, _id); + return B_BAD_DATA; + } + // get the value error = ReadAttributeValue(type, attribute_tag_encoding(tag), _value); if (error != B_OK) return error; - - _id = attribute_tag_id(tag); - if (_id >= B_HPKG_ATTRIBUTE_ID_ENUM_COUNT) { - fErrorOutput->PrintError("Error: Invalid %s section: " - "attribute id %d not supported!\n", fCurrentSection->name, _id); - return B_BAD_DATA; - } } if (_hasChildren != NULL) diff --git a/src/kits/package/hpkg/RepositoryReaderImpl.cpp b/src/kits/package/hpkg/RepositoryReaderImpl.cpp index a9a5b1c662..30554ea4f9 100644 --- a/src/kits/package/hpkg/RepositoryReaderImpl.cpp +++ b/src/kits/package/hpkg/RepositoryReaderImpl.cpp @@ -73,7 +73,7 @@ RepositoryReaderImpl::Init(int fd, bool keepFD) { hpkg_repo_header header; status_t error = inherited::Init(fd, keepFD, header, 0); + B_HPKG_REPO_VERSION, B_HPKG_REPO_MINOR_VERSION>(fd, keepFD, header, 0); if (error != B_OK) return error;