{"record":{"id":"c85cd13765b2f5b2","repo":"docker/cli","slug":"invalid-label-s-empty-name","errorCode":null,"errorMessage":"invalid label '%s': empty name","messagePattern":"invalid label '(.+?)': empty name","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"opts/opts.go","lineNumber":243,"sourceCode":"\nconst whiteSpaces = \" \\t\"\n\n// ValidateLabel validates that the specified string is a valid label, and returns it.\n//\n// Labels are in the form of key=value; key must be a non-empty string, and not\n// contain whitespaces. A value is optional (defaults to an empty string if omitted).\n//\n// Leading whitespace is removed during validation but values are kept as-is\n// otherwise, so any string value is accepted for both, which includes whitespace\n// (for values) and quotes (surrounding, or embedded in key or value).\n//\n// TODO discuss if quotes (and other special characters) should be valid or invalid for keys\n// TODO discuss if leading/trailing whitespace in keys should be preserved (and valid)\nfunc ValidateLabel(value string) (string, error) {\n\tkey, _, _ := strings.Cut(value, \"=\")\n\tkey = strings.TrimLeft(key, whiteSpaces)\n\tif key == \"\" {\n\t\treturn \"\", fmt.Errorf(\"invalid label '%s': empty name\", value)\n\t}\n\tif strings.ContainsAny(key, whiteSpaces) {\n\t\treturn \"\", fmt.Errorf(\"label '%s' contains whitespaces\", key)\n\t}\n\treturn value, nil\n}\n\n// ValidateSysctl validates a sysctl and returns it.\nfunc ValidateSysctl(val string) (string, error) {\n\tvalidSysctlMap := map[string]bool{\n\t\t\"kernel.msgmax\":          true,\n\t\t\"kernel.msgmnb\":          true,\n\t\t\"kernel.msgmni\":          true,\n\t\t\"kernel.sem\":             true,\n\t\t\"kernel.shmall\":          true,\n\t\t\"kernel.shmmax\":          true,\n\t\t\"kernel.shmmni\":          true,\n\t\t\"kernel.shm_rmid_forced\": true,","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/opts/opts.go#L225-L261","documentation":"Thrown by ValidateLabel (opts.go:243) when, after splitting on '=' and trimming leading whitespace, the key is empty. Docker labels are key=value pairs; the key (label name) is mandatory. A value may be omitted (defaults to empty), but a key with no name — e.g. `=value`, `  =value`, or just `=` — is rejected.","triggerScenarios":"Calling ValidateLabel with `=foo`, ` =foo`, `=`, or a string whose only content before '=' is whitespace. strings.Cut at line 240 yields an empty key, TrimLeft at 241 leaves it empty, and the check at 242 fires.","commonSituations":"Shell-expanding an empty variable as the key (`--label =$VAR`), malformed config from a YAML/JSON file where the key was null, copy-paste dropping the key, or building label strings by concatenation with a missing left operand.","solutions":["Provide a non-empty key before the '=', e.g. `--label com.example.team=payments`.","If constructing labels programmatically, check the key is non-empty before joining with '='.","For a label with no value, use `--label com.example.flag` (the value is optional) rather than `--label =x`.","Audit env-var substitution so empty variables don't become empty keys."],"exampleFix":"// before\n--label =$LABEL_VALUE\n// after\n--label com.example.role=$LABEL_VALUE","handlingStrategy":"validation","validationCode":"// Verify the label has a non-empty key before joining/validating.\nfunc validLabel(s string) error {\n    key, _, _ := strings.Cut(s, \"=\")\n    if strings.TrimLeft(key, \" \\t\") == \"\" {\n        return fmt.Errorf(\"label key is empty in %q\", s)\n    }\n    return nil\n}\n\nif err := validLabel(l); err != nil { return err }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never use an empty/unset env var as the label key.","Build labels as `key + \"=\" + value` and assert key != \"\".","Quote CLI label args so the shell doesn't split them.","Omit '=' for flag-only labels rather than emitting `=value`."],"tags":["docker","labels","validation","cli","metadata"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}