Skip to content

bpo-15954: Check return code of wcsxfrm(). #508

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions Modules/_localemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,12 @@ PyLocale_strxfrm(PyObject* self, PyObject* args)
PyErr_NoMemory();
goto exit;
}
errno = 0;
n2 = wcsxfrm(buf, s, n1);
if (errno) {
PyErr_SetFromErrno(PyExc_OSError);
goto exit;
}
if (n2 >= (size_t)n1) {
/* more space needed */
wchar_t * new_buf = PyMem_Realloc(buf, (n2+1)*sizeof(wchar_t));
Expand All @@ -269,14 +274,17 @@ PyLocale_strxfrm(PyObject* self, PyObject* args)
goto exit;
}
buf = new_buf;
errno = 0;
n2 = wcsxfrm(buf, s, n2+1);
if (errno) {
PyErr_SetFromErrno(PyExc_OSError);
goto exit;
}
}
result = PyUnicode_FromWideChar(buf, n2);
exit:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you modify this function, can you please remove useless "if (buf)" and "if (s)" below? PyMem_Free(NULL) is well defined: it does nothing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except adding an overhead for function call.

But I don't think it matters in comparison with the time of calling other functions.

if (buf)
PyMem_Free(buf);
if (s)
PyMem_Free(s);
PyMem_Free(buf);
PyMem_Free(s);
return result;
}
#endif
Expand Down