adamchainz / time-machine

Travel through time in your tests.
MIT License
685 stars 35 forks source link

Fix segmentation fault when initializing travel with timedelta #432

Closed marcinsulikowski closed 5 months ago

marcinsulikowski commented 6 months ago

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

adamchainz commented 5 months ago

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