Recently I had an issue running this server’s Git server, a super simple front-end for displaying git output as HTML. After installing gdb
on the server, I got the following signal:
0001 Program received signal SIGFPE, Arithmetic exception
That’s weird. After reading the output, there was an issue with the following code:
0002 /* Initialise the server */ 0003 printf("Starting server on port %i\n", port); 0004 serv s = {}; 0005 serv_init(&s, port); 0006 /* Pick-up some clients */ 0007 unsigned int x = 0; 0008 int pid = getpid(); 0009 while(pid == getpid()){ 0010 serv_update(&s, process); 0011 /* For each client, take it in turns to check a repo on main thread */ 0012 update_repo(x++ % repos_n); // <-- Problem here 0013 } 0014 /* Return nicely */ 0015 return 0;
After spending a moment checking update_repo()
, it occurred to me what the issue is. When testing the program I always tested it with repos_n > 0
. In Python we can see a similar issue:
0016 x = 3 # This number doesn't matter so much 0017 n = 1 # Well tested divisor 0018 print(str(x) + " / " + str(n) + " = ", end="") 0019 print(str(x / n)) 0020 n = 0 # Untested failure divisor 0021 print(str(x) + " / " + str(n) + " = ", end="") 0022 print(str(x / n))
3 / 1 = 3.0 3 / 0 = Traceback (most recent call last): File "/home/user/website/highlight.py", line 289, in process exec(command, exec_globals) File "<string>", line 7, in <module> ZeroDivisionError: division by zero
Gah, that was dumb. Note to self, remember all of the edge cases!
All we need is a simple check for the zero case:
0023 if(repos_n > 0) update_repo(x++ % repos_n);