jaraco / jaraco.windows

MIT License
29 stars 7 forks source link

Null Pointer exception in GetAdaptersAddresses #3

Closed jaraco closed 8 years ago

jaraco commented 10 years ago

Originally reported by: Jason R. Coombs (Bitbucket: jaraco, GitHub: jaraco)


Mark Woan reports:

Whilst calling the GetAdaptersAddresses method (inet.py) I found that it would give a null pointer exception when it attempted to enumerate a non-existant network adapter as the struct_p variable always appears to be valid.

I have only just moved over to python from C# so I am not sure my fix is optimal! In the end I changed the loop like so:

while struct_p:
        yield struct_p.contents
        struct_p = struct_p.contents.next
        if hasattr(struct_p, 'contents.next') == False:
            break

Below is a screenshot from Visual Studio showing the contents of struct_p

debug.png


jaraco commented 10 years ago

Original comment by Jason R. Coombs (Bitbucket: jaraco, GitHub: jaraco):


I don't believe an issue has been demonstrated and the workaround is surely not correct, so I'm going to close this issue unless additional information comes to light that clarifies that an issue exists.

jaraco commented 10 years ago

Original comment by Jason R. Coombs (Bitbucket: jaraco, GitHub: jaraco):


I should point out that .contents simply de-references a pointer, so struct_p.contents in Python (when using ctypes) is equivalent to *struct_p in C.

At the point where struct_p.contents is referenced, struct_p is known to be non-null (because of the while struct_p test immediately before). It's conceivable that struct_p.contents.next may be null, but that's fine, because it's subsequently tested by while struct_p.

I don't see an issue here. Can you provide more detail on how you triggered the failure?

jaraco commented 10 years ago

Original comment by Jason R. Coombs (Bitbucket: jaraco, GitHub: jaraco):


I'm certain that

hasattr(struct_p, 'contents.next')

Is not a suitable test. Python doesn't recognize periods in attribute names. That expression will always be False, so will essentially always terminate the loop.