k3s-io/k3s · error

fields for file %s (%d) smaller than required index (key: %d

Error message

fields for file %s (%d) smaller than required index (key: %d, val: %d)

What it means

fileMapFields splits each non-blank line of .sha256sums (key index 1, val index 0) or .links (key 0, val 1) on whitespace. A non-empty line with fewer fields than the required indices - i.e. a single bare token instead of '<a> <b>' - aborts parsing with this error naming the file and the field count.

Source

Thrown at pkg/dataverify/dataverify.go:99

	}
	return nil
}

func fileMapFields(fileName string, key, val int) (map[string]string, error) {
	file, err := os.Open(fileName)
	if err != nil {
		return nil, err
	}
	defer file.Close()
	result := map[string]string{}
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		fields := strings.Fields(scanner.Text())
		if len(fields) == 0 {
			continue
		}
		if len(fields) <= key || len(fields) <= val {
			return nil, fmt.Errorf("fields for file %s (%d) smaller than required index (key: %d, val: %d)", fileName, len(fields), key, val)
		}
		result[fields[key]] = fields[val]
	}
	return result, scanner.Err()
}

func sha256Sum(filePath string) (string, error) {
	file, err := os.Open(filePath)
	if err != nil {
		return "", err
	}
	defer file.Close()
	hash := sha256.New()
	if _, err := io.Copy(hash, file); err != nil {
		return "", err
	}
	return hex.EncodeToString(hash.Sum(nil)), nil
}

View on GitHub (pinned to 6ba341e396)

Solutions

  1. Inspect the named file and fix or remove the malformed line - every entry must have exactly the two whitespace-separated columns (sum + path, or link + target).
  2. Regenerate the sums file properly: `cd <dir> && sha256sum $(cat .sha256sums | awk '{print $2}') > .sha256sums` rather than editing by hand.
  3. For a shipped k3s bundle, do not repair it - delete <data-dir>/data/<version>-tmp and reinstall/re-extract so the original manifests are restored.

Example fix

# before (.sha256sums contains a bare token line)
1f0e...dad  kubectl
e38ad2...(no filename)

# after
1f0e...dad  kubectl
e38ad214943...91f  containerd
Defensive patterns

Strategy: validation

Validate before calling

// Validate manifest line shape before use:
func validManifest(fileName string, key, val int) error {
    sc := bufio.NewScanner(mustOpen(fileName))
    for sc.Scan() {
        f := strings.Fields(sc.Text())
        if len(f) == 0 { continue }
        if len(f) <= key || len(f) <= val {
            return fmt.Errorf("malformed line in %s: %q", fileName, sc.Text())
        }
    }
    return sc.Err()
}

Prevention

When it happens

Trigger: A malformed line in .sha256sums or .links inside the extracted bundle: a lone hash with no filename, or a lone path with no target (pkg/dataverify/dataverify.go:91-100).

Common situations: Hand-edited or hand-regenerated manifest where a line lost its second column; concatenation of files with headers/footers; encoding issues that remove the separating whitespace; wrapping that split one entry across lines.

Related errors


AI-assisted analysis of k3s-io/k3s@6ba341e396 (2026-08-15). Data as JSON: /api/errors/773b2ed62a6760fc. Report an issue: GitHub.