Skip to content

Instantly share code, notes, and snippets.

@uroboro
Created December 5, 2017 14:47
Show Gist options
  • Save uroboro/f609020adf4f3feb9cdc543fbbeb4368 to your computer and use it in GitHub Desktop.
Save uroboro/f609020adf4f3feb9cdc543fbbeb4368 to your computer and use it in GitHub Desktop.
Always check user input.

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.

Line 12:

if (argv[2]) threads = atoi(argv[2]);

Line 18:

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.

Line 23:

for (int increment = 0; increment < threads; increment++) {

This loop will run UINT_MAX times.

Line 28:

while (counter < execPer) counter++;

Will never run since counter is equal to execPer, so counter will still be 0.

Line 34:

if (numberOfExecutions == threads) {

This will be equal when the loop has ran UINT_MAX times.

Line 36:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment