Skip to content

Comments

[Fix] Logical and Formatting Issues in IncrementalPad Function (Issue #642)#670

Merged
Mzack9999 merged 4 commits intoprojectdiscovery:mainfrom
s-cu-bot:fixed_zero_pad
Aug 29, 2025
Merged

[Fix] Logical and Formatting Issues in IncrementalPad Function (Issue #642)#670
Mzack9999 merged 4 commits intoprojectdiscovery:mainfrom
s-cu-bot:fixed_zero_pad

Conversation

@s-cu-bot
Copy link
Contributor

@s-cu-bot s-cu-bot commented Aug 13, 2025

Fix zero‑padding encoding for IPv4 (-zpp, -zpn) and remove duplicates

Summary

This PR fixes the IPv4 zero‑padding feature used by -zpp (zero‑pad permutations) and -zpn (max width) so that it:

  • Generates the correct set of representations for a given -zpn value (no off‑by‑one).
  • Removes duplicate encodings that were produced by the previous nested‑loop approach.
  • Adds/updates tests (ip_test.go) that verify both correctness and absence of duplicates.

Tested on Windows and Linux; the changes are limited to the IP encoding helpers.


Motivation & linked issue

Fixes the behaviour reported in #642:

  • Redundant results: four nested loops iterating over the same range produced many repeated IP strings.
  • Incorrect result set: with padding=2 and ip=192.168.1.1, the result was always 192.168.1.1 instead of the expected permutations, e.g.
    192.168.1.1, 192.168.1.01, 192.168.01.1, 192.168.01.01.
image

This PR addresses both problems and aligns the feature with the CLI flags’ intent.


Issues:

  • %#0Ns with strings does not match numeric zero‑padding semantics and produced surprising results.
  • Nested loops used p? < padding with 0‑based widths, so -zpn N effectively meant widths 0..N-1off‑by‑one.
  • Many duplicates due to formatting of identical widths across octets.

Improvements:

  • Uses numeric formatting (%0*d) so zero‑padding is applied to integer octets exactly like users expect.
  • -zpn N now truly permits widths 1..N. (No off‑by‑one.)
  • Output is unique and deterministic.
  • Guard rails: len(parts) != 4 and padding < 1 cases are handled safely.

CLI examples

-zpn 2 (with -zpp)

echo 127.0.0.1 | mapcidr -if 10 -zpn 2 -zpp -silent
패딩증적(2)

-zpn 3 (with -zpp)

echo 127.0.0.1 | mapcidr -if 10 -zpn 3 -zpp -silent
패딩증적(3)

Tests

  • Updated TestIpEncodings in ip_test.go to assert:
    • No duplicates in the output (via set cardinality vs slice length).
    • Width coverage exactly matches 1..N for -zpn N.
    • FixedPad pads to exactly N digits per octet.

Example run:

go test -v -run TestIpEncodings .
test증적

Complexity & performance

  • The generator performs N⁴ formatted writes for a single IPv4, which is acceptable for small N (the common use‑case is N<=3).
  • Removing duplicates avoids extra work downstream (filtering/reporting), reducing overall runtime for typical pipelines.

Backward compatibility

  • The change corrects the off‑by‑one in -zpn. If someone relied on the previous “N means N-1” behaviour, their result set will expand (never shrink), which is the expected and documented meaning of a “max width”. -zpn 1 remains identical to the original address (no leading zeros).

Checklist

  • Fix off‑by‑one in -zpn
  • Replace string padding with numeric %0*d formatting
  • Deduplicate outputs
  • Update/add tests (ip_test.go)
  • Manual verification on Windows/Linux

@s-cu-bot
Copy link
Contributor Author

It seems that the ASN related test is broken with 401 (unauthorized) due to lack of API key, and there are no other unusual results from the test.

@s-cu-bot s-cu-bot marked this pull request as draft August 13, 2025 23:52
@s-cu-bot s-cu-bot marked this pull request as ready for review August 14, 2025 14:49
@Mzack9999 Mzack9999 linked an issue Aug 29, 2025 that may be closed by this pull request
@Mzack9999 Mzack9999 merged commit 76ea629 into projectdiscovery:main Aug 29, 2025
6 checks passed
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.

Logical and Formatting Issues in IncrementalPad Function

2 participants