From 908b8ca0b3bf2af143a19d94814c1882b9165b8c Mon Sep 17 00:00:00 2001 From: Roman D Date: Mon, 21 Aug 2023 22:24:13 +0200 Subject: [PATCH] Limit configurable CPU count to the number of detected CPU cores. --- client/src/proxmark3.c | 5 +++-- client/src/util.c | 6 +++++- client/src/util.h | 3 ++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/client/src/proxmark3.c b/client/src/proxmark3.c index 26f4a5698..3e924a1c7 100644 --- a/client/src/proxmark3.c +++ b/client/src/proxmark3.c @@ -1004,8 +1004,9 @@ int main(int argc, char *argv[]) { return 1; } long int ncpus = strtol(argv[i + 1], NULL, 10); - if (ncpus < 0 || ncpus >= INT_MAX) { - PrintAndLogEx(ERR, _RED_("ERROR:") " invalid number of CPU cores: --ncpu " _YELLOW_("%s") "\n", argv[i + 1]); + const int detected_cpus = detect_num_CPUs(); + if (ncpus < 0 || ncpus >= detected_cpus) { + PrintAndLogEx(ERR, _RED_("ERROR:") " invalid number of CPU cores: --ncpu " _YELLOW_("%s") " (available: %d)\n", argv[i + 1], detected_cpus); return 1; } g_numCPUs = ncpus; diff --git a/client/src/util.c b/client/src/util.c index 150b042a5..f1e58abe0 100644 --- a/client/src/util.c +++ b/client/src/util.c @@ -1079,12 +1079,16 @@ uint64_t HornerScheme(uint64_t num, uint64_t divider, uint64_t factor) { return result; } -// determine number of logical CPU cores (use for multithreaded functions) int num_CPUs(void) { if (g_numCPUs > 0) { return g_numCPUs; } + return detect_num_CPUs(); +} + +// determine number of logical CPU cores (use for multithreaded functions) +int detect_num_CPUs(void) { #if defined(_WIN32) #include SYSTEM_INFO sysinfo; diff --git a/client/src/util.h b/client/src/util.h index 0712d8d18..fa636ce9b 100644 --- a/client/src/util.h +++ b/client/src/util.h @@ -131,7 +131,8 @@ void wiegand_add_parity_swapped(uint8_t *target, uint8_t *source, uint8_t length uint32_t PackBits(uint8_t start, uint8_t len, const uint8_t *bits); uint64_t HornerScheme(uint64_t num, uint64_t divider, uint64_t factor); -int num_CPUs(void); // number of logical CPUs +int num_CPUs(void); +int detect_num_CPUs(void); // number of logical CPUs void str_lower(char *s); // converts string to lower case void str_upper(char *s); // converts string to UPPER case