Skip to content

Commit

Permalink
Merge pull request #7650 from drew2a/fix/5713
Browse files Browse the repository at this point in the history
Set the limit for int values of the libtorrent config
  • Loading branch information
drew2a committed Oct 27, 2023
2 parents 9b5657b + 3567e93 commit cbf0056
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/tribler/core/components/libtorrent/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from tribler.core.utilities.osutils import get_home_dir
from tribler.core.utilities.path_util import Path

INT32_MAX = 2147483647 # max int value for C

TRIBLER_DOWNLOADS_DEFAULT = "TriblerDownloads"


Expand Down Expand Up @@ -43,6 +45,12 @@ def validate_proxy_type(cls, v):
assert v is None or 0 <= v <= 5, 'Proxy type must be in range [0..5]'
return v

def __setattr__(self, key, value):
"""Override __setattr__ to limit the max int value to `INT_MAX` (the max int value for C)"""
if isinstance(value, int):
value = min(INT32_MAX, value)
super().__setattr__(key, value)


class SeedingMode(str, Enum):
forever = 'forever'
Expand Down
11 changes: 10 additions & 1 deletion src/tribler/core/components/libtorrent/tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from tribler.core.components.libtorrent.settings import TRIBLER_DOWNLOADS_DEFAULT, get_default_download_dir
from tribler.core.components.libtorrent.settings import INT32_MAX, TRIBLER_DOWNLOADS_DEFAULT, get_default_download_dir
from tribler.core.config.tribler_config import TriblerConfig
from tribler.core.utilities.path_util import Path


Expand Down Expand Up @@ -36,3 +37,11 @@ def test_get_default_home_nothing_exists(tmp_path, monkeypatch):

download_dir = get_default_download_dir(home)
assert download_dir == (home / TRIBLER_DOWNLOADS_DEFAULT).resolve()


def test_big_int_setter(tmpdir):
# Test the setter for big integers. In the case of overflow, the value should be set to INT_MAX
config = TriblerConfig(state_dir=tmpdir)
big_int = 2 ** 1000
config.libtorrent.max_connections_download = big_int
assert config.libtorrent.max_connections_download == INT32_MAX

0 comments on commit cbf0056

Please sign in to comment.