New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PyString_FromFormatV() fails to build empty strings #77998
Comments
Making PyString_FromFormatV() build an empty string on 2.7 will fail and raise the following error: It does not matter if format is empty or not (e.g., both PyString_FromFormat("") and PyString_FromFormat("%s", "")). The exception is raised from _PyString_Resize() (called at the end of PyString_FromFormatV()), because the string given to that method has a ref count different than 1. This is expected for empty strings, because PyString_FromStringAndSize() returns a reference to <nullstring> when <size> is 0. A possible fix would be to prevent the call to _PyString_Resize() from PyString_FromFormatV() if <string> is equal to <nullstring>, or if there is no need to resize the string. Python 3 versions before 3.6 might be impacted as well through PyBytes_FromFormatV() (cannot check). The following code can be used to trigger the bug: static int dobug(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
PyObject *str = PyString_FromFormatV(fmt, args);
va_end(args);
if(str == NULL) {
fprintf(stderr, "Error: PyString_FromFormatV(%s) returned NULL\n", fmt);
return -1;
}
Py_DECREF(str);
return 0;
}
static PyObject* bug(PyObject *self) {
fprintf(stderr, "dobug(\"\") => %d\n", dobug(""));
fprintf(stderr, "dobug(\"%%s\", \"\") => %d\n", dobug("%s", ""));
if(PyErr_Occurred()) return NULL;
Py_RETURN_NONE;
} |
For the record, it does not fail on 3.x because _PyBytes_Resize() checks if the "string" needs to be resized (and returns if not) before checking its ref count. Maybe something similar should be done in _PyString_Resize() for 2.x. |
BTW, problem does appear in 3.4 as it's only been fixed in 3.5+ as a fix for issue bpo-25270 |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: