SHARE
    TWEET
    gpshead

    _Py_HashSeed basics (3.3)

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

    AltStyle によって変換されたページ (->オリジナル) /