from test import support
import array
+import io
import marshal
import sys
import unittest
unicode_string = 'T'
self.assertRaises(TypeError, marshal.loads, unicode_string)
+ def test_bad_reader(self):
+ class BadReader(io.BytesIO):
+ def read(self, n=-1):
+ b = super().read(n)
+ if n is not None and n > 4:
+ b += b' ' * 10**6
+ return b
+ for value in (1.0, 1j, b'0123456789', '0123456789'):
+ self.assertRaises(ValueError, marshal.load,
+ BadReader(marshal.dumps(value)))
+
LARGE_SIZE = 2**31
pointer_size = 8 if sys.maxsize > 0xFFFFFFFF else 4
else {
read = PyBytes_GET_SIZE(data);
if (read > 0) {
- ptr = PyBytes_AS_STRING(data);
- memcpy(s, ptr, read);
+ if (read > n) {
+ PyErr_Format(PyExc_ValueError,
+ "read() returned too much data: "
+ "%zd bytes requested, %zd returned",
+ n, read);
+ read = -1;
+ }
+ else {
+ ptr = PyBytes_AS_STRING(data);
+ memcpy(s, ptr, read);
+ }
}
}
Py_DECREF(data);
double dx;
retval = NULL;
n = r_byte(p);
- if (n == EOF || r_string(buf, n, p) != n) {
+ if (n == EOF) {
PyErr_SetString(PyExc_EOFError,
"EOF read where object expected");
break;
}
+ if (r_string(buf, n, p) != n)
+ break;
buf[n] = '\0';
dx = PyOS_string_to_double(buf, NULL, NULL);
if (dx == -1.0 && PyErr_Occurred())
unsigned char buf[8];
double x;
if (r_string((char*)buf, 8, p) != 8) {
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
retval = NULL;
break;
}
Py_complex c;
retval = NULL;
n = r_byte(p);
- if (n == EOF || r_string(buf, n, p) != n) {
+ if (n == EOF) {
PyErr_SetString(PyExc_EOFError,
"EOF read where object expected");
break;
}
+ if (r_string(buf, n, p) != n)
+ break;
buf[n] = '\0';
c.real = PyOS_string_to_double(buf, NULL, NULL);
if (c.real == -1.0 && PyErr_Occurred())
break;
n = r_byte(p);
- if (n == EOF || r_string(buf, n, p) != n) {
+ if (n == EOF) {
PyErr_SetString(PyExc_EOFError,
"EOF read where object expected");
break;
}
+ if (r_string(buf, n, p) != n)
+ break;
buf[n] = '\0';
c.imag = PyOS_string_to_double(buf, NULL, NULL);
if (c.imag == -1.0 && PyErr_Occurred())
unsigned char buf[8];
Py_complex c;
if (r_string((char*)buf, 8, p) != 8) {
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
retval = NULL;
break;
}
break;
}
if (r_string((char*)buf, 8, p) != 8) {
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
retval = NULL;
break;
}
}
if (r_string(PyBytes_AS_STRING(v), n, p) != n) {
Py_DECREF(v);
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
retval = NULL;
break;
}
}
if (r_string(buffer, n, p) != n) {
PyMem_DEL(buffer);
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
retval = NULL;
break;
}