{"record":{"id":"a735b2e33f1e049c","repo":"TheAlgorithms/Go","slug":"provided-size-d-cannot-be-negative","errorCode":null,"errorMessage":"provided size %d cannot be negative","messagePattern":"provided size (.+?) cannot be negative","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cipher/polybius/polybius.go","lineNumber":27,"sourceCode":"import (\n\t\"fmt\"\n\t\"math\"\n\t\"strings\"\n)\n\n// Polybius is struct having size, characters, and key\ntype Polybius struct {\n\tsize       int\n\tcharacters string\n\tkey        string\n}\n\n// NewPolybius returns a pointer to object of Polybius.\n// If the size of \"chars\" is longer than \"size\",\n// \"chars\" are truncated to \"size\".\nfunc NewPolybius(key string, size int, chars string) (*Polybius, error) {\n\tif size < 0 {\n\t\treturn nil, fmt.Errorf(\"provided size %d cannot be negative\", size)\n\t}\n\tkey = strings.ToUpper(key)\n\tif size > len(chars) {\n\t\treturn nil, fmt.Errorf(\"provided size %d is too small to use to slice string %q of len %d\", size, chars, len(chars))\n\t}\n\tfor _, r := range chars {\n\t\tif (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') {\n\t\t\treturn nil, fmt.Errorf(\"provided string %q should only contain latin characters\", chars)\n\t\t}\n\t}\n\tchars = strings.ToUpper(chars)[:size]\n\tfor i, r := range chars {\n\t\tif strings.ContainsRune(chars[i+1:], r) {\n\t\t\treturn nil, fmt.Errorf(\"%q contains same character %q\", chars[i+1:], r)\n\t\t}\n\t}\n\n\tif len(key) != size*size {","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/cipher/polybius/polybius.go#L9-L45","documentation":"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.","triggerScenarios":"Thrown at cipher/polybius/polybius.go:27 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","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"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}