On Thu, 5 Jan 2006, tsuraan wrote:
In the catch-all else statement of j2p, you have the line
Py_UNICODE pchars[len];
This causes bus errors for large values of len under freebsd (I didn't
think c++ allowed this at all, but I guess I was wrong :). What works
better is
Py_UNICODE *pchars = new Py_UNICODE[len];
with a free before the return at the bottom of the function. The diff
between my PyLucene.i and the old one is attached.
Yes, indeed, this code is intrinsically unsafe as it may blow the stack.
Since I wrote this I discovered I could pass NULL to PyUnicode_FromUnicode()
and fill in the Py_UNICODE chars after the call. This is even better as it
saves a malloc/memcpy/free cycle.
The patch is included below.
Andi..
Index: PyLucene.i
===================================================================
--- PyLucene.i (revision 222)
+++ PyLucene.i (working copy)
@@ -528,13 +528,18 @@
else
{
int len = js->length();
- Py_UNICODE pchars[len];
+ PyObject *string = PyUnicode_FromUnicode(NULL, len);
+
+ if (!string)
+ return NULL;
+
jchar *jchars = JvGetStringChars(js);
+ Py_UNICODE *pchars = PyUnicode_AS_UNICODE(string);
for (int i = 0; i < len; i++)
pchars[i] = jchars[i];
- return PyUnicode_FromUnicode((const Py_UNICODE *) pchars, len);
+ return string;
}
}
_______________________________________________
pylucene-dev mailing list
[email protected]
http://lists.osafoundation.org/mailman/listinfo/pylucene-dev