Skip to content

[ZSvirt] bug: NetworkUtils.convertNetmask(0) returns the wrong mask #31

Description

@tllovesxs

Component

utils / network

Problem description

NetworkUtils.convertNetmask(Integer prefix) computes the mask with:

0xffffffff << (32 - prefix)

In Java, shifting an int by 32 is equivalent to shifting by 0. Therefore convertNetmask(0) returns 255.255.255.255 instead of the correct 0.0.0.0.

Relevant code:
https://github.com/ZSvirt/zsvirt/blob/main/utils/src/main/java/org/zstack/utils/network/NetworkUtils.java#L816-L829

This affects callers that compare or apply a valid IPv4 /0 configuration.

Steps to reproduce

Call NetworkUtils.convertNetmask(0).

Expected behavior

Return 0.0.0.0 for prefix 0. Prefixes outside the IPv4 range should be rejected explicitly.

Proposed fix

Handle prefix 0 before the shift and validate that the prefix is in [0, 32]. Add boundary tests for 0, 1, 24, and 32.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions