Stupid Crash Bug

I spent a few hours debugging this line of code. It was crashing, and I was debugging it at the assembly level assuming it was a SIGSEGV. It was so confused I began to think it was a code generation error.

int r = rand() % gpGlobals->maxClients;

Of course, everything looked fine. Finally, I realized it wasn’t a SIGSEGV, but a divide by zero. Modulus operator is an “IDIV” instruction, and when there are zero players, this will crash.

Take two lessons from this: always read the error messages, and if you’re making mistakes like this, it’s time to stop coding for a few hours.

Disassembly series will return on Wednesday.

Leave a Reply

You must be logged in to post a comment.