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

PyModule_AddIntConstant and PyModule_AddStringConstant can leak #46115

Closed
hniksic mannequin opened this issue Jan 10, 2008 · 7 comments
Closed

PyModule_AddIntConstant and PyModule_AddStringConstant can leak #46115

hniksic mannequin opened this issue Jan 10, 2008 · 7 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage

Comments

@hniksic
Copy link
Mannequin

hniksic mannequin commented Jan 10, 2008

BPO 1782
Nosy @gvanrossum, @birkenfeld, @tiran, @hniksic
Files
  • addpatch
  • 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 = None
    closed_at = <Date 2008-01-19.18:02:57.045>
    created_at = <Date 2008-01-10.14:47:50.570>
    labels = ['interpreter-core', 'performance']
    title = 'PyModule_AddIntConstant and PyModule_AddStringConstant can leak'
    updated_at = <Date 2008-01-19.18:02:57.043>
    user = 'https://github.com/hniksic'

    bugs.python.org fields:

    activity = <Date 2008-01-19.18:02:57.043>
    actor = 'georg.brandl'
    assignee = 'none'
    closed = True
    closed_date = <Date 2008-01-19.18:02:57.045>
    closer = 'georg.brandl'
    components = ['Interpreter Core']
    creation = <Date 2008-01-10.14:47:50.570>
    creator = 'hniksic'
    dependencies = []
    files = ['9160']
    hgrepos = []
    issue_num = 1782
    keywords = []
    message_count = 7.0
    messages = ['59662', '59664', '59673', '59698', '59716', '59892', '60205']
    nosy_count = 4.0
    nosy_names = ['gvanrossum', 'georg.brandl', 'christian.heimes', 'hniksic']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'resource usage'
    url = 'https://bugs.python.org/issue1782'
    versions = ['Python 2.6', 'Python 3.0']

    @hniksic
    Copy link
    Mannequin Author

    hniksic mannequin commented Jan 10, 2008

    PyModule_AddObject has somewhat strange reference-counting behavior in
    that it *conditionally* steals a reference. In case of error it doesn't
    change the reference to the passed object, but in case of success it
    steals it. This means that, as written, PyModule_AddIntConstant and
    PyModuleAddStringConstant can leak created objects if PyModule_AddObject
    fails.

    As far as I can tell, the correct way to write those functions would be
    (using PyModule_AddIntConstant as the example):

    int 
    PyModule_AddIntConstant(PyObject *m, const char *name, long value)
    {
            PyObject *o = PyInt_FromLong(value);
    	if (PyModule_AddObject(m, name, o) == 0)
                    return 0;
            Py_XDECREF(o);
            return -1;
    }

    PyModule_AddObject was obviously intended to enable writing the "simple"
    code (it even gracefully handles being passed NULL object to add) like
    the one in PyModule_AddIntConstant, but I don't see a way to enable such
    usage and avoid both leaks and an interface change. Changing the
    reference-counting behavior of PyModule_AddObject would be
    backward-incompatible, but it might be a good idea to consider it for
    Python 3.

    If there is agreement that my analysis and the proposed fixes are
    correct, I will produce a proper patch.

    @hniksic hniksic mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage labels Jan 10, 2008
    @tiran
    Copy link
    Member

    tiran commented Jan 10, 2008

    Thanks for your analysis! I *think* you are right but I've to study the
    code more carefully before I can make a decision. We can't target the
    change for 2.5 though. Guido would accuse my of being insane again. *g*
    The problem should be discussed at the upcoming bug day.

    @gvanrossum
    Copy link
    Member

    These are meant purely for the convenience of module initialization, and
    there correct handling of reference counts in the light of failures is
    of marginal importance. So while these may technically have leaks, you
    shouldn't care.

    @hniksic
    Copy link
    Mannequin Author

    hniksic mannequin commented Jan 11, 2008

    I agree that a leak would very rarely occur in practice, but since there
    is a straightforward fix, why not apply it? If nothing else, the code
    in the core should be an example of writing leak-free Python/C code, and
    a fix will also prevent others from wasting time on this in the future.

    @tiran
    Copy link
    Member

    tiran commented Jan 11, 2008

    I don't mind if you like to pursue the issue. I won't invest any time
    into it. But if you can come up with a patch we can surely apply it.

    @hniksic
    Copy link
    Mannequin Author

    hniksic mannequin commented Jan 14, 2008

    Here is a patch, as per description above.

    @birkenfeld
    Copy link
    Member

    Committed in r60084.

    @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
    interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants