Skip to content
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

Divide by zero error when merging unsorted IPv6 addresses #17

Open
evelynhathaway opened this issue Oct 30, 2023 · 2 comments
Open

Divide by zero error when merging unsorted IPv6 addresses #17

evelynhathaway opened this issue Oct 30, 2023 · 2 comments
Labels

Comments

@evelynhathaway
Copy link

I am using merge on an unsorted array of CIDR blocks and ran into this error for IPv6. I cannot reproduce the error in IPv4. I don't see a requirement in the documentation to sort the blocks before merging, so I believe it's a bug.

Minimal Reproduction

import {merge} from "cidr-tools";

// Works, returns an array of two CIDR blocks
merge([
	'1:1:1:1::/128',
	'1:1:1:2::/128',
]);

// Fails, throws a division by zero error
merge([
	'1:1:1:2::/128',
	'1:1:1:1::/128',
]);

Error

node_modules/cidr-tools/index.js:194
        start = ((part.end / biggest) - 1n) * biggest;
                           ^

RangeError: Division by zero
    at subparts (node_modules/cidr-tools/index.js:194:28)
    at merge (node_modules/cidr-tools/index.js:289:27)

Source Line of Code

start = ((part.end / biggest) - 1n) * biggest;

Versions

  • Node.js v20.9.0
  • cidr-tools@7.0.2
@silverwind
Copy link
Owner

Definitely a bug. The smallest numbers I could reproduce with is merge(["::2:0:0/128", "::1:0:0/128"]). At least safeguarding for division by zero does not fix it because the subparts function would recurse forever.

silverwind added a commit that referenced this issue Oct 30, 2023
silverwind added a commit that referenced this issue Oct 30, 2023
* add workaround for #17 (silverwind)
* update related (silverwind)
* add related (silverwind)
* add related (silverwind)
* update deps (silverwind)
@silverwind
Copy link
Owner

silverwind commented Oct 30, 2023

I added a workaround that pre-sorts the networks in merge which does seem to resolve it. It's definitely not the right fix as merge should work with any sorting, but a real fix is likely not so trivial and that's why I'll keep this issue open.

@silverwind silverwind added the bug label Feb 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants