FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-34543: Fix SystemErrors and segfaults with uninitialized Structs by ZackerySpytz · Pull Request #14777 · python/cpython · GitHub

Repository navigation

bpo-34543: Fix SystemErrors and segfaults with uninitialized Structs - #14777

Closed
ZackerySpytz wants to merge 1 commit into
python:mainfrom
ZackerySpytz:bpo-34543-struct-crashes
Closed

ZackerySpytz wants to merge 1 commit into
python:mainfrom
ZackerySpytz:bpo-34543-struct-crashes

Conversation

ZackerySpytz commented Jul 14, 2019 •
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Contributor

Copy link
Copy Markdown
Contributor Author

This patch uses a CHECK_INITIALIZED() macro (like what is done in Modules/_io), but there are other ways to fix this issue.

aeros left a comment •
edited
Loading

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

In order to test the changes, I attempted to recreate the segfault in the most current commit to cpython master and in your remote branch bpo-34543-struct-crashes.

cpython master:

PR branch:

For the version used for testing the latest cpython, I used the function test_segfault() on the second round in order to perform the test multiple times consecutively. The issue tracker reported similar problems with replication. The first round caused a TypeError, and the second one caused the segfault.

After performing test_segfault() 3 times consecutively in the PR's branch, ValueError was raised each time with the same message. As far as I can tell, this resolves the segfault issue and provides a significant improvement by raising a consistent exception each time.

Nicely done @ZackerySpytz, approved.

Comment thread Lib/test/test_struct.py
for meth in s.iter_unpack, s.pack, s.unpack, s.unpack_from:
self.assertRaises(ValueError, meth, b'0')
self.assertRaises(ValueError, s.pack_into, bytearray(1), 0, b'0')
self.assertRaises(ValueError, s.__sizeof__)

aeros Jul 15, 2019 •
edited
Loading

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Also, realized this after submitting the approval. This is quite minor, and I still approve of the PR either way. Instead of using s.__sizeof__, I would recommend using sys.getsizeof(s):

Suggested change
self.assertRaises(ValueError, s.__sizeof__)
self.assertRaises(ValueError, sys.getsizeof(s))

In general, it seems to be preferable to use functions instead of directly accessing the special object attributes when possible. If you had a specific reason for not using sys.getsizeof(), let me know. Here's the a link to the function defintion and the docs. I looked over it, but I'm not very experienced with the python c-api. I mostly rely on the docs when it comes to the modules implemented in c.

aeros commented Jul 15, 2019 •
edited
Loading

Copy link
Copy Markdown
Contributor

Also, this should probably be backported to previous versions. The code sample I used was the same from 3.7 with no noticeable difference in behavior prior to this patch: https://bugs.python.org/msg324498.

brettcannon added the type-bug An unexpected behavior, bug, or error label Jul 15, 2019

Copy link
Copy Markdown
Contributor

Wouldn't it make more sense to ensure that the invalid objects can't be created in the first place, by doing the initialization in __new__ instead of __init__?

Copy link
Copy Markdown
Contributor

Superseded by #94532

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review type-bug An unexpected behavior, bug, or error

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants


Back | FazBrowse Home | New Git URL