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

Issue#4 #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Improve error handling and store variables sent to the clibrary in th…
…e class to avoid pointer dereferencement because of potential garbage collection
Olivier Medoc committed Apr 5, 2016
commit 334683b470c0dd0503e2e6697ba3e4e0c99e892b
15 changes: 12 additions & 3 deletions pynetfilter_conntrack/conntrack.py
Original file line number Diff line number Diff line change
@@ -24,9 +24,14 @@ def register_callback(self, callback, event_type=NFCT_T_ALL, data=None):
return: NFCT_CB_CONTINUE, NFCT_CB_FAILURE, NFCT_CB_STOP,
or NFCT_CB_STOLEN (like continue, but ct is not freed).
"""
self.event_type = event_type
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad indent here too.

self.callback = nfct_callback_t(callback)
self.callback_arg = data
nfct_callback_register(self.handle, event_type, self.callback, self.callback_arg)
ret = nfct_callback_register(self.handle, self.event_type, self.callback, self.callback_arg)
if ret == -1:
self._error('nfct_callback_register')
elif ret != 0:
self._error('nfct_callback_register unknown return code')

def unregister_callback(self):
"""Unregister callback"""
@@ -90,9 +95,13 @@ def query(self, command, argument):

May raise a RuntimeError.
"""
ret = nfct_query(self.handle, command, argument)
if ret != 0:
self.query_type = command
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indenting looks weird here ? That should confuse Python.


ret = nfct_query(self.handle, self.query_type, byref(argument))
if ret == -1:
self._error('nfct_query')
elif ret != 0:
self._error('nfct_query unknown return code')

def catch(self, callback):
"""
7 changes: 5 additions & 2 deletions pynetfilter_conntrack/conntrack_base.py
Original file line number Diff line number Diff line change
@@ -16,9 +16,12 @@ def __init__(self, subsys, subscriptions):
self.conntrack = None
self.handle = None

self.subsys = subsys
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is it use ? Do you plan to do something with it ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used on next list (open a conntrack handler), however it is not really used in the class.

The reason is that I saw several reports of dereferencements of data when using python clib because the garbage collector lost track of a variable used in the c library and cleaned it up.
I did not analysed this carefully, but I try to make sure the garbage collector will not dereference the data by storing systematically in the class variables that are used in the c library calls.

self.subscriptions = subscriptions
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.


# Open a conntrack handler
self.handle = nfct_open(subsys, subscriptions)
if not self.handle:
self.handle = nfct_open(self.subsys, self.subscriptions)
if self.handle == None:
self._error('nfct_new')

def _error(self, func_name):