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

Fix segmentation fault when initializing travel with timedelta #432

Conversation

marcinsulikowski
Copy link

@marcinsulikowski marcinsulikowski commented Mar 22, 2024

The following Python script:

from datetime import timedelta
import time_machine
with time_machine.travel(timedelta()):
    pass

crashes due to a segmentation fault in the constructor of the travel class.

The crash happens when the constructor tries to determine the destination timestamp like this:

timestamp = time() + dest.total_seconds()

where time() calls result = _time_machine.original_time() which crashes if time_machine's patches are not applied yet and thus the original_time function pointer is still NULL.

This happens when trying to initialize travel with timedelta only when travel was not successfully used yet in the same process before. Those conditions are hard to meet in the test cases of this library so we fix the bug without adding a new test that would prove that the fix works.

Closes #431

The following Python script:

    from datetime import timedelta
    import time_machine
    with time_machine.travel(timedelta()):
        pass

crashes due to a segmentation fault in the constructor of the `travel`
class.

The crash happens when the constructor tries to determine the destination
timestamp like this:

    timestamp = time() + dest.total_seconds()

where `time()` calls `result = _time_machine.original_time()` which
crashes if time_machine's patches are not applied yet and thus the
`original_time` function pointer is still `NULL`.

This happens only when trying to initialize `travel` with `timedelta`
only when `travel` was not successfully used yet in the same process
before. Those conditions are hard to meet in the test cases of this
library so we fix the bug without adding a new test that would prove
that the fix works.

Closes adamchainz#431
@adamchainz
Copy link
Owner

Went with an alternative approach in #433, per comment on #431.

@adamchainz adamchainz closed this Mar 22, 2024
@marcinsulikowski marcinsulikowski deleted the fix-segfault-when-initializing-with-timedelta branch March 22, 2024 23:40
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

Successfully merging this pull request may close these issues.

Segmentation fault in time_machine_travel(timedelta())
2 participants