Bug 791278 - Bugs found in clearsilver-0.10.5-17.fc17 using gcc-with-cpychecker static analyzer
Summary: Bugs found in clearsilver-0.10.5-17.fc17 using gcc-with-cpychecker static ana...
Keywords:
Status: CLOSED EOL
Alias: None
Product: Fedora
Classification: Fedora
Component: clearsilver
Version: 19
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: ---
Assignee: Gwyn Ciesla
QA Contact: Fedora Extras Quality Assurance
URL: http://fedorapeople.org/~dmalcolm/gcc...
Whiteboard:
Depends On:
Blocks: cpychecker
TreeView+ depends on / blocked
 
Reported: 2012-02-16 16:13 UTC by Dave Malcolm
Modified: 2015-02-18 13:41 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2015-02-18 13:41:14 UTC
Type: ---
Embargoed:


Attachments (Terms of Use)
Patch to protect against exception in callback (NULL) (632 bytes, patch)
2012-02-17 17:58 UTC, Dave Malcolm
no flags Details | Diff

Description Dave Malcolm 2012-02-16 16:13:43 UTC
Description of problem:
I've been writing an experimental static analysis tool to detect bugs commonly occurring within C Python extension modules:
  https://fedorahosted.org/gcc-python-plugin/
  http://gcc-python-plugin.readthedocs.org/en/latest/cpychecker.html
  http://fedoraproject.org/wiki/Features/StaticAnalysisOfPythonRefcounts

I ran the latest version of the tool (in git master; post 0.9) on
clearsilver-0.10.5-17.fc17.src.rpm, and it reports various errors.

You can see a list of errors here, triaged into categories (from most significant to least significant):
http://fedorapeople.org/~dmalcolm/gcc-python-plugin/2012-02-16/clearsilver-0.10.5-17.fc17/

I've manually inspected the results.

The issue reported in "Reference count too low" appears to be a genuine bug: the code should use:
   Py_RETURN_NONE;
here, rather than
   return Py_None;
to ensure that the reference count of Py_None is incremented (otherwise repeated calls of the setUploadCB method will lead to python crashing).

The 5 issues reported in "Reference leak within initialization" appear to be very minor, given that this appears to be one-time initialization, rather than ongoing code.

The issue reported in "Segfaults within error-handling paths:
  neo_cgi.c:python_upload_cb:dereferencing NULL (result->ob_refcnt) at neo_cgi.c:146
appears to be a genuine segfaulter: if the callback raises an exception, returning NULL, then:
   result = PyEval_CallObject(cb, args);
will be NULL, and, if I'm reading it right, this code will be called:
  144   r = PyInt_AsLong(result);
  145   Py_DECREF(result);
which will segfault the interpreter.

The issue reported in "Possible reference leaks" may well be just a false-positive.

There may of course be other bugs in my checker tool.

Hope this is helpful; let me know if you need help reading the logs that the tool generates - I know that it could use some improvement.

Version-Release number of selected component (if applicable):
clearsilver-0.10.5-17.fc17
gcc-python-plugin post-0.9 git 073d390de53ef52136bd90e5ac06f1ef833d047d running the checker in an *f16* chroot

Comment 1 Gwyn Ciesla 2012-02-16 17:17:02 UTC
Fascinating. . .thank you!  It looks like these haven't been addressed in upstream releases or SVN as of this writing.  I've started a patch.

For the segfaulter, my C is less than it should be, is simply testing result's NULLness and free()ing it as appropriate likely to be sufficient here?

Comment 2 Dave Malcolm 2012-02-16 17:58:51 UTC
(In reply to comment #1)
> Fascinating. . .thank you!  It looks like these haven't been addressed in
> upstream releases or SVN as of this writing.  I've started a patch.

Thanks!

> For the segfaulter, my C is less than it should be, is simply testing result's
> NULLness and free()ing it as appropriate likely to be sufficient here?

That doesn't sound correct to me - you don't typically free() python objects.

If I'm reading python_upload_cb right, the case where result is non-NULL *is* being correctly cleaned up.  The issue is when result is NULL: something ought to check for result == NULL and do something appropriate, similar to the error-handling for the case when the callback returns a non-int value.

FWIW, a guide to the C Python API can be seen here:
  http://docs.python.org/extending/index.html
and detailed notes are here:
  http://docs.python.org/c-api/index.html

though if you're not yet familiar with this API, it's probably best to engage with upstream.

Hope this is helpful
Dave

Comment 3 Gwyn Ciesla 2012-02-16 18:09:08 UTC
Ah, I see.

I think it's nearly correct, except it looks like not all cases.  What if I modified:

if (result != NULL && !PyInt_Check(result)) {
    Py_DECREF(result);
    result = NULL;
    PyErr_SetString(PyExc_TypeError,
        "upload_cb () returned non-integer");
    self->upload_error = 1;
    return 1;
  }
  r = PyInt_AsLong(result);
  Py_DECREF(result);
  result = NULL;
  return r;


thusly:

if (result != NULL) {
  if (!PyInt_Check(result)) {
   {
      Py_DECREF(result);
      result = NULL;
      PyErr_SetString(PyExc_TypeError,
          "upload_cb () returned non-integer");
      self->upload_error = 1;
      return 1;
   }
  } else {
    r = PyInt_AsLong(result);
    Py_DECREF(result);
    result = NULL;
    return r;
  }

This way, in no case does the result get defererenced if NULL.  At least here. :)

Comment 4 Dave Malcolm 2012-02-17 17:58:59 UTC
Created attachment 563952 [details]
Patch to protect against exception in callback (NULL)

(In reply to comment #3)
> if (result != NULL) {
>   if (!PyInt_Check(result)) {
>    {
>       Py_DECREF(result);
>       result = NULL;
>       PyErr_SetString(PyExc_TypeError,
>           "upload_cb () returned non-integer");
>       self->upload_error = 1;
>       return 1;
>    }
>   } else {
>     r = PyInt_AsLong(result);
>     Py_DECREF(result);
>     result = NULL;
>     return r;
>   }

Am I right in thinking you still have the trailing "return r" at the end?  Isn't r still uninitialized at this point?

Am attaching a patch that (I hope) covers this case.  Applies cleanly, but untested though.

Comment 5 Gwyn Ciesla 2012-02-17 18:18:40 UTC
No, my version went to the end of the function.  So I suppose that would have left open the possibility of not returning anything.  

I'll try your patch.

Comment 6 Gwyn Ciesla 2012-02-17 18:42:36 UTC
Maybe I'm foggy today, but how can I run this against my patched RPM?

Comment 7 Fedora End Of Life 2013-04-03 19:31:05 UTC
This bug appears to have been reported against 'rawhide' during the Fedora 19 development cycle.
Changing version to '19'.

(As we did not run this process for some time, it could affect also pre-Fedora 19 development
cycle bugs. We are very sorry. It will help us with cleanup during Fedora 19 End Of Life. Thank you.)

More information and reason for this action is here:
https://fedoraproject.org/wiki/BugZappers/HouseKeeping/Fedora19

Comment 8 Fedora End Of Life 2015-01-09 21:56:09 UTC
This message is a notice that Fedora 19 is now at end of life. Fedora 
has stopped maintaining and issuing updates for Fedora 19. It is 
Fedora's policy to close all bug reports from releases that are no 
longer maintained. Approximately 4 (four) weeks from now this bug will
be closed as EOL if it remains open with a Fedora 'version' of '19'.

Package Maintainer: If you wish for this bug to remain open because you
plan to fix it in a currently maintained version, simply change the 'version' 
to a later Fedora version.

Thank you for reporting this issue and we are sorry that we were not 
able to fix it before Fedora 19 is end of life. If you would still like 
to see this bug fixed and are able to reproduce it against a later version 
of Fedora, you are encouraged  change the 'version' to a later Fedora 
version prior this bug is closed as described in the policy above.

Although we aim to fix as many bugs as possible during every release's 
lifetime, sometimes those efforts are overtaken by events. Often a 
more recent Fedora release includes newer upstream software that fixes 
bugs or makes them obsolete.

Comment 9 Fedora End Of Life 2015-02-18 13:41:14 UTC
Fedora 19 changed to end-of-life (EOL) status on 2015-01-06. Fedora 19 is
no longer maintained, which means that it will not receive any further
security or bug fix updates. As a result we are closing this bug.

If you can reproduce this bug against a currently maintained version of
Fedora please feel free to reopen this bug against that version. If you
are unable to reopen this bug, please file a new report against the
current release. If you experience problems, please add a comment to this
bug.

Thank you for reporting this bug and we are sorry it could not be fixed.


Note You need to log in before you can comment on or make changes to this bug.