I love errors

Hello all,

The best thing I have learnt from my computer engineering courses is that if your program is giving errors then you are on the verge of learning something new. Some of the programmers even say that if your code is compiled without errors in first try then something is wrong(:D obviously we are talking here about at least moderate complex codes; some people may want to boast that there ‘hello world’ code compiled successfully in first attempt and nothing was wrong *le ‘what to say on this’ face*).

The motto of the post is try to write code without errors but don’t get disheartened if you find you have written some buggy piece of code. Instead try to find why the error came. Now I am going to discuss one of the coolest possible errors I encountered with. Let me show it – (screenshot taken at fosslab, College of engineering, Pune).

I had used perror(“open failed”); to display the error message if any; as all of us know perror displays custom error message first – in this case ‘open failed’ and then displays the error message corresponding to the errno returned. Now as it is printing ‘Success’ it means file did opened successfully and return value value of the open system call was not -1. Then where did things go wrong? Lets see what code I wrote:

We are interested only in the exception handling code snippet so I am posting only that much.

if((fd = open(“temp.txt”, O_WRONLY|O_CREAT|O_TRUNC, 0666)>1)) {
perror(“open failed\n”);
I am sure some of you must have got the error easily and those who haven’t mates this is a good example of how getting code compiled successfully is not end of the story. First error is that as it is exception code we should check with ‘< 1’. But the error which is causing this hillarious thing is in positions of parentheses. As assignment operator is right associative the checking of return value of open > 1 happens first (NOTE this). As open is success it is true if gets value 1 and as a side effect fd is assigned value 1(NOTE this as well). As there is no syntax error in this code in the block gets executed printing my custom message. But as open has returned a non-zero value it prints ‘: Success’ through errno. Fun isn’t it?

The correct if condition:

if((fd = open(“temp.txt”, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 1)

This time fd is assigned first and then checked with >1. So ‘if block’ functions as intended.

Now why end here – I have some more goods for you in this shipment 😉 Lets say in our erroneous snippet we check < 1 instead of > 1.

i.e. try if((fd = open(“temp.txt”, O_WRONLY|O_CREAT|O_TRUNC, 0666) < 1))

Then if gets value of 0 and perror isn’t executed. Then we have got rid of our problem, have we? Try this out and tell me what happened. If you can explain it even better or else I will post my results with explanation in comments in few days. (In this case things turn out even more interesting!!)

So moral of the story is title of this post – don’t be afraid to write erroneous codes(not as your assignments of course :P)

– Payas


One response to “I love errors

  1. Hmm guess no one wants to share their findings… Alright.
    For the second case as condition returns 1 to fd output is printed to stdout instead of file since fd=1 corresponds to stdout.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s