Advertisement
gpshead

_Py_HashSeed basics (3.3)

Jan 13th, 2012
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 2.06 KB | None | 0 0
  1. $ hg diff
  2. diff --git a/Include/object.h b/Include/object.h
  3. --- a/Include/object.h
  4. +++ b/Include/object.h
  5. @@ -51,6 +51,14 @@
  6.  whose size is determined when the object is allocated.
  7.  */
  8.  
  9. +/* Used to make bytes and unicode hashes unpredictable. */
  10. +#define Py_HASH_SEED_SUPPORTED  /* TODO(gregory.p.smith): make this a configure
  11. +#ifdef Py_HASH_SEED_SUPPORTED
  12. +PyAPI_DATA(Py_hash_t) _Py_HashSeed;
  13. +#else
  14. +#define _Py_HashSeed 0
  15. +#endif
  16. +
  17.  /* Py_DEBUG implies Py_TRACE_REFS. */
  18.  #if defined(Py_DEBUG) && !defined(Py_TRACE_REFS)
  19.  #define Py_TRACE_REFS
  20. diff --git a/Objects/object.c b/Objects/object.c
  21. --- a/Objects/object.c
  22. +++ b/Objects/object.c
  23. @@ -8,6 +8,10 @@
  24.  extern "C" {
  25.  #endif
  26.  
  27. +#ifdef Py_HASH_SEED_SUPPORTED
  28. +Py_hash_t _Py_HashSeed;
  29. +#endif
  30. +
  31.  #ifdef Py_REF_DEBUG
  32.  Py_ssize_t _Py_RefTotal;
  33.  
  34. @@ -759,7 +763,7 @@
  35.      Py_uhash_t x;
  36.      Py_ssize_t i;
  37.  
  38. -    x = (Py_uhash_t) *p << 7;
  39. +    x = _Py_HashSeed ^ ((Py_uhash_t) *p << 7);
  40.      for (i = 0; i < len; i++)
  41.          x = (1000003U * x) ^ (Py_uhash_t) *p++;
  42.      x ^= (Py_uhash_t) len;
  43. diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
  44. --- a/Objects/unicodeobject.c
  45. +++ b/Objects/unicodeobject.c
  46. @@ -11162,7 +11162,7 @@
  47.  
  48.      /* The hash function as a macro, gets expanded three times below. */
  49.  #define HASH(P) \
  50. -    x = (Py_uhash_t)*P << 7; \
  51. +    x = _Py_HashSeed ^ ((Py_uhash_t)*P << 7); \
  52.      while (--len >= 0) \
  53.          x = (1000003*x) ^ (Py_uhash_t)*P++;
  54.  
  55. diff --git a/Python/pythonrun.c b/Python/pythonrun.c
  56. --- a/Python/pythonrun.c
  57. +++ b/Python/pythonrun.c
  58. @@ -203,6 +203,12 @@
  59.      initialized = 1;
  60.      _Py_Finalizing = NULL;
  61.  
  62. +#ifdef Py_HASH_SEED_SUPPORTED
  63. +    _Py_HashSeed = 0; /* TODO(gregory.p.smith): Init from a random source. */
  64. +    /* pid combined with highest resolution time possible at a minimum if
  65. +     * a /dev/urandom or equivalent API is not available.  */
  66. +#endif
  67. +
  68.  #if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE)
  69.      /* Set up the LC_CTYPE locale, so we can obtain
  70.         the locale's charset without having to switch
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement