|
|
|
|
|
by adrian_b
1536 days ago
|
|
That code is indeed wrong, but the error consists in using "long" instead of "unsigned long". The sizes of "unsigned long" and of "long" are unknown, but they are at least 32 bit. Had "unsigned long" been used, modular arithmetic without overflow would have been used, which was the intention of the code. Of course the condition of the test must also be changed for an "unsigned long". As it is written here, it is completely wrong. If this program would be compiled with the correct "-fsanitize=undefined -fsanitize-undefined-trap-on-error" options, which should be always used for any C/C++ program, unless there are good reasons to do otherwise, then the program will be aborted at one of the the first invocations of the function. |
|