TheAlgorithms/Go · error

the size of "chars" must be even

Error message

the size of "chars" must be even

What it means

decipher expects exactly two runes (a row char and a column char); being handed 0 or 1 runes means the ciphertext had odd length or the slicing window was wrong, so it reports the pair-size requirement.

Source

Thrown at cipher/polybius/polybius.go:91

		decryptedText += decryptedChar
	}
	return decryptedText, nil
}

func (p *Polybius) encipher(char rune) (string, error) {
	index := strings.IndexRune(p.key, char)
	if index < 0 {
		return "", fmt.Errorf("%q does not exist in keys", char)
	}
	row := index / p.size
	col := index % p.size
	chars := []rune(p.characters)
	return string([]rune{chars[row], chars[col]}), nil
}

func (p *Polybius) decipher(chars []rune) (string, error) {
	if len(chars) != 2 {
		return "", fmt.Errorf("the size of \"chars\" must be even")
	}
	row := strings.IndexRune(p.characters, chars[0])
	if row < 0 {
		return "", fmt.Errorf("%c does not exist in characters", chars[0])
	}
	col := strings.IndexRune(p.characters, chars[1])
	if col < 0 {
		return "", fmt.Errorf("%c does not exist in characters", chars[1])
	}
	return string(p.key[row*p.size+col]), nil
}

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Check len(text)%2==0 before decrypting and reject odd-length ciphertext
  2. Ensure ciphertext was produced by this cipher (pairs of characters)
  3. Handle the trailing leftover rune explicitly instead of calling decipher with one rune
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at cipher/polybius/polybius.go:91 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02). Data as JSON: /api/errors/a8b0b4d4892c2b0e. Report an issue: GitHub.