]> granicus.if.org Git - python/commitdiff
ensure no one tries to hash things before the random seed is found
authorBenjamin Peterson <benjamin@python.org>
Tue, 21 Feb 2012 16:08:50 +0000 (11:08 -0500)
committerBenjamin Peterson <benjamin@python.org>
Tue, 21 Feb 2012 16:08:50 +0000 (11:08 -0500)
Include/object.h
Objects/stringobject.c
Objects/unicodeobject.c
Python/random.c

index 0b7d41d003136c4a4af24e7ba32887ed296846a6..c0601c57ac90e3a715a34caed2b4678cd1faa7a0 100644 (file)
@@ -512,6 +512,10 @@ typedef struct {
 } _Py_HashSecret_t;
 PyAPI_DATA(_Py_HashSecret_t) _Py_HashSecret;
 
+#ifdef Py_DEBUG
+PyAPI_DATA(int) _Py_HashSecret_Initialized;
+#endif
+
 /* Helper for passing objects to printf and the like */
 #define PyObject_REPR(obj) PyString_AS_STRING(PyObject_Repr(obj))
 
index 9e2673d4fa20f9937ff16bd2b91ac87645deddd5..9d058662bcd3d2d500cc37c95c2fd6051c7cc10d 100644 (file)
@@ -1209,6 +1209,7 @@ string_hash(PyStringObject *a)
     register unsigned char *p;
     register long x;
 
+    assert(_Py_HashSecret_Initialized);
     if (a->ob_shash != -1)
         return a->ob_shash;
     len = Py_SIZE(a);
index 2f80e59b32fd84dbcaa741f108784d417470f0fd..1df38e8819c39996fb5a71ae696c826a67c830a1 100644 (file)
@@ -6692,6 +6692,7 @@ unicode_hash(PyUnicodeObject *self)
     register Py_UNICODE *p;
     register long x;
 
+    assert(_Py_HashSecret_Initialized);
     if (self->hash != -1)
         return self->hash;
     len = PyUnicode_GET_SIZE(self);
index 01cd83aa564cc8a2f91cc34bfb1ca1e1c65f1bf3..a2ae002261acdc5875b47d8193ed8bd7a628821f 100644 (file)
@@ -5,7 +5,11 @@
 #include <fcntl.h>
 #endif
 
-static int random_initialized = 0;
+#ifdef Py_DEBUG
+int _Py_HashSecret_Initialized = 0;
+#else
+static int _Py_HashSecret_Initialized = 0;
+#endif
 
 #ifdef MS_WINDOWS
 typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
@@ -246,11 +250,11 @@ _PyRandom_Init(void)
 {
     char *env;
     void *secret = &_Py_HashSecret;
-    Py_ssize_t secret_size = sizeof(_Py_HashSecret);
+    Py_ssize_t secret_size = sizeof(_Py_HashSecret_t);
 
-    if (random_initialized)
+    if (_Py_HashSecret_Initialized)
         return;
-    random_initialized = 1;
+    _Py_HashSecret_Initialized = 1;
 
     /*
       By default, hash randomization is disabled, and only