{"record":{"id":"cdf9fd3103a48650","repo":"netdata/netdata","slug":"number-of-threads-must-be-a-positive-integer","errorCode":null,"errorMessage":"Number of threads must be a positive integer.\n","messagePattern":"Number of threads must be a positive integer\\.\n","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/collectors/apps.plugin/busy_threads.c","lineNumber":47,"sourceCode":"    keep_running = 0;\n}\n\nvoid *busy_loop(void *arg) {\n    while (keep_running) {\n        // Busy loop to keep CPU at 100%\n    }\n    return NULL;\n}\n\nint main(int argc, char *argv[]) {\n    if (argc != 2) {\n        fprintf(stderr, \"Usage: %s <number of threads>\\n\", argv[0]);\n        exit(EXIT_FAILURE);\n    }\n\n    int num_threads = atoi(argv[1]);\n    if (num_threads <= 0) {\n        fprintf(stderr, \"Number of threads must be a positive integer.\\n\");\n        exit(EXIT_FAILURE);\n    }\n\n    // Register the signal handler to gracefully exit on Ctrl-C\n    signal(SIGINT, handle_signal);\n\n    pthread_t *threads = malloc(sizeof(pthread_t) * num_threads);\n    if (threads == NULL) {\n        perror(\"malloc\");\n        exit(EXIT_FAILURE);\n    }\n\n    // Create threads\n    for (int i = 0; i < num_threads; i++) {\n        if (pthread_create(&threads[i], NULL, busy_loop, NULL) != 0) {\n            perror(\"pthread_create\");\n            free(threads);\n            exit(EXIT_FAILURE);","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/netdata/netdata/blob/4864de85e26f6734d92cfc27ccbeff5921f49938/src/collectors/apps.plugin/busy_threads.c#L29-L65","documentation":"Validation error from busy_threads (src/collectors/apps.plugin/busy_threads.c:47): the single argument parsed with atoi() must be a positive integer. When num_threads <= 0 (non-numeric input that atoi() maps to 0, zero, or a negative number), the tool prints this message and exits with EXIT_FAILURE before creating any threads.","triggerScenarios":"Running './busy_threads abc' (atoi returns 0), './busy_threads 0', './busy_threads -2', or a value that overflows int so atoi wraps to a non-positive result.","commonSituations":"Shell scripts passing an empty or unset variable ('$THREADS' when THREADS is empty becomes argument '0' or missing); copy/paste with a stray character; automated harness supplying a non-numeric parameter.","solutions":["Pass a positive integer: './busy_threads 4'.","In scripts, default and validate first: 'THREADS=${THREADS:-2}; case $THREADS in (*[!0-9]*|'') echo bad;; esac'.","Keep the value within int range to avoid atoi overflow wrap-around."],"exampleFix":"# before\n$ ./busy_threads \"$THREADS\"   # THREADS unset -> '' -> 0\n# after\n$ THREADS=${THREADS:-2}; ./busy_threads \"$THREADS\"","handlingStrategy":"validation","validationCode":"# bash: reject empty/non-numeric/non-positive values\nn=${1:-}; [[ $n =~ ^[1-9][0-9]*$ ]] || { echo \"thread count must be a positive integer\" >&2; exit 1; }\n./busy_threads \"$n\"","typeGuard":"/* C: strict positive-int check unlike the tool's atoi */\nstatic bool positive_int(const char *s, long *out) {\n    char *end; long v = strtol(s, &end, 10);\n    if (end == s || *end != '\\0' || v <= 0 || v > INT_MAX) return false;\n    *out = v; return true;\n}","tryCatchPattern":null,"preventionTips":["Never pass unset shell variables: default them first.","Note atoi() silently maps garbage to 0 — validate before invoking."],"tags":["c","test-tool","validation","busy-threads","netdata"],"backgroundTag":null,"analyzedSha":"4864de85e26f6734d92cfc27ccbeff5921f49938","analyzedAt":"2026-08-15T09:12:38.226Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}