Skip to content

Commit

Permalink
bootloader: ensure pypath and pypath_w buffers are of same size
Browse files Browse the repository at this point in the history
Having pypath with greater size than pypath_w invites potential
trouble when path to executable is long enough that length of
pypath string exceeds PATH_MAX. And it makes little sense for the
two buffers to be of differently sized, anyway.
  • Loading branch information
rokm committed Apr 8, 2021
1 parent 35e13c7 commit c2f4c4e
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions bootloader/src/pyi_pythonlib.c
Expand Up @@ -392,10 +392,11 @@ pyi_pylib_start_python(ARCHIVE_STATUS *status)
* its contents nor free its memory.
*
* NOTE: Statics are zero-initialized. */
static char pypath[3 * PATH_MAX + 32];
#define MAX_PYPATH_SIZE (3 * PATH_MAX + 32)
static char pypath[MAX_PYPATH_SIZE];

/* Wide string forms of the above, for Python 3. */
static wchar_t pypath_w[PATH_MAX + 1];
static wchar_t pypath_w[MAX_PYPATH_SIZE];
static wchar_t pyhome_w[PATH_MAX + 1];
static wchar_t progname_w[PATH_MAX + 1];

Expand All @@ -420,26 +421,26 @@ pyi_pylib_start_python(ARCHIVE_STATUS *status)

/* Set sys.path */
/* sys.path = [mainpath/base_library.zip, mainpath/lib-dynload, mainpath] */
if (snprintf(pypath, sizeof pypath, "%s%c%s" "%c" "%s%c%s" "%c" "%s",
if (snprintf(pypath, MAX_PYPATH_SIZE, "%s%c%s" "%c" "%s%c%s" "%c" "%s",
status->mainpath, PYI_SEP, "base_library.zip",
PYI_PATHSEP,
status->mainpath, PYI_SEP, "lib-dynload",
PYI_PATHSEP,
status->mainpath)
>= sizeof pypath) {
>= MAX_PYPATH_SIZE) {
// This should never happen, since mainpath is < PATH_MAX and pypath is
// huge enough
FATALERROR("sys.path (based on %s) exceeds buffer[%d] space\n",
status->mainpath, sizeof pypath);
status->mainpath, MAX_PYPATH_SIZE);
return -1;
}

/*
* E must set sys.path to have base_library.zip before
* We must set sys.path to have base_library.zip before
* calling Py_Initialize as it needs `encodings` and other modules.
*/
/* Decode using current locale */
if (!pyi_locale_char2wchar(pypath_w, pypath, PATH_MAX)) {
if (!pyi_locale_char2wchar(pypath_w, pypath, MAX_PYPATH_SIZE)) {
FATALERROR("Failed to convert pypath to wchar_t\n");
return -1;
}
Expand Down

0 comments on commit c2f4c4e

Please sign in to comment.