hashicorp/nomad · error

bitmap must be positive size

Error message

bitmap must be positive size

What it means

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.

Source

Thrown at nomad/structs/bitmap.go:17

// Copyright IBM Corp. 2015, 2026
// SPDX-License-Identifier: BUSL-1.1

package structs

import (
	"fmt"
	"slices"
)

// Bitmap is a simple uncompressed bitmap
type Bitmap []byte

// NewBitmap returns a bitmap with up to size indexes
func NewBitmap(size uint) (Bitmap, error) {
	if size == 0 {
		return nil, fmt.Errorf("bitmap must be positive size")
	}
	if size&7 != 0 {
		return nil, fmt.Errorf("bitmap must be byte aligned")
	}
	b := make([]byte, size>>3)
	return Bitmap(b), nil
}

// Copy returns a copy of the Bitmap
func (b Bitmap) Copy() (Bitmap, error) {
	if b == nil {
		return nil, fmt.Errorf("can't copy nil Bitmap")
	}

	raw := make([]byte, len(b))
	copy(raw, b)
	return Bitmap(raw), nil
}

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Ensure the computed port-range size is > 0 before calling NewBitmap (guard with if size == 0 { skip }).
  2. Fix the network/resource configuration so at least one port exists (e.g. define a non-empty dynamic port range).
  3. If a valid empty state, represent it as a nil Bitmap instead of constructing one.
  4. Check custom device/network plugins for emitting empty host resources.

Example fix

// before
bm, err := structs.NewBitmap(uint(len(ports)))
// after
if len(ports) == 0 {
    return nil, nil // no ports to track
}
bm, err := structs.NewBitmap(uint(len(ports)))
Defensive patterns

Strategy: validation

Validate before calling

if size == 0 {
    // skip bitmap creation entirely
    return nil
}
bm, err := structs.NewBitmap(size)

Try / catch

bm, err := structs.NewBitmap(size)
if err != nil {
    if strings.Contains(err.Error(), "positive size") {
        return fmt.Errorf("no ports/resources to allocate: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling structs.NewBitmap(0) directly, or indirectly via getUsedPortsFor / getDynamicPortsPrecise when a NetworkResource yields zero total ports to represent.

Common situations: 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.

Related errors


AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04). Data as JSON: /api/errors/9ac972049e9649f1. Report an issue: GitHub.