Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some safety improvement changes #59

Merged
merged 9 commits into from
Jul 7, 2019
57 changes: 42 additions & 15 deletions include/wx/wxsqlite3.h
Original file line number Diff line number Diff line change
Expand Up @@ -2326,18 +2326,13 @@ class WXDLLIMPEXP_SQLITE3 wxSQLite3Blob
class WXDLLIMPEXP_SQLITE3 wxSQLite3NamedCollection
{
public:
/// Constructor
wxSQLite3NamedCollection();

/// Copy constructor
wxSQLite3NamedCollection(const wxSQLite3NamedCollection& collection);

/// Assignment constructor
wxSQLite3NamedCollection& operator=(const wxSQLite3NamedCollection& collection);

/// Constructor (internal use only)
wxSQLite3NamedCollection(const wxString& collectionName, void* collectionData);

/// Destructor
virtual ~wxSQLite3NamedCollection();

Expand All @@ -2347,29 +2342,51 @@ class WXDLLIMPEXP_SQLITE3 wxSQLite3NamedCollection
*/
const wxString& GetName() const { return m_name; }

/// Gets state of object
/**
* \return state of object
*/
bool IsOk() const { return (m_data != NULL); }

/// Gets state of object (same as IsOk() method)
/**
* \return state of object
*/
operator bool() const { return IsOk(); }

protected:
wxString m_name; ///< Name of the collection
void* m_data; ///< Reference to the actual array of values representing the collection

/// Constructor (internal use only)
wxSQLite3NamedCollection(const wxString& collectionName, void* collectionData);

/// Default constructor
/**
Creates fully empty object that must be set by assignment, be careful
*/
wxSQLite3NamedCollection() : m_name(wxEmptyString), m_data(NULL) {}

friend class wxSQLite3Database;
};

/// Represents a named integer value collection
class WXDLLIMPEXP_SQLITE3 wxSQLite3IntegerCollection : public wxSQLite3NamedCollection
{
public:
/// Constructor
wxSQLite3IntegerCollection();

/// Default constructor
/**
Creates fully empty object that must be set by assignment, be careful
*/
wxSQLite3IntegerCollection() {}

/// Copy constructor
wxSQLite3IntegerCollection(const wxSQLite3IntegerCollection& collection);

/// Assignment constructor
wxSQLite3IntegerCollection& operator=(const wxSQLite3IntegerCollection& collection);

/// Constructor (internal use only)
wxSQLite3IntegerCollection(const wxString& collectionName, void* collectionData);

/// Destructor
virtual ~wxSQLite3IntegerCollection();

Expand All @@ -2392,6 +2409,11 @@ class WXDLLIMPEXP_SQLITE3 wxSQLite3IntegerCollection : public wxSQLite3NamedColl
*/
void Bind(int n, int* integerCollection);

protected:

/// Constructor (internal use only)
wxSQLite3IntegerCollection(const wxString& collectionName, void* collectionData);

private:
friend class wxSQLite3Database;
};
Expand All @@ -2400,18 +2422,19 @@ class WXDLLIMPEXP_SQLITE3 wxSQLite3IntegerCollection : public wxSQLite3NamedColl
class WXDLLIMPEXP_SQLITE3 wxSQLite3StringCollection : public wxSQLite3NamedCollection
{
public:
/// Constructor
wxSQLite3StringCollection();

/// Default constructor
/**
Creates fully empty object that must be set by assignment, be careful
*/
wxSQLite3StringCollection() {}

/// Copy constructor
wxSQLite3StringCollection(const wxSQLite3StringCollection& collection);

/// Assignment constructor
wxSQLite3StringCollection& operator=(const wxSQLite3StringCollection& collection);

/// Constructor (internal use only)
wxSQLite3StringCollection(const wxString& collectionName, void* collectionData);

/// Destructor
virtual ~wxSQLite3StringCollection();

Expand All @@ -2424,6 +2447,10 @@ class WXDLLIMPEXP_SQLITE3 wxSQLite3StringCollection : public wxSQLite3NamedColle
*/
void Bind(const wxArrayString& stringCollection);

protected:
/// Constructor (internal use only)
wxSQLite3StringCollection(const wxString& collectionName, void* collectionData);

private:
friend class wxSQLite3Database;
};
Expand Down
4 changes: 2 additions & 2 deletions sqlite3secure/src/chacha20poly1305.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static void chacha20_block(unsigned char out[64], const uint32_t in[16])
void chacha20_xor(unsigned char* data, size_t n, const unsigned char key[32],
const unsigned char nonce[12], uint32_t counter)
{
int i;
size_t i;
uint32_t state[16];
unsigned char block[64];
static const unsigned char sigma[16] = "expand 32-byte k";
Expand Down Expand Up @@ -184,7 +184,7 @@ void poly1305(const unsigned char* msg, size_t n, const unsigned char key[32],
}
if (n)
{
int i;
size_t i;
for (i = 0; i < n; i++) buf[i] = msg[i];
buf[i++] = 1;
while (i < 16) buf[i++] = 0;
Expand Down
62 changes: 29 additions & 33 deletions src/wxsqlite3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ const char* wxERRMSG_DBASSIGN_FAILED = wxTRANSLATE("Database assignment failed")
const char* wxERRMSG_FINALIZE_FAILED = wxTRANSLATE("Finalize failed");

const char* wxERRMSG_CIPHER_APPLY_FAILED = wxTRANSLATE("Application of cipher failed");

const char* wxERRMSG_CORRUPTED_STATE = wxTRANSLATE("Collection object state is not properly initialized");
#else
const wxChar* wxERRMSG_NODB = wxTRANSLATE("No Database opened");
const wxChar* wxERRMSG_NOSTMT = wxTRANSLATE("Statement not accessible");
Expand Down Expand Up @@ -246,6 +248,8 @@ const wxChar* wxERRMSG_DBASSIGN_FAILED = wxTRANSLATE("Database assignment failed
const wxChar* wxERRMSG_FINALIZE_FAILED = wxTRANSLATE("Finalize failed");

const wxChar* wxERRMSG_CIPHER_APPLY_FAILED = wxTRANSLATE("Application of cipher failed");

const wxChar* wxERRMSG_CORRUPTED_STATE = wxTRANSLATE("Collection object state is not properly initialized");
#endif

static const char* LocalMakePointerTypeCopy(wxArrayPtrVoid& ptrTypes, const wxString& pointerType)
Expand Down Expand Up @@ -5534,16 +5538,9 @@ static sqlite3_module chararrayModule =

#endif // WXSQLITE3_USE_NAMED_COLLECTIONS

wxSQLite3NamedCollection::wxSQLite3NamedCollection()
{
m_name = wxEmptyString;
m_data = NULL;
}

wxSQLite3NamedCollection::wxSQLite3NamedCollection(const wxString& collectionName, void* collectionData)
: m_name(collectionName), m_data(collectionData)
{
m_name = collectionName;
m_data = collectionData;
}

wxSQLite3NamedCollection::wxSQLite3NamedCollection(const wxSQLite3NamedCollection& collection)
Expand All @@ -5566,11 +5563,6 @@ wxSQLite3NamedCollection::~wxSQLite3NamedCollection()
{
}

wxSQLite3IntegerCollection::wxSQLite3IntegerCollection()
: wxSQLite3NamedCollection(wxEmptyString, NULL)
{
}

wxSQLite3IntegerCollection::wxSQLite3IntegerCollection(const wxSQLite3IntegerCollection& collection)
: wxSQLite3NamedCollection(collection)
{
Expand Down Expand Up @@ -5598,14 +5590,17 @@ wxSQLite3IntegerCollection::~wxSQLite3IntegerCollection()
void
wxSQLite3IntegerCollection::Bind(const wxArrayInt& integerCollection)
{
if (!IsOk())
{
throw wxSQLite3Exception(WXSQLITE_ERROR, wxERRMSG_CORRUPTED_STATE);
}

size_t n = integerCollection.Count();
sqlite3_intarray* pIntArray = (sqlite3_intarray*) m_data;
if (m_data != NULL)

if (pIntArray->a != NULL && pIntArray->xFree)
{
if (pIntArray->a != NULL && pIntArray->xFree)
{
pIntArray->xFree(pIntArray->a);
}
pIntArray->xFree(pIntArray->a);
}
pIntArray->n = n;
if (n > 0)
Expand All @@ -5629,13 +5624,16 @@ wxSQLite3IntegerCollection::Bind(const wxArrayInt& integerCollection)
void
wxSQLite3IntegerCollection::Bind(int n, int* integerCollection)
{
if (!IsOk())
{
throw wxSQLite3Exception(WXSQLITE_ERROR, wxERRMSG_CORRUPTED_STATE);
}

sqlite3_intarray* pIntArray = (sqlite3_intarray*) m_data;
if (m_data != NULL)

if (pIntArray->a != NULL && pIntArray->xFree)
{
if (pIntArray->a != NULL && pIntArray->xFree)
{
pIntArray->xFree(pIntArray->a);
}
pIntArray->xFree(pIntArray->a);
}
pIntArray->n = n;
if (n > 0)
Expand Down Expand Up @@ -5691,11 +5689,6 @@ wxSQLite3Database::CreateIntegerCollection(const wxString& collectionName)
#endif // WXSQLITE3_USE_NAMED_COLLECTIONS
}

wxSQLite3StringCollection::wxSQLite3StringCollection()
: wxSQLite3NamedCollection(wxEmptyString, NULL)
{
}

wxSQLite3StringCollection::wxSQLite3StringCollection(const wxSQLite3StringCollection& collection)
: wxSQLite3NamedCollection(collection)
{
Expand Down Expand Up @@ -5723,14 +5716,17 @@ wxSQLite3StringCollection::~wxSQLite3StringCollection()
void
wxSQLite3StringCollection::Bind(const wxArrayString& stringCollection)
{
if (!IsOk())
{
throw wxSQLite3Exception(WXSQLITE_ERROR, wxERRMSG_CORRUPTED_STATE);
}

size_t n = stringCollection.Count();
sqlite3_chararray* pCharArray = (sqlite3_chararray*) m_data;
if (m_data != NULL)

if (pCharArray->a != NULL && pCharArray->xFree)
{
if (pCharArray->a != NULL && pCharArray->xFree)
{
pCharArray->xFree(pCharArray->a);
}
pCharArray->xFree(pCharArray->a);
}
pCharArray->n = n;
if (n > 0)
Expand Down