Skip to content

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

Closed

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. dajiaohuang commented on Sep 25, 2026

    @dajiaohuang
  2. prattmic commented on Sep 29, 2026

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

    @prattmic
    Member

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

  6. 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.

  7. dajiaohuang commented on Oct 7, 2026

    @dajiaohuang

    非常抱歉在issue中直接贴了代码patch,给各位维护者添麻烦了。我理解Go项目只能通过Gerrit贡献流程接收patch(https://go.dev/doc/contribute),这是出于版权原因的必要流程。后续如果我认为有合适的修复方案,会严格按照贡献流程通过Gerrit提交。再次致歉!

  8. cagedmantis commented on Oct 13, 2026

    @cagedmantis
    Contributor

    Just a note: IPMask.MarshalText has been discussed in the proposal committee recently. There may be a need to revisit this.

  9. gopherbot commented on Oct 13, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/848108 mentions this issue: net: marshal and unmarshal invalid IPMask values

  10. ianlancetaylor commented on Oct 14, 2026

    @ianlancetaylor
    Contributor

    I sent https://go.dev/cl/848108, which uses the old marshaling for invalid IPMask values rather than returning an error. That seems best for backward compatibility.

  11. added a commit that references this issue on Oct 15, 2026
    c8e4416
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