TheAlgorithms/Go · error
provided size %d cannot be negative
Error message
provided size %d cannot be negative
What it means
Constructor validation error from NewPolybius: it fires when the caller passes a negative size, since the Polybius grid dimension must be a non-negative integer to slice the character set and square it into a key grid.
Source
Thrown at cipher/polybius/polybius.go:27
import (
"fmt"
"math"
"strings"
)
// Polybius is struct having size, characters, and key
type Polybius struct {
size int
characters string
key string
}
// NewPolybius returns a pointer to object of Polybius.
// If the size of "chars" is longer than "size",
// "chars" are truncated to "size".
func NewPolybius(key string, size int, chars string) (*Polybius, error) {
if size < 0 {
return nil, fmt.Errorf("provided size %d cannot be negative", size)
}
key = strings.ToUpper(key)
if size > len(chars) {
return nil, fmt.Errorf("provided size %d is too small to use to slice string %q of len %d", size, chars, len(chars))
}
for _, r := range chars {
if (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') {
return nil, fmt.Errorf("provided string %q should only contain latin characters", chars)
}
}
chars = strings.ToUpper(chars)[:size]
for i, r := range chars {
if strings.ContainsRune(chars[i+1:], r) {
return nil, fmt.Errorf("%q contains same character %q", chars[i+1:], r)
}
}
if len(key) != size*size {View on GitHub (pinned to 5ba447ec5f)
Solutions
- Pass a positive size (typically 5 for a 5x5 square)
- Validate user-supplied size before constructing the cipher
- Default to a standard size when input is untrusted
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at cipher/polybius/polybius.go:27 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/a735b2e33f1e049c.
Report an issue: GitHub.