Skip to content

Fix IPv4 validation to reject leading zeros - #7

Open
Dszae wants to merge 2 commits into
csfive:mainfrom
Dszae:week7_01
Open

Dszae wants to merge 2 commits into
csfive:mainfrom
Dszae:week7_01

Conversation

@Dszae

@Dszae Dszae commented Nov 21, 2026 •

Copy link
Copy Markdown

Problem:
The existing IPv4 validation incorrectly accepts addresses containing leading zeros.
For example: 000.001.010.100 : Expected: False, Actual: True

Changes:

  • Updated IPv4 address validation.
  • Rejects octets with leading zeros, the same as Python's own ipaddress module (it rejects them since 3.9.5).
  • Ensures each octet is between 0 and 255.
  • Validates the complete IPv4 address.
  • Matches ASCII digits only (re.ASCII). Without it, Python's \d also matches digits from other scripts, so 1٩٢.168.1.1 validated, on main too.

Tests:

  • test_numb3rs.py now covers leading zeros (000.001.010.100, 01.2.3.4, 1.2.3.04, 1.00.3.4), a lone 0 octet (0.0.0.0), and non-ASCII digits. The new tests fail on main.
  • pytest test_numb3rs.py: 3 passed.
  • Cross-checked validate against ipaddress.IPv4Address on 20,000 random inputs: no disagreements.

Co-authored-by: Claude noreply@anthropic.com

Dszae and others added 2 commits November 21, 2026 07:41
Without re.ASCII, Python's \d also matches digits from other scripts, so
"1٩٢.168.1.1" (Arabic-Indic 9 and 2) still validated. Also drops the
blank line added above the import.

test_numb3rs.py now covers leading zeros (000.001.010.100, 01.2.3.4,
1.2.3.04, 1.00.3.4), a lone 0 octet (0.0.0.0), and non-ASCII digits.
Checked against Python's ipaddress.IPv4Address on 20,000 random inputs:
no disagreements.

Co-authored-by: Claude <noreply@anthropic.com>
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.

1 participant