Skip to content

Commit 71c173c

Browse files
committed
fix other uses of strto*
1 parent 9c6d577 commit 71c173c

2 files changed

Lines changed: 15 additions & 12 deletions

File tree

singlestoredb/mysql/accel.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,10 @@ static PyObject *read_row_from_packet(
12671267

12681268
// If no converter was passed in, do the default processing.
12691269
else {
1270+
// Make sure not to overrun
1271+
char tmp = *end;
1272+
if (data_l) *end = '\0';
1273+
12701274
switch (py_state->type_codes[i]) {
12711275
case MYSQL_TYPE_NEWDECIMAL:
12721276
case MYSQL_TYPE_DECIMAL:
@@ -1283,19 +1287,13 @@ static PyObject *read_row_from_packet(
12831287
case MYSQL_TYPE_LONG:
12841288
case MYSQL_TYPE_LONGLONG:
12851289
case MYSQL_TYPE_INT24:
1286-
{
1287-
char *substr = calloc(out_l + 1, sizeof(char));
1288-
memcpy(substr, out, out_l);
12891290
if (py_state->flags[i] & MYSQL_FLAG_UNSIGNED) {
1290-
py_item = PyLong_FromUnsignedLongLong(strtoull(substr, NULL, 10));
1291+
py_item = PyLong_FromUnsignedLongLong(strtoull(out, NULL, 10));
12911292
} else {
1292-
py_item = PyLong_FromLongLong(strtoll(substr, NULL, 10));
1293+
py_item = PyLong_FromLongLong(strtoll(out, NULL, 10));
12931294
}
1294-
DESTROY(substr);
1295-
out += out_l;
12961295
if (!py_item) goto error;
12971296
break;
1298-
}
12991297
case MYSQL_TYPE_FLOAT:
13001298
case MYSQL_TYPE_DOUBLE:
13011299
py_item = PyFloat_FromDouble(strtod(out, &end));
@@ -1430,8 +1428,7 @@ static PyObject *read_row_from_packet(
14301428
goto error;
14311429
break;
14321430
}
1433-
end = &out[out_l];
1434-
year = strtoul(out, &end, 10);
1431+
year = strtoul(out, NULL, 10);
14351432
py_item = PyLong_FromLong(year);
14361433
if (!py_item) goto error;
14371434
break;
@@ -1472,6 +1469,7 @@ static PyObject *read_row_from_packet(
14721469
py_state->type_codes[i], NULL);
14731470
goto error;
14741471
}
1472+
if (data_l) *end = tmp;
14751473
}
14761474
}
14771475

singlestoredb/tests/test_basics.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -977,11 +977,16 @@ def _test_MySQLdb(self):
977977
for a, b in zip(s2_out, mydb_out):
978978
assert a == b, (a, b)
979979

980-
def test_long_string(self):
980+
def test_int_string(self):
981981
string = 'a' * 48
982-
self.cur.execute(f"SELECT 1 AS column_1, '{string}' AS column_2")
982+
self.cur.execute(f"SELECT 1, '{string}'")
983983
self.assertEqual((1, string), self.cur.fetchone())
984984

985+
def test_double_string(self):
986+
string = 'a' * 49
987+
self.cur.execute(f"SELECT 1.2 :> DOUBLE, '{string}'")
988+
self.assertEqual((1.2, string), self.cur.fetchone())
989+
985990

986991
if __name__ == '__main__':
987992
import nose2

0 commit comments

Comments
 (0)