Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-35989: Forbid a netmask > 32 for ipaddress.IPv4Network #11844

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

matrixise
Copy link
Member

@matrixise matrixise commented Feb 13, 2019

Lib/ipaddress.py Outdated
mask = address[1] if len(address) > 1 else self._max_prefixlen
if len(address) > 1:
if isinstance(address[1], int) and 0 < address[1] > 32:
raise NetmaskValueError("The netmask should be isn't valid")

Choose a reason for hiding this comment

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

This error message doesn't read well.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, this can be reworded and could include the max value (32) in the error so that users know the limit.

Copy link
Member Author

Choose a reason for hiding this comment

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

really strange, that was not my original message. Maybe a bad paste :/

@matrixise
Copy link
Member Author

@tirkarthi @jflorian maybe we could validate this PR and ping a core-dev.

Lib/ipaddress.py Outdated
@@ -1519,7 +1519,13 @@ def __init__(self, address, strict=True):
# Constructing from a tuple (addr, [mask])
elif isinstance(address, tuple):
addr = address[0]
mask = address[1] if len(address) > 1 else self._max_prefixlen
if len(address) > 1:
if isinstance(address[1], int) and 0 < address[1] > 32:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if isinstance(address[1], int) and 0 < address[1] > 32:
if isinstance(address[1], int) and address[1] > 32:

if address is larger than 32, it is larger than 0.

@matrixise
Copy link
Member Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

: please review the changes made to this pull request.

@tirkarthi
Copy link
Member

I believe this is fixed in master with https://bugs.python.org/issue36845 and backported to 3.7 .

./python.exe
Python 3.8.0a4+ (heads/master:73934b9da0, May 18 2019, 17:28:10)
[Clang 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ipaddress
>>> ipaddress.IPv4Network(('192.0.2.1', 33))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/ipaddress.py", line 1450, in __init__
    self.netmask, self._prefixlen = self._make_netmask(mask)
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/ipaddress.py", line 1112, in _make_netmask
    cls._report_invalid_netmask(prefixlen)
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/ipaddress.py", line 474, in _report_invalid_netmask
    raise NetmaskValueError(msg) from None
ipaddress.NetmaskValueError: 33 is not a valid netmask

@remilapeyre
Copy link
Contributor

Yes, this seems to have been fixed and is not an issue on master anymore:

➜  cpython git:(master) ./python      
Python 3.10.0a0 (heads/master:07d8112812, Jun 10 2020, 23:50:22) 
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ipaddress
>>> ipaddress.IPv4Network(('192.0.2.1', 33))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/remi/src/cpython/Lib/ipaddress.py", line 1500, in __init__
    self.netmask, self._prefixlen = self._make_netmask(mask)
  File "/Users/remi/src/cpython/Lib/ipaddress.py", line 1162, in _make_netmask
    cls._report_invalid_netmask(prefixlen)
  File "/Users/remi/src/cpython/Lib/ipaddress.py", line 474, in _report_invalid_netmask
    raise NetmaskValueError(msg) from None
ipaddress.NetmaskValueError: 33 is not a valid netmask

@matrixise
Copy link
Member Author

hi all, I will follow this PR and the associated BPO. Thanks for the reminder.

Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting change review stale Stale PR or inactive for long period of time.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants