Skip to content

Commit 8c56da0

Browse files
authored
Merge pull request #7 from morokosi/fix-read-integer
fix corrupted integer value followed by long string
2 parents 7128b67 + 70bacef commit 8c56da0

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

singlestoredb/mysql/accel.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ static PyObject *read_row_from_packet(
12131213
PyObject *py_result = NULL;
12141214
PyObject *py_item = NULL;
12151215
PyObject *py_str = NULL;
1216-
char *end = NULL;
1216+
char end = '\0';
12171217

12181218
int sign = 1;
12191219
int year = 0;
@@ -1240,7 +1240,7 @@ static PyObject *read_row_from_packet(
12401240
for (unsigned long i = 0; i < py_state->n_cols; i++) {
12411241

12421242
read_length_coded_string(&data, &data_l, &out, &out_l, &is_null);
1243-
end = &out[out_l];
1243+
end = out[out_l];
12441244

12451245
orig_out = out;
12461246
orig_out_l = out_l;
@@ -1283,17 +1283,21 @@ static PyObject *read_row_from_packet(
12831283
case MYSQL_TYPE_LONG:
12841284
case MYSQL_TYPE_LONGLONG:
12851285
case MYSQL_TYPE_INT24:
1286+
if (data_l) out[out_l] = '\0';
12861287
if (py_state->flags[i] & MYSQL_FLAG_UNSIGNED) {
1287-
py_item = PyLong_FromUnsignedLongLong(strtoull(out, &end, 10));
1288+
py_item = PyLong_FromUnsignedLongLong(strtoull(out, NULL, 10));
12881289
} else {
1289-
py_item = PyLong_FromLongLong(strtoll(out, &end, 10));
1290+
py_item = PyLong_FromLongLong(strtoll(out, NULL, 10));
12901291
}
1292+
if (data_l) out[out_l] = end;
12911293
if (!py_item) goto error;
12921294
break;
12931295

12941296
case MYSQL_TYPE_FLOAT:
12951297
case MYSQL_TYPE_DOUBLE:
1296-
py_item = PyFloat_FromDouble(strtod(out, &end));
1298+
if (data_l) out[out_l] = '\0';
1299+
py_item = PyFloat_FromDouble(strtod(out, NULL));
1300+
if (data_l) out[out_l] = end;
12971301
if (!py_item) goto error;
12981302
break;
12991303

@@ -1425,9 +1429,10 @@ static PyObject *read_row_from_packet(
14251429
goto error;
14261430
break;
14271431
}
1428-
end = &out[out_l];
1429-
year = strtoul(out, &end, 10);
1432+
if (data_l) out[out_l] = '\0';
1433+
year = strtoul(out, NULL, 10);
14301434
py_item = PyLong_FromLong(year);
1435+
if (data_l) out[out_l] = end;
14311436
if (!py_item) goto error;
14321437
break;
14331438

singlestoredb/tests/test_basics.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +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_int_string(self):
981+
string = 'a' * 48
982+
self.cur.execute(f"SELECT 1, '{string}'")
983+
self.assertEqual((1, string), self.cur.fetchone())
984+
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+
980990

981991
if __name__ == '__main__':
982992
import nose2

0 commit comments

Comments
 (0)