Skip to content

net: IPMask.MarshalText silently returns empty output for masks that are not 4 or 16 bytes long #81749

Open

Description

@Sanjays2402

net.IPMask.MarshalText gained a new text format in the Go 1.28 cycle (issue #29678), gated behind GODEBUG=netmarshal=1. For a mask whose length is not 0, 4, or 16 bytes, the new path returns empty bytes with a nil error instead of an error, and the data is silently destroyed:

m := net.IPMask{255, 255, 255, 0, 1}
b, err := m.MarshalText()  // GODEBUG=netmarshal=1: b="", err=nil
j, _ := json.Marshal(m)    // j = ""
var m2 net.IPMask
json.Unmarshal(j, &m2)     // m2 = [] -- the original 5 bytes are gone

(The old base64 format round-trips the same mask fine.)

Root cause in src/net/ip.go:

addr, _ := netip.AddrFromSlice(m)
return addr.AppendTo(nil), nil

The ok result is discarded; on failure addr is the zero Addr and AppendTo(nil) yields nothing. The sibling IP.MarshalText returns an honest error (address 0102030405: invalid IP address) for invalid lengths, so this is also internally inconsistent. The existing ipMaskJSONTests table only covers nil/4-byte/16-byte masks, which is why it slipped through.

Expected: MarshalText returns a non-nil error for a mask that cannot be represented as an IP address.

Seen on master at 8190b02c (2026-09-25), linux/amd64, go1.28-devel.

Activity

  1. prattmic commented on Sep 29, 2026

    @prattmic
    Member
  2. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Sep 29, 2026
  3. added this to the Go1.28 milestone on Sep 29, 2026
  4. prattmic commented on Sep 29, 2026

    @prattmic
    Member

    Marked as a release blocker to ensure this is investigated before the 1.28 release.

  5. ianlancetaylor commented on Sep 29, 2026

    @ianlancetaylor
    Contributor

    I will take a look at this.

    @dajiaohuang Please never send code patches in the issue tracker. For copyright reasons we can only use patches that go through the contribution process (https://go.dev/doc/contribute). Patches in the issue tracker make our work harder, not easier. Thanks.

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

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.release-blocker

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions