From 2b282951af20b6482e8a922c343969cd1fe95198 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Tue, 15 Nov 2016 06:34:10 +0200 Subject: [PATCH] Extra validation code in the MaxmindDB module. --- modules/mmdb/Common.hpp | 2 ++ modules/mmdb/EntryData.cpp | 20 ++++++++++--- modules/mmdb/EntryData.hpp | 55 ++++++++-------------------------- modules/mmdb/EntryDataList.cpp | 12 +++----- modules/mmdb/SearchNode.cpp | 16 ++++------ modules/mmdb/SearchNode.hpp | 30 +++++++------------ 6 files changed, 50 insertions(+), 85 deletions(-) diff --git a/modules/mmdb/Common.hpp b/modules/mmdb/Common.hpp index fdb3f279..9e6be772 100644 --- a/modules/mmdb/Common.hpp +++ b/modules/mmdb/Common.hpp @@ -30,10 +30,12 @@ namespace SqMod { #define SQMOD_VALIDATE(x) (x).Validate(__FILE__, __LINE__) #define SQMOD_GET_VALID(x) (x).GetValid(__FILE__, __LINE__) #define SQMOD_GET_VALID_ELEM(x) (x).GetValidElem(__FILE__, __LINE__) + #define SQMOD_GET_VALID_DATA(x) (x).GetValidData(__FILE__, __LINE__) #else #define SQMOD_VALIDATE(x) (x).Validate() #define SQMOD_GET_VALID(x) (x).GetValid() #define SQMOD_GET_VALID_ELEM(x) (x).GetValidElem() + #define SQMOD_GET_VALID_DATA(x) (x).GetValidData() #endif // _DEBUG /* ------------------------------------------------------------------------------------------------ diff --git a/modules/mmdb/EntryData.cpp b/modules/mmdb/EntryData.cpp index fc8884f2..6d9b1299 100644 --- a/modules/mmdb/EntryData.cpp +++ b/modules/mmdb/EntryData.cpp @@ -38,16 +38,28 @@ void EntryData::Validate() const // ------------------------------------------------------------------------------------------------ #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) -const DbRef & EntryData::GetValid(CCStr file, Int32 line) const +EntryData::ConstRef EntryData::GetValid(CCStr file, Int32 line) const { Validate(file, line); - return m_Handle; + // See if the entry has any data + if (!m_Entry.has_data) + { + SqThrowF("The referenced entry has no data =>[%s:%d]", file, line); + } + // Return the entry + return m_Entry; } #else -const DbRef & EntryData::GetValid() const +EntryData::ConstRef EntryData::GetValid() const { Validate(); - return m_Handle; + // See if the entry has any data + if (!m_Entry.has_data) + { + SqThrowF("The referenced entry has no data"); + } + // Return the entry + return m_Entry; } #endif // _DEBUG diff --git a/modules/mmdb/EntryData.hpp b/modules/mmdb/EntryData.hpp index 0de154f1..ab203b8c 100644 --- a/modules/mmdb/EntryData.hpp +++ b/modules/mmdb/EntryData.hpp @@ -40,9 +40,9 @@ protected: * Validate the managed database handle and throw an error if invalid. */ #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) - const DbRef & GetValid(CCStr file, Int32 line) const; + ConstRef GetValid(CCStr file, Int32 line) const; #else - const DbRef & GetValid() const; + ConstRef GetValid() const; #endif // _DEBUG private: @@ -142,10 +142,7 @@ public: */ bool HasData() const { - // Validate the handle - SQMOD_VALIDATE(*this); - // Return the requested information - return ConvTo< bool >::From(m_Entry.has_data); + return ConvTo< bool >::From(SQMOD_GET_VALID(*this).has_data); } /* -------------------------------------------------------------------------------------------- @@ -153,10 +150,7 @@ public: */ SQInteger GetType() const { - // Validate the handle - SQMOD_VALIDATE(*this); - // Return the requested information - return ConvTo< SQInteger >::From(m_Entry.type); + return ConvTo< SQInteger >::From(SQMOD_GET_VALID(*this).type); } /* -------------------------------------------------------------------------------------------- @@ -164,10 +158,7 @@ public: */ SQInteger GetOffset() const { - // Validate the handle - SQMOD_VALIDATE(*this); - // Return the requested information - return ConvTo< SQInteger >::From(m_Entry.offset); + return ConvTo< SQInteger >::From(SQMOD_GET_VALID(*this).offset); } /* -------------------------------------------------------------------------------------------- @@ -175,10 +166,7 @@ public: */ SQInteger DataSize() const { - // Validate the handle - SQMOD_VALIDATE(*this); - // Return the requested information - return ConvTo< SQInteger >::From(m_Entry.data_size); + return ConvTo< SQInteger >::From(SQMOD_GET_VALID(*this).data_size); } /* -------------------------------------------------------------------------------------------- @@ -186,10 +174,7 @@ public: */ bool GetBool() const { - // Validate the handle - SQMOD_VALIDATE(*this); - // Return the requested information - return GetEntryAsBool(m_Entry); + return GetEntryAsBool(SQMOD_GET_VALID(*this)); } /* -------------------------------------------------------------------------------------------- @@ -197,10 +182,7 @@ public: */ SQInteger GetInteger() const { - // Validate the handle - SQMOD_VALIDATE(*this); - // Return the requested information - return GetEntryAsInteger(m_Entry); + return GetEntryAsInteger(SQMOD_GET_VALID(*this)); } /* -------------------------------------------------------------------------------------------- @@ -208,10 +190,7 @@ public: */ SQFloat GetFloat() const { - // Validate the handle - SQMOD_VALIDATE(*this); - // Return the requested information - return GetEntryAsFloat(m_Entry); + return GetEntryAsFloat(SQMOD_GET_VALID(*this)); } /* -------------------------------------------------------------------------------------------- @@ -219,10 +198,7 @@ public: */ Object GetLong() const { - // Validate the handle - SQMOD_VALIDATE(*this); - // Return the requested information - return GetEntryAsLong(m_Entry); + return GetEntryAsLong(SQMOD_GET_VALID(*this)); } /* -------------------------------------------------------------------------------------------- @@ -230,10 +206,7 @@ public: */ Object GetString() const { - // Validate the handle - SQMOD_VALIDATE(*this); - // Return the requested information - return GetEntryAsString(m_Entry); + return GetEntryAsString(SQMOD_GET_VALID(*this)); } /* -------------------------------------------------------------------------------------------- @@ -241,12 +214,8 @@ public: */ Object GetBytes() const { - // Validate the handle - SQMOD_VALIDATE(*this); - // Return the requested information - return GetEntryAsBytes(m_Entry); + return GetEntryAsBytes(SQMOD_GET_VALID(*this)); } - }; } // Namespace:: SqMod diff --git a/modules/mmdb/EntryDataList.cpp b/modules/mmdb/EntryDataList.cpp index 71b678bf..dcc78628 100644 --- a/modules/mmdb/EntryDataList.cpp +++ b/modules/mmdb/EntryDataList.cpp @@ -4,8 +4,6 @@ // ------------------------------------------------------------------------------------------------ #include -#include -#include // ------------------------------------------------------------------------------------------------ namespace SqMod { @@ -117,12 +115,10 @@ Uint32 EntryDataList::GetCount() const // ------------------------------------------------------------------------------------------------ bool EntryDataList::Next() { - // Validate the database handle - SQMOD_VALIDATE(*this); // Attempt to fetch the next element - m_Elem = m_Elem ? m_Elem->next : nullptr; + m_Elem = SQMOD_GET_VALID(*this) ? m_Elem->next : nullptr; // Return whether we have a valid element - return !!m_Elem; + return (m_Elem != nullptr); } // ------------------------------------------------------------------------------------------------ @@ -146,7 +142,7 @@ bool EntryDataList::Advance(SQInteger n) void EntryDataList::DumpTo(CSStr filepath, Int32 indent) const { // Validate the database and list handle - SQMOD_VALIDATE(*this); + Pointer ptr = SQMOD_GET_VALID(*this); // Validate the specified file path if (!filepath || *filepath == '\0') { @@ -160,7 +156,7 @@ void EntryDataList::DumpTo(CSStr filepath, Int32 indent) const STHROWF("Unable to open file %s", filepath); } // Attempt to dump the entry data list - Int32 status = MMDB_dump_entry_data_list(fp, m_List, indent); + Int32 status = MMDB_dump_entry_data_list(fp, ptr, indent); // Close the file handle fclose(fp); // Validate the result of the operation diff --git a/modules/mmdb/SearchNode.cpp b/modules/mmdb/SearchNode.cpp index c660bcd8..decd039f 100644 --- a/modules/mmdb/SearchNode.cpp +++ b/modules/mmdb/SearchNode.cpp @@ -41,16 +41,16 @@ void SearchNode::Validate() const // ------------------------------------------------------------------------------------------------ #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) -const DbRef & SearchNode::GetValid(CCStr file, Int32 line) const +SearchNode::Reference SearchNode::GetValid(CCStr file, Int32 line) { Validate(file, line); - return m_Handle; + return m_Node; } #else -const DbRef & SearchNode::GetValid() const +SearchNode::Reference SearchNode::GetValid() { Validate(); - return m_Handle; + return m_Node; } #endif // _DEBUG @@ -77,12 +77,10 @@ Database SearchNode::GetDatabase() const // ------------------------------------------------------------------------------------------------ Object SearchNode::GetLeftRecordEntryDataList() { - // Validate the managed handles - SQMOD_VALIDATE(*this); // Prepare a temporary entry data list pointer MMDB_entry_data_list_s * entry_data_list = nullptr; // Attempt to retrieve the entire entry data list at once - const int status = MMDB_get_entry_data_list(&m_Node.left_record_entry, &entry_data_list); + const int status = MMDB_get_entry_data_list(&(SQMOD_GET_VALID(*this).left_record_entry), &entry_data_list); // Validate the status code if (status != MMDB_SUCCESS) { @@ -95,12 +93,10 @@ Object SearchNode::GetLeftRecordEntryDataList() // ------------------------------------------------------------------------------------------------ Object SearchNode::GetRightRecordEntryDataList() { - // Validate the managed handles - SQMOD_VALIDATE(*this); // Prepare a temporary entry data list pointer MMDB_entry_data_list_s * entry_data_list = nullptr; // Attempt to retrieve the entire entry data list at once - const int status = MMDB_get_entry_data_list(&m_Node.right_record_entry, &entry_data_list); + const int status = MMDB_get_entry_data_list(&(SQMOD_GET_VALID(*this).right_record_entry), &entry_data_list); // Validate the status code if (status != MMDB_SUCCESS) { diff --git a/modules/mmdb/SearchNode.hpp b/modules/mmdb/SearchNode.hpp index 3c0a7cdf..d9f73173 100644 --- a/modules/mmdb/SearchNode.hpp +++ b/modules/mmdb/SearchNode.hpp @@ -40,9 +40,9 @@ protected: * Validate the managed database handle and throw an error if invalid. */ #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) - const DbRef & GetValid(CCStr file, Int32 line) const; + Reference GetValid(CCStr file, Int32 line); #else - const DbRef & GetValid() const; + Reference GetValid(); #endif // _DEBUG private: @@ -130,14 +130,12 @@ public: /* -------------------------------------------------------------------------------------------- * Retrieve the left record value. */ - Object GetLeftRecord() const + Object GetLeftRecord() { - // Validate the managed handles - SQMOD_VALIDATE(*this); // Obtain the initial stack size const StackGuard sg; // Push a long integer instance with the requested value on the stack - SqMod_PushULongObject(DefaultVM::Get(), ConvTo< Uint64 >::From(m_Node.left_record)); + SqMod_PushULongObject(DefaultVM::Get(), ConvTo< Uint64 >::From(SQMOD_GET_VALID(*this).left_record)); // Obtain the object from the stack and return it return Var< Object >(DefaultVM::Get(), -1).value; } @@ -145,14 +143,12 @@ public: /* -------------------------------------------------------------------------------------------- * Retrieve the right record value. */ - Object GetRightRecord() const + Object GetRightRecord() { - // Validate the managed handles - SQMOD_VALIDATE(*this); // Obtain the initial stack size const StackGuard sg; // Push a long integer instance with the requested value on the stack - SqMod_PushULongObject(DefaultVM::Get(), ConvTo< Uint64 >::From(m_Node.right_record)); + SqMod_PushULongObject(DefaultVM::Get(), ConvTo< Uint64 >::From(SQMOD_GET_VALID(*this).right_record)); // Obtain the object from the stack and return it return Var< Object >(DefaultVM::Get(), -1).value; } @@ -160,23 +156,17 @@ public: /* -------------------------------------------------------------------------------------------- * Retrieve the left record value type. */ - SQInteger GetLeftRecordType() const + SQInteger GetLeftRecordType() { - // Validate the managed handles - SQMOD_VALIDATE(*this); - // Return the requested information - return static_cast< SQInteger >(m_Node.left_record_type); + return static_cast< SQInteger >(SQMOD_GET_VALID(*this).left_record_type); } /* -------------------------------------------------------------------------------------------- * Retrieve the right record value type. */ - SQInteger GetRightRecordType() const + SQInteger GetRightRecordType() { - // Validate the managed handles - SQMOD_VALIDATE(*this); - // Return the requested information - return static_cast< SQInteger >(m_Node.right_record_type); + return static_cast< SQInteger >(SQMOD_GET_VALID(*this).right_record_type); } /* --------------------------------------------------------------------------------------------