TheAlgorithms/Python · error · ValueError
Invalid IPv4 octet {octet}
Error message
Invalid IPv4 octet {octet} What it means
Raised by ipv4_to_decimal(ipv4_address) in conversions/ipv4_conversion.py:35 when an octet parses as an integer but falls outside 0..255. The function checks each of the four parts after the count check, so a well-formed dotted quad with an out-of-range value ('10.0.0.256', '300.1.1.1', '-1.0.0.0' style negatives) raises ValueError(f"Invalid IPv4 octet {octet}") naming the offending octet.
Source
Thrown at conversions/ipv4_conversion.py:35
167772415
>>> ipv4_to_decimal("10.0.255")
Traceback (most recent call last):
...
ValueError: Invalid IPv4 address format
>>> ipv4_to_decimal("10.0.0.256")
Traceback (most recent call last):
...
ValueError: Invalid IPv4 octet 256
"""
octets = [int(octet) for octet in ipv4_address.split(".")]
if len(octets) != 4:
raise ValueError("Invalid IPv4 address format")
decimal_ipv4 = 0
for octet in octets:
if not 0 <= octet <= 255:
raise ValueError(f"Invalid IPv4 octet {octet}") # noqa: EM102
decimal_ipv4 = (decimal_ipv4 << 8) + int(octet)
return decimal_ipv4
def alt_ipv4_to_decimal(ipv4_address: str) -> int:
"""
>>> alt_ipv4_to_decimal("192.168.0.1")
3232235521
>>> alt_ipv4_to_decimal("10.0.0.255")
167772415
"""
return int("0x" + "".join(f"{int(i):02x}" for i in ipv4_address.split(".")), 16)
def decimal_to_ipv4(decimal_ipv4: int) -> str:
"""
Convert a decimal representation of an IP address to its IPv4 format.View on GitHub (pinned to f5988cc097)
Solutions
- Clamp or wrap overflowing octets: carry into the previous octet when the last exceeds 255
- Validate up front with ipaddress.IPv4Address(addr) and catch ValueError
- If generating from integers, ensure the source int is already in 0..4294967295 and use decimal_to_ipv4 instead
Example fix
# before
ip = f'10.0.0.{250 + i}' # i > 5 -> ValueError
ipv4_to_decimal(ip)
# after
import ipaddress
ip = str(ipaddress.IPv4Address(ipaddress.IPv4Address('10.0.0.250') + i))
ipv4_to_decimal(ip) Defensive patterns
Strategy: validation
Validate before calling
parts = ipv4_address.split('.')
if len(parts) != 4 or not all(p.isdigit() and 0 <= int(p) <= 255 for p in parts):
raise ValueError(f'invalid IPv4: {ipv4_address!r}') Prevention
- Carry overflow into higher octets when incrementing addresses
- Use ipaddress.IPv4Address + int arithmetic for IP math
- Validate with ipaddress before passing user-typed addresses
When it happens
Trigger: ipv4_to_decimal('10.0.0.256'), ipv4_to_decimal('999.999.999.999'), or octets computed arithmetically (base + offset) that overflow 255.
Common situations: Incrementing the last octet past 255 when generating IPs in a loop; user-typed addresses with typos; values from counters that wrap into the next /24 without carries.
Related errors
- Invalid decimal IPv4 address
- Invalid IPv4 address format
- base must be >= 2
- base must be <= 36
- No input value was provided
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/65c2c1ae455ac8e8.
Report an issue: GitHub.