Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion 3 Include/internal/pycore_fileutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ PyAPI_FUNC(int) _Py_EncodeUTF8Ex(

PyAPI_FUNC(wchar_t*) _Py_DecodeUTF8_surrogateescape(
const char *arg,
Py_ssize_t arglen);
Py_ssize_t arglen,
size_t *wlen);

PyAPI_FUNC(int) _Py_GetForceASCII(void);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Python initialization now fails if decoding ``pybuilddir.txt`` configuration
file fails at startup.
28 changes: 16 additions & 12 deletions 28 Modules/getpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -563,23 +563,27 @@ search_for_exec_prefix(const _PyCoreConfig *core_config,
}
else {
char buf[MAXPATHLEN+1];
wchar_t *rel_builddir_path;
n = fread(buf, 1, MAXPATHLEN, f);
buf[n] = '\0';
fclose(f);
rel_builddir_path = _Py_DecodeUTF8_surrogateescape(buf, n);
if (rel_builddir_path) {
wcsncpy(exec_prefix, calculate->argv0_path, MAXPATHLEN);
exec_prefix[MAXPATHLEN] = L'\0';
err = joinpath(exec_prefix, rel_builddir_path);
PyMem_RawFree(rel_builddir_path );
if (_Py_INIT_FAILED(err)) {
return err;
}

*found = -1;
return _Py_INIT_OK();
size_t dec_len;
wchar_t *pybuilddir;
pybuilddir = _Py_DecodeUTF8_surrogateescape(buf, n, &dec_len);
if (!pybuilddir) {
return DECODE_LOCALE_ERR("pybuilddir.txt", dec_len);
}

wcsncpy(exec_prefix, calculate->argv0_path, MAXPATHLEN);
exec_prefix[MAXPATHLEN] = L'\0';
err = joinpath(exec_prefix, pybuilddir);
PyMem_RawFree(pybuilddir );
if (_Py_INIT_FAILED(err)) {
return err;
}

*found = -1;
return _Py_INIT_OK();
}
}

Expand Down
13 changes: 11 additions & 2 deletions 13 Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5064,12 +5064,21 @@ _Py_DecodeUTF8Ex(const char *s, Py_ssize_t size, wchar_t **wstr, size_t *wlen,
return 0;
}


wchar_t*
_Py_DecodeUTF8_surrogateescape(const char *arg, Py_ssize_t arglen)
_Py_DecodeUTF8_surrogateescape(const char *arg, Py_ssize_t arglen,
size_t *wlen)
{
wchar_t *wstr;
int res = _Py_DecodeUTF8Ex(arg, arglen, &wstr, NULL, NULL, 1);
int res = _Py_DecodeUTF8Ex(arg, arglen,
&wstr, wlen,
NULL, _Py_ERROR_SURROGATEESCAPE);
if (res != 0) {
/* _Py_DecodeUTF8Ex() must support _Py_ERROR_SURROGATEESCAPE */
assert(res != -3);
if (wlen) {
*wlen = (size_t)res;
}
return NULL;
}
return wstr;
Expand Down
2 changes: 1 addition & 1 deletion 2 Python/pathconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ _Py_FindEnvConfigValue(FILE *env_file, const wchar_t *key,
continue;
}

wchar_t *tmpbuffer = _Py_DecodeUTF8_surrogateescape(buffer, n);
wchar_t *tmpbuffer = _Py_DecodeUTF8_surrogateescape(buffer, n, NULL);
if (tmpbuffer) {
wchar_t * state;
wchar_t * tok = WCSTOK(tmpbuffer, L" \t\r\n", &state);
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.