Skip to content

Commit 86838b0

Browse files
committed
Fix test failure in test_cmd_line by initializing the hash secret at the earliest point.
1 parent dbd3f61 commit 86838b0

5 files changed

Lines changed: 41 additions & 4 deletions

File tree

Include/pygetopt.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ extern "C" {
99
PyAPI_DATA(int) _PyOS_opterr;
1010
PyAPI_DATA(int) _PyOS_optind;
1111
PyAPI_DATA(wchar_t *) _PyOS_optarg;
12+
13+
PyAPI_FUNC(void) _PyOS_ResetGetOpt(void);
1214
#endif
1315

1416
PyAPI_FUNC(int) _PyOS_GetOpt(int argc, wchar_t **argv, wchar_t *optstring);

Include/pythonrun.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ PyAPI_FUNC(void) _PyImportHooks_Init(void);
196196
PyAPI_FUNC(int) _PyFrame_Init(void);
197197
PyAPI_FUNC(void) _PyFloat_Init(void);
198198
PyAPI_FUNC(int) PyByteArray_Init(void);
199+
PyAPI_FUNC(void) _PyRandom_Init(void);
199200
#endif
200201

201202
/* Various internal finalizers */

Modules/main.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,33 @@ Py_Main(int argc, wchar_t **argv)
337337
orig_argc = argc; /* For Py_GetArgcArgv() */
338338
orig_argv = argv;
339339

340+
/* Hash randomization needed early for all string operations
341+
(including -W and -X options). */
342+
while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
343+
if (c == 'm' || c == 'c') {
344+
/* -c / -m is the last option: following arguments are
345+
not interpreter options. */
346+
break;
347+
}
348+
switch (c) {
349+
case 'E':
350+
Py_IgnoreEnvironmentFlag++;
351+
break;
352+
case 'R':
353+
Py_HashRandomizationFlag++;
354+
break;
355+
}
356+
}
357+
/* The variable is only tested for existence here; _PyRandom_Init will
358+
check its value further. */
359+
if (!Py_HashRandomizationFlag &&
360+
(p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
361+
Py_HashRandomizationFlag = 1;
362+
363+
_PyRandom_Init();
364+
340365
PySys_ResetWarnOptions();
366+
_PyOS_ResetGetOpt();
341367

342368
while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
343369
if (c == 'c') {
@@ -398,7 +424,7 @@ Py_Main(int argc, wchar_t **argv)
398424
break;
399425

400426
case 'E':
401-
Py_IgnoreEnvironmentFlag++;
427+
/* Already handled above */
402428
break;
403429

404430
case 't':
@@ -440,7 +466,7 @@ Py_Main(int argc, wchar_t **argv)
440466
break;
441467

442468
case 'R':
443-
Py_HashRandomizationFlag++;
469+
/* Already handled above */
444470
break;
445471

446472
/* This space reserved for other options */

Python/getopt.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,18 @@ int _PyOS_opterr = 1; /* generate error messages */
4141
int _PyOS_optind = 1; /* index into argv array */
4242
wchar_t *_PyOS_optarg = NULL; /* optional argument */
4343

44+
static wchar_t *opt_ptr = L"";
45+
46+
void _PyOS_ResetGetOpt(void)
47+
{
48+
_PyOS_opterr = 1;
49+
_PyOS_optind = 1;
50+
_PyOS_optarg = NULL;
51+
opt_ptr = L"";
52+
}
53+
4454
int _PyOS_GetOpt(int argc, wchar_t **argv, wchar_t *optstring)
4555
{
46-
static wchar_t *opt_ptr = L"";
4756
wchar_t *ptr;
4857
wchar_t option;
4958

Python/pythonrun.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ extern void _PyUnicode_Init(void);
7070
extern void _PyUnicode_Fini(void);
7171
extern int _PyLong_Init(void);
7272
extern void PyLong_Fini(void);
73-
extern void _PyRandom_Init(void);
7473

7574
#ifdef WITH_THREAD
7675
extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);

0 commit comments

Comments
 (0)