Home | Techie Talk | Story Time | About | Contact

Thursday, May 17, 2007

A simple mistake can take your whole day, away!!!


 
 
Today I had to implement a small task related to Windows Active Directory in our product. The base for this had already been implemented in C language, few months ago. Just a wrapper code was to be written in Java, today.

The input for this would be 1. Domain Name, 2. login Name 3. Password.

Everything worked fine if all these parameters were passed as individual texts - i,e. login name without the domain prefix

Its normal for few Windows users to enter the login name with the domain prefix i,e. something like "DomainName\LoginName"

So to handle such cases, I wanted to
1. check if the character '\' is present in the login name,
2. If present, split the string and take the login name alone

This is what I used from Java for step (2)

//*** Assume oldLoginName = domainName\loginName
int i = oldLoginName.indexof('\');
//*** this should be '\\'. Using '\' for better understanding

int l = oldLoginName.length();
String newLoginName = oldLoginName.substring(i,l);


and then pass the newLoginName as the parameter to the native implementation.

Bhooooom!!!! Nothing happened as expected. When I looked at the logs, the input paremeters were right.

So I assumed the problem should somewhere be in the C implentation - especially while converting the Java String to C String.

Analyzed the C code that was written several months ago - Nothing helped!
Browsed through several sites to find if Java to C conversion of Strings had any problem - even this didn't help!!
Starred at my computer monitor for sometime, as if it would obey my words - This toooo didnt HELP!!!

Again thinking the problem should be in conversion, printed the length of C String in the log files.

This time A BIG BHOOOOOOOM!!!

image from www.nowhereland.it

The length was one more than the actual length of login name, that was expected.

Now I got where the problem was. The substring(...) method in Java would return a string extrated from the parent string - from the start Index to the End Index. My falut is that I had passed the index of '\' as the start index which means that character will also be present in the new extracted string.

All I wanted was "loginName" but by a very simple mistake i had extracted "\loginName"

Then why didn't the debug messages in the log show me this???

Very simple - '\' is an escape sequence and the hence the log file had skipped it while displaying.

I love the challenges in the software profession but never thought so much of efforts would be required to understand just two lines of code.

Good learning - alas a very costly one!!!

Labels: ,

0 Comments:

Post a Comment

<< Home