{"record":{"id":"d1797b36447ab19e","repo":"redis/redis","slug":"memkeys-samples-value-should-be-positive","errorCode":null,"errorMessage":"--memkeys-samples value should be positive.\n","messagePattern":"--memkeys-samples value should be positive\\.\n","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/redis-cli.c","lineNumber":2893,"sourceCode":"            config.pipe_mode = 1;\n        } else if (!strcmp(argv[i],\"--pipe-timeout\") && !lastarg) {\n            config.pipe_timeout = atoi(argv[++i]);\n        } else if (!strcmp(argv[i],\"--bigkeys\")) {\n            config.bigkeys = 1;\n        } else if (!strcmp(argv[i],\"--memkeys\")) {\n            config.memkeys = 1;\n            config.memkeys_samples = -1; /* use redis default */\n        } else if (!strcmp(argv[i],\"--memkeys-samples\") && !lastarg) {\n            char *endptr;\n            config.memkeys = 1;\n            config.keystats = 1;\n            config.memkeys_samples = strtoll(argv[++i], &endptr, 10);\n            if (*endptr) {\n                fprintf(stderr, \"--memkeys-samples conversion error.\\n\");\n                exit(1);\n            }\n            if (config.memkeys_samples < 0) {\n               fprintf(stderr, \"--memkeys-samples value should be positive.\\n\");\n               exit(1);\n            }\n        } else if (!strcmp(argv[i],\"--hotkeys\")) {\n            config.hotkeys = 1;\n        } else if (!strcmp(argv[i], \"--keystats\")) {\n            config.keystats = 1;\n            config.memkeys_samples = -1; /* use redis default */\n        } else if (!strcmp(argv[i],\"--keystats-samples\") && !lastarg) {\n            char *endptr;\n            config.keystats = 1;\n            config.memkeys_samples = strtoll(argv[++i], &endptr, 10);\n            if (*endptr) {\n                fprintf(stderr, \"--keystats-samples conversion error.\\n\");\n                exit(1);\n            }\n            if (config.memkeys_samples < 0) {\n               fprintf(stderr, \"--keystats-samples value should be positive.\\n\");\n               exit(1);","sourceCodeStart":2875,"sourceCodeEnd":2911,"githubUrl":"https://github.com/redis/redis/blob/4f20cb48934463db5970bd476461b2d57af3f38d/src/redis-cli.c#L2875-L2911","documentation":"After a successful integer conversion of `--memkeys-samples`, the CLI checks `config.memkeys_samples < 0` and exits. Note 0 is permitted by this check (the upstream SAMPLES command handles it). The flag implicitly enables `--memkeys` and `--keystats` modes (lines 2885-2886).","triggerScenarios":"`--memkeys-samples -5`, or a negative value produced by a sign error in a computed variable.","commonSituations":"A negative default in a script; subtracting instead of adding when computing a sample count; copy-paste of a negative value.","solutions":["Use a non-negative integer (`0` or higher).","Omit the flag entirely to let Redis use its default samples value.","Fix the sign in the variable/expression feeding the flag."],"exampleFix":"// before\nredis-cli --memkeys --memkeys-samples -5\n// after\nredis-cli --memkeys --memkeys-samples 5","handlingStrategy":"validation","validationCode":"val=\"$MEMKEYS_SAMPLES\"\nif ! [[ \"$val\" =~ ^-?[0-9]+$ ]]; then echo \"not an integer\" >&2; exit 1; fi\nif [ \"$val\" -lt 0 ] 2>/dev/null; then echo \"--memkeys-samples must be >= 0\" >&2; exit 1; fi\nredis-cli --memkeys --memkeys-samples \"$val\"","typeGuard":null,"tryCatchPattern":"if ! redis-cli --memkeys --memkeys-samples \"$val\"; then\n  echo \"memkeys-samples range check failed\" >&2; exit 1\nfi","preventionTips":["Clamp negative computed values to 0 before passing.","Default to omitting the flag to use the server's default samples."],"tags":["redis-cli","memkeys","cli-args","validation","integer","range"],"backgroundTag":null,"analyzedSha":"4f20cb48934463db5970bd476461b2d57af3f38d","analyzedAt":"2026-08-10T14:17:07.821Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}