Skip to content

Commit 528b54b

Browse files
committed
Fix test failure in test_cmd_line by initializing the hash secret at the earliest point.
2 parents 61f996b + 86838b0 commit 528b54b

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

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
@@ -193,6 +193,7 @@ PyAPI_FUNC(void) _PyImportHooks_Init(void);
193193
PyAPI_FUNC(int) _PyFrame_Init(void);
194194
PyAPI_FUNC(void) _PyFloat_Init(void);
195195
PyAPI_FUNC(int) PyByteArray_Init(void);
196+
PyAPI_FUNC(void) _PyRandom_Init(void);
196197
#endif
197198

198199
/* Various internal finalizers */

Modules/main.c

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

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

344370
while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
345371
if (c == 'c') {
@@ -400,7 +426,7 @@ Py_Main(int argc, wchar_t **argv)
400426
break;
401427

402428
case 'E':
403-
Py_IgnoreEnvironmentFlag++;
429+
/* Already handled above */
404430
break;
405431

406432
case 't':
@@ -442,7 +468,7 @@ Py_Main(int argc, wchar_t **argv)
442468
break;
443469

444470
case 'R':
445-
Py_HashRandomizationFlag++;
471+
/* Already handled above */
446472
break;
447473

448474
/* 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
@@ -73,7 +73,6 @@ extern int _PyLong_Init(void);
7373
extern void PyLong_Fini(void);
7474
extern int _PyFaulthandler_Init(void);
7575
extern void _PyFaulthandler_Fini(void);
76-
extern void _PyRandom_Init(void);
7776

7877
#ifdef WITH_THREAD
7978
extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);

0 commit comments

Comments
 (0)