{"record":{"id":"9ac972049e9649f1","repo":"hashicorp/nomad","slug":"bitmap-must-be-positive-size","errorCode":null,"errorMessage":"bitmap must be positive size","messagePattern":"bitmap must be positive size","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nomad/structs/bitmap.go","lineNumber":17,"sourceCode":"// Copyright IBM Corp. 2015, 2026\n// SPDX-License-Identifier: BUSL-1.1\n\npackage structs\n\nimport (\n\t\"fmt\"\n\t\"slices\"\n)\n\n// Bitmap is a simple uncompressed bitmap\ntype Bitmap []byte\n\n// NewBitmap returns a bitmap with up to size indexes\nfunc NewBitmap(size uint) (Bitmap, error) {\n\tif size == 0 {\n\t\treturn nil, fmt.Errorf(\"bitmap must be positive size\")\n\t}\n\tif size&7 != 0 {\n\t\treturn nil, fmt.Errorf(\"bitmap must be byte aligned\")\n\t}\n\tb := make([]byte, size>>3)\n\treturn Bitmap(b), nil\n}\n\n// Copy returns a copy of the Bitmap\nfunc (b Bitmap) Copy() (Bitmap, error) {\n\tif b == nil {\n\t\treturn nil, fmt.Errorf(\"can't copy nil Bitmap\")\n\t}\n\n\traw := make([]byte, len(b))\n\tcopy(raw, b)\n\treturn Bitmap(raw), nil\n}","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/nomad/structs/bitmap.go#L1-L35","documentation":"NewBitmap in Nomad's structs package requires a strictly positive size because a zero-size bitmap cannot address any indexes and would allocate nothing meaningful. It rejects size 0 with this error before allocating the backing byte slice. Callers derive it from host resources (ports, dynamic allocations), so a zero here usually means upstream data was empty.","triggerScenarios":"Calling structs.NewBitmap(0) directly, or indirectly via getUsedPortsFor / getDynamicPortsPrecise when a NetworkResource yields zero total ports to represent.","commonSituations":"Plugins or device/host-resource code reporting empty port ranges; tests constructing bitmaps with literal 0; custom network plugins producing zero-length reserved/dynamic port lists.","solutions":["Ensure the computed port-range size is > 0 before calling NewBitmap (guard with if size == 0 { skip }).","Fix the network/resource configuration so at least one port exists (e.g. define a non-empty dynamic port range).","If a valid empty state, represent it as a nil Bitmap instead of constructing one.","Check custom device/network plugins for emitting empty host resources."],"exampleFix":"// before\nbm, err := structs.NewBitmap(uint(len(ports)))\n// after\nif len(ports) == 0 {\n    return nil, nil // no ports to track\n}\nbm, err := structs.NewBitmap(uint(len(ports)))","handlingStrategy":"validation","validationCode":"if size == 0 {\n    // skip bitmap creation entirely\n    return nil\n}\nbm, err := structs.NewBitmap(size)","typeGuard":null,"tryCatchPattern":"bm, err := structs.NewBitmap(size)\nif err != nil {\n    if strings.Contains(err.Error(), \"positive size\") {\n        return fmt.Errorf(\"no ports/resources to allocate: %w\", err)\n    }\n    return err\n}","preventionTips":["Compute sizes from actual resource data and short-circuit when zero.","Check that network plugins never report empty port ranges where a bitmap is expected.","Add unit tests asserting bitmap inputs are > 0."],"tags":["nomad","bitmap","validation","ports"],"backgroundTag":"invalid-input-value","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}