Note-to-self: short circuiting==buggy programs
Posted: Wed Jan 28, 2009 1:04 pm
Ok, well, recently I learned in my Java programming class about short circuiting.. like
if(foo() && bar()) if foo is false, then bar will never be executed because the condition is automatically false.
Well, I wondered if C did this by default or different optimization levels and such..
GCC did this by default no matter what the optimization level, as did PCC(bsd C compiler).
Now, I'm just giving everyone a warning as to this behavior. I am almost for sure somewhere in my old code of millions of bugs, I was unaware of this.
This will probably not usually be a problem, as I can't even think of a decent example of a bug made by this.
but here is my shot at it..
in this example, if say some terminal got locked and could not be shutdown or something, it would return an error. This would mean the compiler would short-circuit it. So shutdown_terminals would be executed and it would return 1. This would make the OR condition TRUE. So to the compiler, there is no need to try to sync disks because the condition is already true.
Just saying all this to maybe aid someone out there trying to find a hard bug.
if(foo() && bar()) if foo is false, then bar will never be executed because the condition is automatically false.
Well, I wondered if C did this by default or different optimization levels and such..
GCC did this by default no matter what the optimization level, as did PCC(bsd C compiler).
Now, I'm just giving everyone a warning as to this behavior. I am almost for sure somewhere in my old code of millions of bugs, I was unaware of this.
This will probably not usually be a problem, as I can't even think of a decent example of a bug made by this.
but here is my shot at it..
Code: Select all
//shutdown code
//functions return 1 on error.
if(shutdown_terminals() || sync_disks()){
kprintf("An error occured shutting down, attempting to continue\n");
}
//....
Just saying all this to maybe aid someone out there trying to find a hard bug.