Skip to content

Commit f07448b

Browse files
bpo-41894: Fix UnicodeDecodeError while loading native module (GH-22466)
When running in a non-UTF-8 locale, if an error occurs while importing a native Python module (say because a dependent share library is missing), the error message string returned may contain non-ASCII code points causing a UnicodeDecodeError. PyUnicode_DecodeFSDefault is used for buffers which may contain filesystem paths. For consistency with os.strerror(), PyUnicode_DecodeLocale is used for buffers which contain system error messages. While the shortname parameter is always encoded in ASCII according to PEP 489, it is left decoded using PyUnicode_FromString to minimize the changes and since it should not affect the decoding (albeit _potentially_ slower). In dynload_hpux, since the error buffer contains a message generated from a static ASCII string and the module filesystem path, PyUnicode_DecodeFSDefault is used instead of PyUnicode_DecodeLocale as is used elsewhere. * bpo-41894: Fix bugs in dynload error msg handling For both dynload_aix and dynload_hpux, properly handle the possibility that decoding strings may return NULL and when such an error happens, properly decrement any previously decoded strings and return early. In addition, in dynload_aix, ensure that we pass the decoded string *object* pathname_ob to PyErr_SetImportError instead of the original pathname buffer. Co-authored-by: Serhiy Storchaka <[email protected]> (cherry picked from commit 2d2af32) Co-authored-by: Kevin Adler <[email protected]>
1 parent 69f040c commit f07448b

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
When loading a native module and a load failure occurs, prevent a possible
2+
UnicodeDecodeError when not running in a UTF-8 locale by decoding the load
3+
error message using the current locale's encoding.

Python/dynload_aix.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,16 @@ aix_loaderror(const char *pathname)
144144
ERRBUF_APPEND(message[i]);
145145
ERRBUF_APPEND("\n");
146146
}
147-
errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */
148-
pathname_ob = PyUnicode_FromString(pathname);
149-
errbuf_ob = PyUnicode_FromString(errbuf);
150-
PyErr_SetImportError(errbuf_ob, NULL, pathname);
147+
/* Subtract 1 from the length to trim off trailing newline */
148+
errbuf_ob = PyUnicode_DecodeLocaleAndSize(errbuf, strlen(errbuf)-1, "surrogateescape");
149+
if (errbuf_ob == NULL)
150+
return;
151+
pathname_ob = PyUnicode_DecodeFSDefault(pathname);
152+
if (pathname_ob == NULL) {
153+
Py_DECREF(errbuf_ob);
154+
return;
155+
}
156+
PyErr_SetImportError(errbuf_ob, NULL, pathname_ob);
151157
Py_DECREF(pathname_ob);
152158
Py_DECREF(errbuf_ob);
153159
return;

Python/dynload_hpux.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,20 @@ dl_funcptr _PyImport_FindSharedFuncptr(const char *prefix,
3636
char buf[256];
3737
PyOS_snprintf(buf, sizeof(buf), "Failed to load %.200s",
3838
pathname);
39-
PyObject *buf_ob = PyUnicode_FromString(buf);
39+
PyObject *buf_ob = PyUnicode_DecodeFSDefault(buf);
40+
if (buf_ob == NULL)
41+
return NULL;
4042
PyObject *shortname_ob = PyUnicode_FromString(shortname);
41-
PyObject *pathname_ob = PyUnicode_FromString(pathname);
43+
if (shortname_ob == NULL) {
44+
Py_DECREF(buf_ob);
45+
return NULL;
46+
}
47+
PyObject *pathname_ob = PyUnicode_DecodeFSDefault(pathname);
48+
if (pathname_ob == NULL) {
49+
Py_DECREF(buf_ob);
50+
Py_DECREF(shortname_ob);
51+
return NULL;
52+
}
4253
PyErr_SetImportError(buf_ob, shortname_ob, pathname_ob);
4354
Py_DECREF(buf_ob);
4455
Py_DECREF(shortname_ob);

Python/dynload_shlib.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ _PyImport_FindSharedFuncptr(const char *prefix,
106106
const char *error = dlerror();
107107
if (error == NULL)
108108
error = "unknown dlopen() error";
109-
error_ob = PyUnicode_FromString(error);
109+
error_ob = PyUnicode_DecodeLocale(error, "surrogateescape");
110110
if (error_ob == NULL)
111111
return NULL;
112112
mod_name = PyUnicode_FromString(shortname);
113113
if (mod_name == NULL) {
114114
Py_DECREF(error_ob);
115115
return NULL;
116116
}
117-
path = PyUnicode_FromString(pathname);
117+
path = PyUnicode_DecodeFSDefault(pathname);
118118
if (path == NULL) {
119119
Py_DECREF(error_ob);
120120
Py_DECREF(mod_name);

0 commit comments

Comments
 (0)