1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-04-19 10:47:13 +02:00

Extra validation code in the MaxmindDB module.

This commit is contained in:
Sandu Liviu Catalin 2016-11-15 06:34:10 +02:00
parent 5cadbc82b8
commit 2b282951af
6 changed files with 50 additions and 85 deletions

View File

@ -30,10 +30,12 @@ namespace SqMod {
#define SQMOD_VALIDATE(x) (x).Validate(__FILE__, __LINE__) #define SQMOD_VALIDATE(x) (x).Validate(__FILE__, __LINE__)
#define SQMOD_GET_VALID(x) (x).GetValid(__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_ELEM(x) (x).GetValidElem(__FILE__, __LINE__)
#define SQMOD_GET_VALID_DATA(x) (x).GetValidData(__FILE__, __LINE__)
#else #else
#define SQMOD_VALIDATE(x) (x).Validate() #define SQMOD_VALIDATE(x) (x).Validate()
#define SQMOD_GET_VALID(x) (x).GetValid() #define SQMOD_GET_VALID(x) (x).GetValid()
#define SQMOD_GET_VALID_ELEM(x) (x).GetValidElem() #define SQMOD_GET_VALID_ELEM(x) (x).GetValidElem()
#define SQMOD_GET_VALID_DATA(x) (x).GetValidData()
#endif // _DEBUG #endif // _DEBUG
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------

View File

@ -38,16 +38,28 @@ void EntryData::Validate() const
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #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); 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 #else
const DbRef & EntryData::GetValid() const EntryData::ConstRef EntryData::GetValid() const
{ {
Validate(); 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 #endif // _DEBUG

View File

@ -40,9 +40,9 @@ protected:
* Validate the managed database handle and throw an error if invalid. * Validate the managed database handle and throw an error if invalid.
*/ */
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
const DbRef & GetValid(CCStr file, Int32 line) const; ConstRef GetValid(CCStr file, Int32 line) const;
#else #else
const DbRef & GetValid() const; ConstRef GetValid() const;
#endif // _DEBUG #endif // _DEBUG
private: private:
@ -142,10 +142,7 @@ public:
*/ */
bool HasData() const bool HasData() const
{ {
// Validate the handle return ConvTo< bool >::From(SQMOD_GET_VALID(*this).has_data);
SQMOD_VALIDATE(*this);
// Return the requested information
return ConvTo< bool >::From(m_Entry.has_data);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -153,10 +150,7 @@ public:
*/ */
SQInteger GetType() const SQInteger GetType() const
{ {
// Validate the handle return ConvTo< SQInteger >::From(SQMOD_GET_VALID(*this).type);
SQMOD_VALIDATE(*this);
// Return the requested information
return ConvTo< SQInteger >::From(m_Entry.type);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -164,10 +158,7 @@ public:
*/ */
SQInteger GetOffset() const SQInteger GetOffset() const
{ {
// Validate the handle return ConvTo< SQInteger >::From(SQMOD_GET_VALID(*this).offset);
SQMOD_VALIDATE(*this);
// Return the requested information
return ConvTo< SQInteger >::From(m_Entry.offset);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -175,10 +166,7 @@ public:
*/ */
SQInteger DataSize() const SQInteger DataSize() const
{ {
// Validate the handle return ConvTo< SQInteger >::From(SQMOD_GET_VALID(*this).data_size);
SQMOD_VALIDATE(*this);
// Return the requested information
return ConvTo< SQInteger >::From(m_Entry.data_size);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -186,10 +174,7 @@ public:
*/ */
bool GetBool() const bool GetBool() const
{ {
// Validate the handle return GetEntryAsBool(SQMOD_GET_VALID(*this));
SQMOD_VALIDATE(*this);
// Return the requested information
return GetEntryAsBool(m_Entry);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -197,10 +182,7 @@ public:
*/ */
SQInteger GetInteger() const SQInteger GetInteger() const
{ {
// Validate the handle return GetEntryAsInteger(SQMOD_GET_VALID(*this));
SQMOD_VALIDATE(*this);
// Return the requested information
return GetEntryAsInteger(m_Entry);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -208,10 +190,7 @@ public:
*/ */
SQFloat GetFloat() const SQFloat GetFloat() const
{ {
// Validate the handle return GetEntryAsFloat(SQMOD_GET_VALID(*this));
SQMOD_VALIDATE(*this);
// Return the requested information
return GetEntryAsFloat(m_Entry);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -219,10 +198,7 @@ public:
*/ */
Object GetLong() const Object GetLong() const
{ {
// Validate the handle return GetEntryAsLong(SQMOD_GET_VALID(*this));
SQMOD_VALIDATE(*this);
// Return the requested information
return GetEntryAsLong(m_Entry);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -230,10 +206,7 @@ public:
*/ */
Object GetString() const Object GetString() const
{ {
// Validate the handle return GetEntryAsString(SQMOD_GET_VALID(*this));
SQMOD_VALIDATE(*this);
// Return the requested information
return GetEntryAsString(m_Entry);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -241,12 +214,8 @@ public:
*/ */
Object GetBytes() const Object GetBytes() const
{ {
// Validate the handle return GetEntryAsBytes(SQMOD_GET_VALID(*this));
SQMOD_VALIDATE(*this);
// Return the requested information
return GetEntryAsBytes(m_Entry);
} }
}; };
} // Namespace:: SqMod } // Namespace:: SqMod

View File

@ -4,8 +4,6 @@
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include <cstdio> #include <cstdio>
#include <cstdlib>
#include <cstring>
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
namespace SqMod { namespace SqMod {
@ -117,12 +115,10 @@ Uint32 EntryDataList::GetCount() const
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
bool EntryDataList::Next() bool EntryDataList::Next()
{ {
// Validate the database handle
SQMOD_VALIDATE(*this);
// Attempt to fetch the next element // 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 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 void EntryDataList::DumpTo(CSStr filepath, Int32 indent) const
{ {
// Validate the database and list handle // Validate the database and list handle
SQMOD_VALIDATE(*this); Pointer ptr = SQMOD_GET_VALID(*this);
// Validate the specified file path // Validate the specified file path
if (!filepath || *filepath == '\0') if (!filepath || *filepath == '\0')
{ {
@ -160,7 +156,7 @@ void EntryDataList::DumpTo(CSStr filepath, Int32 indent) const
STHROWF("Unable to open file %s", filepath); STHROWF("Unable to open file %s", filepath);
} }
// Attempt to dump the entry data list // 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 // Close the file handle
fclose(fp); fclose(fp);
// Validate the result of the operation // Validate the result of the operation

View File

@ -41,16 +41,16 @@ void SearchNode::Validate() const
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #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); Validate(file, line);
return m_Handle; return m_Node;
} }
#else #else
const DbRef & SearchNode::GetValid() const SearchNode::Reference SearchNode::GetValid()
{ {
Validate(); Validate();
return m_Handle; return m_Node;
} }
#endif // _DEBUG #endif // _DEBUG
@ -77,12 +77,10 @@ Database SearchNode::GetDatabase() const
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
Object SearchNode::GetLeftRecordEntryDataList() Object SearchNode::GetLeftRecordEntryDataList()
{ {
// Validate the managed handles
SQMOD_VALIDATE(*this);
// Prepare a temporary entry data list pointer // Prepare a temporary entry data list pointer
MMDB_entry_data_list_s * entry_data_list = nullptr; MMDB_entry_data_list_s * entry_data_list = nullptr;
// Attempt to retrieve the entire entry data list at once // 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 // Validate the status code
if (status != MMDB_SUCCESS) if (status != MMDB_SUCCESS)
{ {
@ -95,12 +93,10 @@ Object SearchNode::GetLeftRecordEntryDataList()
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
Object SearchNode::GetRightRecordEntryDataList() Object SearchNode::GetRightRecordEntryDataList()
{ {
// Validate the managed handles
SQMOD_VALIDATE(*this);
// Prepare a temporary entry data list pointer // Prepare a temporary entry data list pointer
MMDB_entry_data_list_s * entry_data_list = nullptr; MMDB_entry_data_list_s * entry_data_list = nullptr;
// Attempt to retrieve the entire entry data list at once // 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 // Validate the status code
if (status != MMDB_SUCCESS) if (status != MMDB_SUCCESS)
{ {

View File

@ -40,9 +40,9 @@ protected:
* Validate the managed database handle and throw an error if invalid. * Validate the managed database handle and throw an error if invalid.
*/ */
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
const DbRef & GetValid(CCStr file, Int32 line) const; Reference GetValid(CCStr file, Int32 line);
#else #else
const DbRef & GetValid() const; Reference GetValid();
#endif // _DEBUG #endif // _DEBUG
private: private:
@ -130,14 +130,12 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Retrieve the left record value. * Retrieve the left record value.
*/ */
Object GetLeftRecord() const Object GetLeftRecord()
{ {
// Validate the managed handles
SQMOD_VALIDATE(*this);
// Obtain the initial stack size // Obtain the initial stack size
const StackGuard sg; const StackGuard sg;
// Push a long integer instance with the requested value on the stack // 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 // Obtain the object from the stack and return it
return Var< Object >(DefaultVM::Get(), -1).value; return Var< Object >(DefaultVM::Get(), -1).value;
} }
@ -145,14 +143,12 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Retrieve the right record value. * Retrieve the right record value.
*/ */
Object GetRightRecord() const Object GetRightRecord()
{ {
// Validate the managed handles
SQMOD_VALIDATE(*this);
// Obtain the initial stack size // Obtain the initial stack size
const StackGuard sg; const StackGuard sg;
// Push a long integer instance with the requested value on the stack // 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 // Obtain the object from the stack and return it
return Var< Object >(DefaultVM::Get(), -1).value; return Var< Object >(DefaultVM::Get(), -1).value;
} }
@ -160,23 +156,17 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Retrieve the left record value type. * Retrieve the left record value type.
*/ */
SQInteger GetLeftRecordType() const SQInteger GetLeftRecordType()
{ {
// Validate the managed handles return static_cast< SQInteger >(SQMOD_GET_VALID(*this).left_record_type);
SQMOD_VALIDATE(*this);
// Return the requested information
return static_cast< SQInteger >(m_Node.left_record_type);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Retrieve the right record value type. * Retrieve the right record value type.
*/ */
SQInteger GetRightRecordType() const SQInteger GetRightRecordType()
{ {
// Validate the managed handles return static_cast< SQInteger >(SQMOD_GET_VALID(*this).right_record_type);
SQMOD_VALIDATE(*this);
// Return the requested information
return static_cast< SQInteger >(m_Node.right_record_type);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------