Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in hashlib #50530

Closed
eloff mannequin opened this issue Jun 13, 2009 · 7 comments
Closed

Bug in hashlib #50530

eloff mannequin opened this issue Jun 13, 2009 · 7 comments
Assignees
Labels
type-bug An unexpected behavior, bug, or error

Comments

@eloff
Copy link
Mannequin

eloff mannequin commented Jun 13, 2009

BPO 6281
Nosy @gpshead, @amauryfa, @pitrou
Files
  • hashlib.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/gpshead'
    closed_at = <Date 2009-08-16.22:01:02.403>
    created_at = <Date 2009-06-13.22:45:29.271>
    labels = ['type-bug']
    title = 'Bug in hashlib'
    updated_at = <Date 2009-09-14.18:25:54.190>
    user = 'https://bugs.python.org/Eloff'

    bugs.python.org fields:

    activity = <Date 2009-09-14.18:25:54.190>
    actor = 'Eloff'
    assignee = 'gregory.p.smith'
    closed = True
    closed_date = <Date 2009-08-16.22:01:02.403>
    closer = 'gregory.p.smith'
    components = []
    creation = <Date 2009-06-13.22:45:29.271>
    creator = 'Eloff'
    dependencies = []
    files = ['14297']
    hgrepos = []
    issue_num = 6281
    keywords = []
    message_count = 7.0
    messages = ['89338', '89344', '89355', '89374', '89375', '91646', '92626']
    nosy_count = 4.0
    nosy_names = ['gregory.p.smith', 'amaury.forgeotdarc', 'pitrou', 'Eloff']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'patch review'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue6281'
    versions = ['Python 2.6', 'Python 3.1', 'Python 2.7', 'Python 3.2']

    @eloff
    Copy link
    Mannequin Author

    eloff mannequin commented Jun 13, 2009

    The try statement at the end of hashlib.py is some of the worst python
    code I've had the mispleasure of reading for a long time.

    Secondly, it seems flawed in function as well as form.
    __get_builtin_constructor can throw an ImportError, which seems
    erroneously caught in the except (why on earth does the except span more
    than the "import _hashlib"? That's bad style for precisely this reason.)
    This will cause an error in the import of hashlib when there are
    possibly many working hash functions already imported. Changing the:

    try:
    exec funcName + ' = __get_builtin_constructor(funcName)'
    except ValueError:
    pass

    to catch ImportError as well solves the problem for me.

    @eloff eloff mannequin added the type-bug An unexpected behavior, bug, or error label Jun 13, 2009
    @pitrou
    Copy link
    Member

    pitrou commented Jun 14, 2009

    Can you propose an actual test case and a patch?

    @eloff
    Copy link
    Mannequin Author

    eloff mannequin commented Jun 14, 2009

    If you're missing any hash algorithm from openssl (say sha224) and the
    corresponding _sha224 module, hashlib will fail to import and no hash
    algorithms will be available.

    Additionally, if no (or only some) openssl algorithms are available, the
    error branch expects every hash algorithm to be present in it's own
    module, if even one is missing, hashlib will fail to import.

    Producing a test case for these without mock objects is difficult at
    best. I leave that to the python team, if they desire to do so.

    Here is a rewritten hashlib.py, without that ugly mess of code and
    without the above two bugs. It passes all tests.

    @amauryfa
    Copy link
    Member

    In addition, I would replace
    exec funcName + ' = __get_hash(funcName)'
    with
    globals()[funcName] = __get_hash(funcName)

    @eloff
    Copy link
    Mannequin Author

    eloff mannequin commented Jun 14, 2009

    Yes, I prefer:

    globals()[funcName] = __get_hash(funcName)

    The exec was used in the original code, I'm not aware of the style of
    the python stdlib programmers, so I tried to be as true to what I found
    as possible.

    I just wanted to blunt my words in the original post, I don't want
    Gregory Smith to view it as a personal attack, I was just frustrated in
    having to debug the hashlib code. I'm sure even Gregory would admit it's
    not code he's proud of (probably done under a tight deadline.) No doubt
    Gregory normally does high quality work.

    @gpshead
    Copy link
    Member

    gpshead commented Aug 16, 2009

    That code was indeed a mess. I've incorporated most suggestions from
    your cleaned up version (and fixed a bug in it) in trunk r74479.

    Have you ever seen __get_builtin_constructor fail in practice? I can
    imagine that packing up a stripped down python interpreter with only a
    subset of the hash extension modules would be the only time this would
    likely be encountered.

    I've changed your code to log an error and continue rather than silently
    continue when an expected hash function is not available. That error
    log may be overkill, perhaps we should just leave catching this problem
    up to the unittests.

    @gpshead gpshead closed this as completed Aug 16, 2009
    @eloff
    Copy link
    Mannequin Author

    eloff mannequin commented Sep 14, 2009

    I have indeed seen __get_builtin_constructor fail in practice, in the
    python build in the Ubuntu repository if IIRC. I seem to recall the
    problem was that python was using a version of openssl that didn't
    include all of hash algorithms, probably because a python library or the
    process hosting python also had a dependency on libssl and had it loaded
    already.

    But there is also the issue that Python is a language with many
    implementations now, and in fact, it's unlikely that CPython will remain
    the defacto implementation forever. Having a more robust hashlib.py that
    can handle missing algorithm implementations makes a lot of sense (.NET
    for example has no implementation of some of the more esoteric SHA
    hashes.) I remember that in the early days of IronPython, hashlib.py had
    to be completely replaced to be used.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants