_compile(): raise an exception if downcasting to SRE_CODE

loses information:

    OverflowError: regular expression code size limit exceeded

Otherwise the compiled code is gibberish, possibly leading at
least to wrong results or (as reported on c.l.py) internal
sre errors at match time.

I'm not sure how to test this.  SRE_CODE is a 2-byte type on
my box, and it's easy to create a regexp that causes the new
exception to trigger here.  But it may be a 4-byte type on
other boxes, and creating a regexp large enough to trigger
problems there would be pretty crazy.

Bugfix candidate.
This commit is contained in:
Tim Peters 2006-01-21 02:47:53 +00:00
parent 887c080a80
commit 3d56350910
1 changed files with 40 additions and 37 deletions

View File

@ -1546,7 +1546,6 @@ SRE_SEARCH(SRE_STATE* state, SRE_CODE* pattern)
}
break;
}
}
ptr++;
}
@ -1652,10 +1651,14 @@ _compile(PyObject* self_, PyObject* args)
for (i = 0; i < n; i++) {
PyObject *o = PyList_GET_ITEM(code, i);
if (PyInt_Check(o))
self->code[i] = (SRE_CODE) PyInt_AsLong(o);
else
self->code[i] = (SRE_CODE) PyLong_AsUnsignedLong(o);
unsigned long value = PyInt_Check(o) ? (unsigned long)PyInt_AsLong(o)
: PyLong_AsUnsignedLong(o);
self->code[i] = (SRE_CODE) value;
if ((unsigned long) self->code[i] != value) {
PyErr_SetString(PyExc_OverflowError,
"regular expression code size limit exceeded");
break;
}
}
if (PyErr_Occurred()) {