(merging from branch) fixed amb1583 - MySQL string fetch from prepared queries returned corrupted data.
authorDavid Anderson <dvander@alliedmods.net>
Thu May 29 04:47:42 2008 +0000 (2008-05-29)
changeset 2092204db878bcfa
parent 2091 9ec85886cc53
child 2093 025abfa950bb
(merging from branch) fixed amb1583 - MySQL string fetch from prepared queries returned corrupted data.
extensions/mysql/mysql/MyBoundResults.cpp
extensions/mysql/mysql/MyBoundResults.h
     1.1 --- a/extensions/mysql/mysql/MyBoundResults.cpp	Thu May 29 03:19:55 2008 +0000
     1.2 +++ b/extensions/mysql/mysql/MyBoundResults.cpp	Thu May 29 04:47:42 2008 +0000
     1.3 @@ -89,6 +89,8 @@
     1.4  	/* Zero data */
     1.5  	memset(m_bind, 0, sizeof(MYSQL_BIND) * m_ColCount);
     1.6  	memset(m_pull, 0, sizeof(ResultBind) * m_ColCount);
     1.7 +
     1.8 +	m_bUpdatedBinds = false;
     1.9  }
    1.10  
    1.11  MyBoundResults::~MyBoundResults()
    1.12 @@ -211,6 +213,15 @@
    1.13  
    1.14  	m_CurRow++;
    1.15  
    1.16 +	if (m_bUpdatedBinds)
    1.17 +	{
    1.18 +		if (mysql_stmt_bind_result(m_stmt, m_bind) != 0)
    1.19 +		{
    1.20 +			return false;
    1.21 +		}
    1.22 +		m_bUpdatedBinds = false;
    1.23 +	}
    1.24 +
    1.25  	/* We should be able to get another row */
    1.26  	int err = mysql_stmt_fetch(m_stmt);
    1.27  	if (err == 0 || err == MYSQL_DATA_TRUNCATED)
    1.28 @@ -266,22 +277,29 @@
    1.29  	{
    1.30  		bind->blob = new unsigned char[len];
    1.31  		bind->length = len;
    1.32 -	} else if (bind->length < len) {
    1.33 +	}
    1.34 +	else if (bind->length < len)
    1.35 +	{
    1.36  		delete [] bind->blob;
    1.37  		bind->blob = new unsigned char[len];
    1.38  		bind->length = len;
    1.39  	}
    1.40  }
    1.41  
    1.42 -bool RefetchField(MYSQL_STMT *stmt, 
    1.43 -				  ResultBind *rbind, 
    1.44 +bool MyBoundResults::RefetchField(MYSQL_STMT *stmt, 
    1.45  				  unsigned int id,
    1.46  				  size_t initSize,
    1.47  				  enum_field_types type)
    1.48  {
    1.49 +	ResultBind *rbind = &m_pull[id];
    1.50 +
    1.51  	/* Make sure there is a buffer to pull into */
    1.52  	ResizeBuffer(rbind, initSize);
    1.53  
    1.54 +	/* Update the bind with the buffer size */
    1.55 +	m_bind[id].buffer_length = (unsigned long)rbind->length;
    1.56 +	m_bUpdatedBinds = true;
    1.57 +
    1.58  	MYSQL_BIND bind;
    1.59  
    1.60  	/* Initialize bind info */
    1.61 @@ -397,7 +415,7 @@
    1.62  		}
    1.63  
    1.64  		/* Attempt to refetch the string */
    1.65 -		if (!RefetchField(m_stmt, &m_pull[id], id, 128, MYSQL_TYPE_STRING))
    1.66 +		if (!RefetchField(m_stmt, id, 128, MYSQL_TYPE_STRING))
    1.67  		{
    1.68  			return DBVal_TypeMismatch;
    1.69  		}
    1.70 @@ -418,7 +436,7 @@
    1.71  	if ((size_t)(m_pull[id].my_length) >= m_pull[id].length)
    1.72  	{
    1.73  		/* Yes, we need to refetch. */
    1.74 -		if (!RefetchField(m_stmt, &m_pull[id], id, m_pull[id].my_length + 1, MYSQL_TYPE_STRING))
    1.75 +		if (!RefetchField(m_stmt, id, m_pull[id].my_length + 1, MYSQL_TYPE_STRING))
    1.76  		{
    1.77  			return DBVal_Error;
    1.78  		}
    1.79 @@ -591,7 +609,7 @@
    1.80  
    1.81  	if ((size_t)m_pull[id].my_length > m_pull[id].length)
    1.82  	{
    1.83 -		if (!RefetchField(m_stmt, &m_pull[id], id, m_pull[id].my_length, MYSQL_TYPE_BLOB))
    1.84 +		if (!RefetchField(m_stmt, id, m_pull[id].my_length, MYSQL_TYPE_BLOB))
    1.85  		{
    1.86  			return DBVal_TypeMismatch;
    1.87  		}
     2.1 --- a/extensions/mysql/mysql/MyBoundResults.h	Thu May 29 03:19:55 2008 +0000
     2.2 +++ b/extensions/mysql/mysql/MyBoundResults.h	Thu May 29 04:47:42 2008 +0000
     2.3 @@ -2,7 +2,7 @@
     2.4   * vim: set ts=4 :
     2.5   * =============================================================================
     2.6   * SourceMod MySQL Extension
     2.7 - * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     2.8 + * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     2.9   * =============================================================================
    2.10   *
    2.11   * This program is free software; you can redistribute it and/or modify it under
    2.12 @@ -84,6 +84,11 @@
    2.13  	bool Initialize();
    2.14  	void Update();
    2.15  private:
    2.16 +	bool RefetchField(MYSQL_STMT *stmt, 
    2.17 +		unsigned int id,
    2.18 +		size_t initSize,
    2.19 +		enum_field_types type);
    2.20 +private:
    2.21  	MYSQL_STMT *m_stmt;
    2.22  	MYSQL_RES *m_pRes;
    2.23  	MYSQL_BIND *m_bind;
    2.24 @@ -92,6 +97,7 @@
    2.25  	bool m_Initialized;
    2.26  	unsigned int m_RowCount;
    2.27  	unsigned int m_CurRow;
    2.28 +	bool m_bUpdatedBinds;
    2.29  };
    2.30  
    2.31  #endif //_INCLUDE_SM_MYSQL_BOUND_RESULTS_H_