Skip to content

Commit 4a4978d

Browse files
authoredOct 22, 2018
Add missing checks for connection before calling mysql APIs (#272)
Fixes #270
·
v2.2.71.3.14
1 parent 54c6943 commit 4a4978d

File tree

1 file changed

+55
-47
lines changed

1 file changed

+55
-47
lines changed
 

‎_mysql.c

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,14 @@ typedef struct {
6969
PyObject *converter;
7070
} _mysql_ConnectionObject;
7171

72-
#define check_connection(c) if (!(c->open)) return _mysql_Exception(c)
72+
#define check_connection(c, func) \
73+
if (!(c->open)) { \
74+
PyErr_SetString(_mysql_ProgrammingError, func "() is called for closed connection"); \
75+
return NULL; \
76+
};
77+
7378
#define result_connection(r) ((_mysql_ConnectionObject *)r->conn)
74-
#define check_result_connection(r) check_connection(result_connection(r))
79+
#define check_result_connection(r, func) check_connection(result_connection(r), func)
7580

7681
extern PyTypeObject _mysql_ConnectionObject_Type;
7782

@@ -750,6 +755,7 @@ static PyObject *
750755
_mysql_ConnectionObject_fileno(
751756
_mysql_ConnectionObject *self)
752757
{
758+
check_connection(self, "fileno");
753759
return PyInt_FromLong(self->connection.net.fd);
754760
}
755761

@@ -761,16 +767,11 @@ _mysql_ConnectionObject_close(
761767
_mysql_ConnectionObject *self,
762768
PyObject *noargs)
763769
{
764-
if (self->open) {
765-
Py_BEGIN_ALLOW_THREADS
766-
mysql_close(&(self->connection));
767-
Py_END_ALLOW_THREADS
768-
self->open = 0;
769-
} else {
770-
PyErr_SetString(_mysql_ProgrammingError,
771-
"closing a closed connection");
772-
return NULL;
773-
}
770+
check_connection(self, "close");
771+
Py_BEGIN_ALLOW_THREADS
772+
mysql_close(&(self->connection));
773+
Py_END_ALLOW_THREADS
774+
self->open = 0;
774775
_mysql_ConnectionObject_clear(self);
775776
Py_RETURN_NONE;
776777
}
@@ -786,7 +787,7 @@ _mysql_ConnectionObject_affected_rows(
786787
PyObject *noargs)
787788
{
788789
my_ulonglong ret;
789-
check_connection(self);
790+
check_connection(self, "affected_rows");
790791
ret = mysql_affected_rows(&(self->connection));
791792
if (ret == (my_ulonglong)-1)
792793
return PyInt_FromLong(-1);
@@ -823,7 +824,7 @@ _mysql_ConnectionObject_dump_debug_info(
823824
PyObject *noargs)
824825
{
825826
int err;
826-
check_connection(self);
827+
check_connection(self, "dump_debug_info");
827828
Py_BEGIN_ALLOW_THREADS
828829
err = mysql_dump_debug_info(&(self->connection));
829830
Py_END_ALLOW_THREADS
@@ -842,6 +843,7 @@ _mysql_ConnectionObject_autocommit(
842843
{
843844
int flag, err;
844845
if (!PyArg_ParseTuple(args, "i", &flag)) return NULL;
846+
check_connection(self, "autocommit");
845847
Py_BEGIN_ALLOW_THREADS
846848
err = mysql_autocommit(&(self->connection), flag);
847849
Py_END_ALLOW_THREADS
@@ -858,6 +860,7 @@ _mysql_ConnectionObject_get_autocommit(
858860
_mysql_ConnectionObject *self,
859861
PyObject *args)
860862
{
863+
check_connection(self, "get_autocommit");
861864
if (self->connection.server_status & SERVER_STATUS_AUTOCOMMIT) {
862865
Py_RETURN_TRUE;
863866
}
@@ -873,6 +876,7 @@ _mysql_ConnectionObject_commit(
873876
PyObject *noargs)
874877
{
875878
int err;
879+
check_connection(self, "commit");
876880
Py_BEGIN_ALLOW_THREADS
877881
err = mysql_commit(&(self->connection));
878882
Py_END_ALLOW_THREADS
@@ -890,13 +894,13 @@ _mysql_ConnectionObject_rollback(
890894
PyObject *noargs)
891895
{
892896
int err;
897+
check_connection(self, "rollback");
893898
Py_BEGIN_ALLOW_THREADS
894899
err = mysql_rollback(&(self->connection));
895900
Py_END_ALLOW_THREADS
896901
if (err) return _mysql_Exception(self);
897-
Py_INCREF(Py_None);
898-
return Py_None;
899-
}
902+
Py_RETURN_NONE;
903+
}
900904

901905
static char _mysql_ConnectionObject_next_result__doc__[] =
902906
"If more query results exist, next_result() reads the next query\n\
@@ -917,6 +921,7 @@ _mysql_ConnectionObject_next_result(
917921
PyObject *noargs)
918922
{
919923
int err;
924+
check_connection(self, "next_result");
920925
Py_BEGIN_ALLOW_THREADS
921926
err = mysql_next_result(&(self->connection));
922927
Py_END_ALLOW_THREADS
@@ -939,6 +944,7 @@ _mysql_ConnectionObject_set_server_option(
939944
int err, flags=0;
940945
if (!PyArg_ParseTuple(args, "i", &flags))
941946
return NULL;
947+
check_connection(self, "set_server_option");
942948
Py_BEGIN_ALLOW_THREADS
943949
err = mysql_set_server_option(&(self->connection), flags);
944950
Py_END_ALLOW_THREADS
@@ -963,6 +969,7 @@ _mysql_ConnectionObject_sqlstate(
963969
_mysql_ConnectionObject *self,
964970
PyObject *noargs)
965971
{
972+
check_connection(self, "sqlstate");
966973
return PyString_FromString(mysql_sqlstate(&(self->connection)));
967974
}
968975

@@ -977,6 +984,7 @@ _mysql_ConnectionObject_warning_count(
977984
_mysql_ConnectionObject *self,
978985
PyObject *noargs)
979986
{
987+
check_connection(self, "warning_count");
980988
return PyInt_FromLong(mysql_warning_count(&(self->connection)));
981989
}
982990

@@ -991,7 +999,7 @@ _mysql_ConnectionObject_errno(
991999
_mysql_ConnectionObject *self,
9921000
PyObject *noargs)
9931001
{
994-
check_connection(self);
1002+
check_connection(self, "errno");
9951003
return PyInt_FromLong((long)mysql_errno(&(self->connection)));
9961004
}
9971005

@@ -1006,7 +1014,7 @@ _mysql_ConnectionObject_error(
10061014
_mysql_ConnectionObject *self,
10071015
PyObject *noargs)
10081016
{
1009-
check_connection(self);
1017+
check_connection(self, "error");
10101018
return PyString_FromString(mysql_error(&(self->connection)));
10111019
}
10121020

@@ -1249,7 +1257,7 @@ _mysql_ResultObject_describe(
12491257
PyObject *d;
12501258
MYSQL_FIELD *fields;
12511259
unsigned int i, n;
1252-
check_result_connection(self);
1260+
check_result_connection(self, "describe");
12531261
n = mysql_num_fields(self->result);
12541262
fields = mysql_fetch_fields(self->result);
12551263
if (!(d = PyTuple_New(n))) return NULL;
@@ -1284,7 +1292,7 @@ _mysql_ResultObject_field_flags(
12841292
PyObject *d;
12851293
MYSQL_FIELD *fields;
12861294
unsigned int i, n;
1287-
check_result_connection(self);
1295+
check_result_connection(self, "field_flags");
12881296
n = mysql_num_fields(self->result);
12891297
fields = mysql_fetch_fields(self->result);
12901298
if (!(d = PyTuple_New(n))) return NULL;
@@ -1523,7 +1531,7 @@ _mysql_ResultObject_fetch_row(
15231531
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii:fetch_row", kwlist,
15241532
&maxrows, &how))
15251533
return NULL;
1526-
check_result_connection(self);
1534+
check_result_connection(self, "fetch_row");
15271535
if (how >= (int)sizeof(row_converters)) {
15281536
PyErr_SetString(PyExc_ValueError, "how out of range");
15291537
return NULL;
@@ -1592,7 +1600,7 @@ _mysql_ConnectionObject_change_user(
15921600
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ss:change_user",
15931601
kwlist, &user, &pwd, &db))
15941602
return NULL;
1595-
check_connection(self);
1603+
check_connection(self, "change_user");
15961604
Py_BEGIN_ALLOW_THREADS
15971605
r = mysql_change_user(&(self->connection), user, pwd, db);
15981606
Py_END_ALLOW_THREADS
@@ -1612,7 +1620,7 @@ _mysql_ConnectionObject_character_set_name(
16121620
PyObject *noargs)
16131621
{
16141622
const char *s;
1615-
check_connection(self);
1623+
check_connection(self, "character_set_name");
16161624
s = mysql_character_set_name(&(self->connection));
16171625
return PyString_FromString(s);
16181626
}
@@ -1630,7 +1638,7 @@ _mysql_ConnectionObject_set_character_set(
16301638
const char *s;
16311639
int err;
16321640
if (!PyArg_ParseTuple(args, "s", &s)) return NULL;
1633-
check_connection(self);
1641+
check_connection(self, "set_character_set");
16341642
Py_BEGIN_ALLOW_THREADS
16351643
err = mysql_set_character_set(&(self->connection), s);
16361644
Py_END_ALLOW_THREADS
@@ -1669,7 +1677,7 @@ _mysql_ConnectionObject_get_character_set_info(
16691677
PyObject *result;
16701678
MY_CHARSET_INFO cs;
16711679

1672-
check_connection(self);
1680+
check_connection(self, "get_character_set_info");
16731681
mysql_get_character_set_info(&(self->connection), &cs);
16741682
if (!(result = PyDict_New())) return NULL;
16751683
if (cs.csname)
@@ -1701,7 +1709,7 @@ _mysql_ConnectionObject_get_native_connection(
17011709
PyObject *noargs)
17021710
{
17031711
PyObject *result;
1704-
check_connection(self);
1712+
check_connection(self, "_get_native_connection");
17051713
result = PyCapsule_New(&(self->connection),
17061714
"_mysql.connection.native_connection", NULL);
17071715
return result;
@@ -1730,7 +1738,7 @@ _mysql_ConnectionObject_get_host_info(
17301738
_mysql_ConnectionObject *self,
17311739
PyObject *noargs)
17321740
{
1733-
check_connection(self);
1741+
check_connection(self, "get_host_info");
17341742
return PyString_FromString(mysql_get_host_info(&(self->connection)));
17351743
}
17361744

@@ -1744,7 +1752,7 @@ _mysql_ConnectionObject_get_proto_info(
17441752
_mysql_ConnectionObject *self,
17451753
PyObject *noargs)
17461754
{
1747-
check_connection(self);
1755+
check_connection(self, "get_proto_info");
17481756
return PyInt_FromLong((long)mysql_get_proto_info(&(self->connection)));
17491757
}
17501758

@@ -1758,7 +1766,7 @@ _mysql_ConnectionObject_get_server_info(
17581766
_mysql_ConnectionObject *self,
17591767
PyObject *noargs)
17601768
{
1761-
check_connection(self);
1769+
check_connection(self, "get_server_info");
17621770
return PyString_FromString(mysql_get_server_info(&(self->connection)));
17631771
}
17641772

@@ -1774,7 +1782,7 @@ _mysql_ConnectionObject_info(
17741782
PyObject *noargs)
17751783
{
17761784
const char *s;
1777-
check_connection(self);
1785+
check_connection(self, "info");
17781786
s = mysql_info(&(self->connection));
17791787
if (s) return PyString_FromString(s);
17801788
Py_INCREF(Py_None);
@@ -1808,7 +1816,7 @@ _mysql_ConnectionObject_insert_id(
18081816
PyObject *noargs)
18091817
{
18101818
my_ulonglong r;
1811-
check_connection(self);
1819+
check_connection(self, "insert_id");
18121820
Py_BEGIN_ALLOW_THREADS
18131821
r = mysql_insert_id(&(self->connection));
18141822
Py_END_ALLOW_THREADS
@@ -1827,7 +1835,7 @@ _mysql_ConnectionObject_kill(
18271835
unsigned long pid;
18281836
int r;
18291837
if (!PyArg_ParseTuple(args, "k:kill", &pid)) return NULL;
1830-
check_connection(self);
1838+
check_connection(self, "kill");
18311839
Py_BEGIN_ALLOW_THREADS
18321840
r = mysql_kill(&(self->connection), pid);
18331841
Py_END_ALLOW_THREADS
@@ -1847,7 +1855,7 @@ _mysql_ConnectionObject_field_count(
18471855
_mysql_ConnectionObject *self,
18481856
PyObject *noargs)
18491857
{
1850-
check_connection(self);
1858+
check_connection(self, "field_count");
18511859
return PyInt_FromLong((long)mysql_field_count(&(self->connection)));
18521860
}
18531861

@@ -1859,7 +1867,7 @@ _mysql_ResultObject_num_fields(
18591867
_mysql_ResultObject *self,
18601868
PyObject *noargs)
18611869
{
1862-
check_result_connection(self);
1870+
check_result_connection(self, "num_fields");
18631871
return PyInt_FromLong((long)mysql_num_fields(self->result));
18641872
}
18651873

@@ -1874,7 +1882,7 @@ _mysql_ResultObject_num_rows(
18741882
_mysql_ResultObject *self,
18751883
PyObject *noargs)
18761884
{
1877-
check_result_connection(self);
1885+
check_result_connection(self, "num_rows");
18781886
return PyLong_FromUnsignedLongLong(mysql_num_rows(self->result));
18791887
}
18801888

@@ -1904,7 +1912,7 @@ _mysql_ConnectionObject_ping(
19041912
{
19051913
int r, reconnect = -1;
19061914
if (!PyArg_ParseTuple(args, "|I", &reconnect)) return NULL;
1907-
check_connection(self);
1915+
check_connection(self, "ping");
19081916
if (reconnect != -1) {
19091917
my_bool recon = (my_bool)reconnect;
19101918
mysql_options(&self->connection, MYSQL_OPT_RECONNECT, &recon);
@@ -1931,7 +1939,7 @@ _mysql_ConnectionObject_query(
19311939
char *query;
19321940
int len, r;
19331941
if (!PyArg_ParseTuple(args, "s#:query", &query, &len)) return NULL;
1934-
check_connection(self);
1942+
check_connection(self, "query");
19351943

19361944
Py_BEGIN_ALLOW_THREADS
19371945
r = mysql_real_query(&(self->connection), query, len);
@@ -1955,7 +1963,7 @@ _mysql_ConnectionObject_send_query(
19551963
int len, r;
19561964
MYSQL *mysql = &(self->connection);
19571965
if (!PyArg_ParseTuple(args, "s#:query", &query, &len)) return NULL;
1958-
check_connection(self);
1966+
check_connection(self, "send_query");
19591967

19601968
Py_BEGIN_ALLOW_THREADS
19611969
r = mysql_send_query(mysql, query, len);
@@ -1976,7 +1984,7 @@ _mysql_ConnectionObject_read_query_result(
19761984
{
19771985
int r;
19781986
MYSQL *mysql = &(self->connection);
1979-
check_connection(self);
1987+
check_connection(self, "reqd_query_result");
19801988

19811989
Py_BEGIN_ALLOW_THREADS
19821990
r = (int)mysql_read_query_result(mysql);
@@ -2006,7 +2014,7 @@ _mysql_ConnectionObject_select_db(
20062014
char *db;
20072015
int r;
20082016
if (!PyArg_ParseTuple(args, "s:select_db", &db)) return NULL;
2009-
check_connection(self);
2017+
check_connection(self, "select_db");
20102018
Py_BEGIN_ALLOW_THREADS
20112019
r = mysql_select_db(&(self->connection), db);
20122020
Py_END_ALLOW_THREADS
@@ -2026,7 +2034,7 @@ _mysql_ConnectionObject_shutdown(
20262034
PyObject *noargs)
20272035
{
20282036
int r;
2029-
check_connection(self);
2037+
check_connection(self, "shutdown");
20302038
Py_BEGIN_ALLOW_THREADS
20312039
r = mysql_shutdown(&(self->connection), SHUTDOWN_DEFAULT);
20322040
Py_END_ALLOW_THREADS
@@ -2048,7 +2056,7 @@ _mysql_ConnectionObject_stat(
20482056
PyObject *noargs)
20492057
{
20502058
const char *s;
2051-
check_connection(self);
2059+
check_connection(self, "stat");
20522060
Py_BEGIN_ALLOW_THREADS
20532061
s = mysql_stat(&(self->connection));
20542062
Py_END_ALLOW_THREADS
@@ -2070,7 +2078,7 @@ _mysql_ConnectionObject_store_result(
20702078
PyObject *arglist=NULL, *kwarglist=NULL, *result=NULL;
20712079
_mysql_ResultObject *r=NULL;
20722080

2073-
check_connection(self);
2081+
check_connection(self, "store_result");
20742082
arglist = Py_BuildValue("(OiO)", self, 0, self->converter);
20752083
if (!arglist) goto error;
20762084
kwarglist = PyDict_New();
@@ -2108,7 +2116,7 @@ _mysql_ConnectionObject_thread_id(
21082116
PyObject *noargs)
21092117
{
21102118
unsigned long pid;
2111-
check_connection(self);
2119+
check_connection(self, "thread_id");
21122120
Py_BEGIN_ALLOW_THREADS
21132121
pid = mysql_thread_id(&(self->connection));
21142122
Py_END_ALLOW_THREADS
@@ -2129,7 +2137,7 @@ _mysql_ConnectionObject_use_result(
21292137
PyObject *arglist=NULL, *kwarglist=NULL, *result=NULL;
21302138
_mysql_ResultObject *r=NULL;
21312139

2132-
check_connection(self);
2140+
check_connection(self, "use_result");
21332141
arglist = Py_BuildValue("(OiO)", self, 1, self->converter);
21342142
if (!arglist) return NULL;
21352143
kwarglist = PyDict_New();
@@ -2187,7 +2195,7 @@ _mysql_ResultObject_data_seek(
21872195
{
21882196
unsigned int row;
21892197
if (!PyArg_ParseTuple(args, "i:data_seek", &row)) return NULL;
2190-
check_result_connection(self);
2198+
check_result_connection(self, "data_seek");
21912199
mysql_data_seek(self->result, row);
21922200
Py_INCREF(Py_None);
21932201
return Py_None;

0 commit comments

Comments
 (0)
Please sign in to comment.