First thing, line 14:
countOnThreads(atoi(argv[1]), threads, ^(CFTimeInterval seconds, unsigned long long check, unsigned int threads) {
atoi(argv[1])
is left unchecked allowing unexpected input like negative numbers which would be interpreted as a big positive number once passed to countOnThreads
.
Then, passing "-1" as the second argument to main() makes atoi() return -1 which is then converted to UINT_MAX. Assuming that the first argument is valid and positive, lets continue.
if (argv[2]) threads = atoi(argv[2]);
unsigned const long long execPer = max/threads;
Both max
and threads
are integer types, so if threads
is bigger than max
, execPer
will be 0.
Here is where it gets nasty.
for (int increment = 0; increment < threads; increment++) {
This loop will run UINT_MAX
times.
while (counter < execPer) counter++;
Will never run since counter
is equal to execPer
, so counter
will still be 0.
if (numberOfExecutions == threads) {
This will be equal when the loop has ran UINT_MAX
times.
unsigned long long fixMod = execPer*threads;
while (fixMod < max) fixMod++;
fixMod
will be 0 because 0 * UINT_MAX
is 0. The loop will run max
times.
So, the for loop will run UINT_MAX
times, the first while loop will never run and the last while loop will run max
times.