Fix [ 616716 ] Bug in PyErr_SetExcFromWindows

Ensure that even if FormatMessage fails we (a) don't crash, and (b) provide something useful.

Bugfix candidate.
This commit is contained in:
Mark Hammond 2002-10-04 00:13:02 +00:00
parent da7efaa681
commit 3d61a06aa2
1 changed files with 28 additions and 9 deletions

View File

@ -269,6 +269,7 @@ PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
#endif #endif
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
char *s_buf = NULL; char *s_buf = NULL;
char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
#endif #endif
#ifdef EINTR #ifdef EINTR
if (i == EINTR && PyErr_CheckSignals()) if (i == EINTR && PyErr_CheckSignals())
@ -306,10 +307,18 @@ PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
(LPTSTR) &s_buf, (LPTSTR) &s_buf,
0, /* size not used */ 0, /* size not used */
NULL); /* no args */ NULL); /* no args */
s = s_buf; if (len==0) {
/* remove trailing cr/lf and dots */ /* Only ever seen this in out-of-mem
while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.')) situations */
s[--len] = '\0'; sprintf(s_small_buf, "Windows Error 0x%X", i);
s = s_small_buf;
s_buf = NULL;
} else {
s = s_buf;
/* remove trailing cr/lf and dots */
while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
s[--len] = '\0';
}
} }
} }
#endif /* Unix/Windows */ #endif /* Unix/Windows */
@ -366,6 +375,8 @@ PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
{ {
int len; int len;
char *s; char *s;
char *s_buf = NULL; /* Free via LocalFree */
char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
PyObject *v; PyObject *v;
DWORD err = (DWORD)ierr; DWORD err = (DWORD)ierr;
if (err==0) err = GetLastError(); if (err==0) err = GetLastError();
@ -378,12 +389,20 @@ PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
err, err,
MAKELANGID(LANG_NEUTRAL, MAKELANGID(LANG_NEUTRAL,
SUBLANG_DEFAULT), /* Default language */ SUBLANG_DEFAULT), /* Default language */
(LPTSTR) &s, (LPTSTR) &s_buf,
0, /* size not used */ 0, /* size not used */
NULL); /* no args */ NULL); /* no args */
/* remove trailing cr/lf and dots */ if (len==0) {
while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.')) /* Only seen this in out of mem situations */
s[--len] = '\0'; sprintf(s_small_buf, "Windows Error 0x%X", err);
s = s_small_buf;
s_buf = NULL;
} else {
s = s_buf;
/* remove trailing cr/lf and dots */
while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
s[--len] = '\0';
}
if (filenameObject != NULL) if (filenameObject != NULL)
v = Py_BuildValue("(isO)", err, s, filenameObject); v = Py_BuildValue("(isO)", err, s, filenameObject);
else else
@ -392,7 +411,7 @@ PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
PyErr_SetObject(exc, v); PyErr_SetObject(exc, v);
Py_DECREF(v); Py_DECREF(v);
} }
LocalFree(s); LocalFree(s_buf);
return NULL; return NULL;
} }