summaryrefslogtreecommitdiff
path: root/Modules/main.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2017-12-15 00:51:22 +0100
committerGitHub <noreply@github.com>2017-12-15 00:51:22 +0100
commit358e5e17a51ba00742bfaee4557a94c3c4179c22 (patch)
tree0918046bb6a75f31957b4d0aa24ca73df93d5bbf /Modules/main.c
parent96a5e50a5de3683b2afd6d680c7ecc4b525986f6 (diff)
downloadcpython-git-358e5e17a51ba00742bfaee4557a94c3c4179c22.tar.gz
bpo-32329: Fix -R option for hash randomization (#4873)
bpo-32329, bpo-32030: * The -R option now turns on hash randomization when the PYTHONHASHSEED environment variable is set to 0 Previously, the option was ignored. * sys.flags.hash_randomization is now properly set to 0 when hash randomization is turned off by PYTHONHASHSEED=0. * _PyCoreConfig_ReadEnv() now reads the PYTHONHASHSEED environment variable. _Py_HashRandomization_Init() now only apply the configuration, it doesn't read PYTHONHASHSEED anymore.
Diffstat (limited to 'Modules/main.c')
-rw-r--r--Modules/main.c35
1 files changed, 28 insertions, 7 deletions
diff --git a/Modules/main.c b/Modules/main.c
index 6db7e5f708..e1a2f98dc6 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -726,7 +726,7 @@ pymain_parse_cmdline_impl(_PyMain *pymain)
break;
case 'R':
- /* Ignored */
+ pymain->core_config.use_hash_seed = 0;
break;
/* This space reserved for other options */
@@ -1293,6 +1293,10 @@ pymain_set_global_config(_PyMain *pymain)
Py_IgnoreEnvironmentFlag = pymain->core_config.ignore_environment;
Py_UTF8Mode = pymain->core_config.utf8_mode;
+
+ /* Random or non-zero hash seed */
+ Py_HashRandomizationFlag = (pymain->core_config.use_hash_seed == 0 ||
+ pymain->core_config.hash_seed != 0);
}
@@ -1694,6 +1698,24 @@ config_init_home(_PyCoreConfig *config)
}
+static _PyInitError
+config_init_hash_seed(_PyCoreConfig *config)
+{
+ if (config->use_hash_seed < 0) {
+ const char *seed_text = pymain_get_env_var("PYTHONHASHSEED");
+ int use_hash_seed;
+ unsigned long hash_seed;
+ if (_Py_ReadHashSeed(seed_text, &use_hash_seed, &hash_seed) < 0) {
+ return _Py_INIT_USER_ERR("PYTHONHASHSEED must be \"random\" "
+ "or an integer in range [0; 4294967295]");
+ }
+ config->use_hash_seed = use_hash_seed;
+ config->hash_seed = hash_seed;
+ }
+ return _Py_INIT_OK();
+}
+
+
_PyInitError
_PyCoreConfig_ReadEnv(_PyCoreConfig *config)
{
@@ -1712,6 +1734,11 @@ _PyCoreConfig_ReadEnv(_PyCoreConfig *config)
return err;
}
+ err = config_init_hash_seed(config);
+ if (_Py_INIT_FAILED(err)) {
+ return err;
+ }
+
return _Py_INIT_OK();
}
@@ -1777,12 +1804,6 @@ pymain_parse_envvars(_PyMain *pymain)
/* Get environment variables */
pymain_set_flags_from_env(pymain);
- /* The variable is only tested for existence here;
- _Py_HashRandomization_Init will check its value further. */
- if (pymain_get_env_var("PYTHONHASHSEED")) {
- Py_HashRandomizationFlag = 1;
- }
-
if (pymain_warnings_envvar(pymain) < 0) {
return -1;
}