Guard against overflows when using the lo32 api

If psycopg supports lo64 but the server doesn't the user may pass values
that would overflow the api range, resulting in:

    lo.seek((2<<30))
    *** OperationalError: ERROR:  invalid seek offset: -2147483648

Also improved the error messages and guard against INT_MIN for negative
seek offsets.
This commit is contained in:
Daniele Varrazzo 2014-09-11 01:06:57 +01:00
parent 79df47a146
commit 2f862972c9

View File

@ -175,10 +175,21 @@ psyco_lobj_seek(lobjectObject *self, PyObject *args)
EXC_IF_LOBJ_LEVEL0(self); EXC_IF_LOBJ_LEVEL0(self);
EXC_IF_LOBJ_UNMARKED(self); EXC_IF_LOBJ_UNMARKED(self);
#ifndef HAVE_LO64 #ifdef HAVE_LO64
if (offset > INT_MAX) { if ((offset < INT_MIN || offset > INT_MAX)
psyco_set_error(InterfaceError, NULL, && self->conn->server_version < 90300) {
"offset out of range"); PyErr_Format(NotSupportedError,
"offset out of range (%ld): server version %d "
"does not support the lobject 64 API",
offset, self->conn->server_version);
return NULL;
}
#else
if (offset < INT_MIN || offset > INT_MAX) {
PyErr_Format(InterfaceError,
"offset out of range (%ld): this psycopg version was not built "
"with lobject 64 API support",
offset);
return NULL; return NULL;
} }
#endif #endif
@ -272,10 +283,20 @@ psyco_lobj_truncate(lobjectObject *self, PyObject *args)
EXC_IF_LOBJ_LEVEL0(self); EXC_IF_LOBJ_LEVEL0(self);
EXC_IF_LOBJ_UNMARKED(self); EXC_IF_LOBJ_UNMARKED(self);
#ifndef HAVE_LO64 #ifdef HAVE_LO64
if (len > INT_MAX && self->conn->server_version < 90300) {
PyErr_Format(NotSupportedError,
"len out of range (%ld): server version %d "
"does not support the lobject 64 API",
len, self->conn->server_version);
return NULL;
}
#else
if (len > INT_MAX) { if (len > INT_MAX) {
psyco_set_error(InterfaceError, NULL, PyErr_Format(InterfaceError,
"len out of range"); "len out of range (%ld): this psycopg version was not built "
"with lobject 64 API support",
len);
return NULL; return NULL;
} }
#endif #endif