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

should pysrp check malloc returns? #10

Open
radii opened this issue Sep 28, 2014 · 1 comment
Open

should pysrp check malloc returns? #10

radii opened this issue Sep 28, 2014 · 1 comment

Comments

@radii
Copy link

radii commented Sep 28, 2014

The C implementation in _srp.c calls malloc and assumes that allocations succeed, which is not guaranteed by the C standard.

Now, there are a couple of possible responses to this observation:

  • perhaps the community standard for Python extensions says that malloc never fails -- I don't know, I have never written a Python module in C.
  • perhaps having the app immediately SEGV is the proper response to malloc failing. Certainly I've never seen a Python application that could deal with allocations failing. I haven't done an exhaustive survey but everywhere I checked, malloc returning NULL results in an immediate NULL pointer dereference, and I think Linux these days is protected against allocating things at address 0.
  • we could switch to a malloc wrapper which explicitly guarantees that failed allocations crash the app with a reasonable error message rather than a potentially obsure SEGV.
  • or, perhaps we do want to handle failed malloc and should modify the ~17 callsites in _srp.c to test for NULL and return errors to their callers.

Thoughts?

@cocagne
Copy link
Owner

cocagne commented Sep 29, 2014

Thanks for giving me the benefit of the doubt on this one radii, but the
failure to check malloc() returns is a definite bug in the code. According
to Python's C-API documentation, the appropriate response to memory
allocation failures is to clean up and return from the extension function
with: "return PyErr_NoMemory();" In fact, it might be better to switch
entirely over to the PyMem_Malloc() and PyMem_Free(). I'd initially planned
on keeping csrp and pysrp close to identical to ease maintenance burdens
but they've already diverged somewhat as it is. The memory allocation
checks, for example, are already in csrp and, for whatever reason, I never
thought to put equivalent ones into pysrp.

Thanks for catching this.

Tom

On Sun, Sep 28, 2014 at 1:28 AM, radii notifications@github.com wrote:

The C implementation in _srp.c calls malloc and assumes that allocations
succeed, which is not guaranteed by the C standard.

Now, there are a couple of possible responses to this observation:

  • perhaps the community standard for Python extensions says that
    malloc never fails -- I don't know, I have never written a Python module in
    C.
  • perhaps having the app immediately SEGV is the proper response to
    malloc failing. Certainly I've never seen a Python application that could
    deal with allocations failing. I haven't done an exhaustive survey but
    everywhere I checked, malloc returning NULL results in an immediate NULL
    pointer dereference, and I think Linux these days is protected against
    allocating things at address 0.
  • we could switch to a malloc wrapper which explicitly guarantees that
    failed allocations crash the app with a reasonable error message rather
    than a potentially obsure SEGV.
  • or, perhaps we do want to handle failed malloc and should modify the
    ~17 callsites in _srp.c to test for NULL and return errors to their callers.

Thoughts?


Reply to this email directly or view it on GitHub
#10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants