Saher El-Neklawy A blog

Searching Source Code: LXR and grep

Looking for a certain functionality in someone else's code could prove to be a tough challenge. This is increased by the size of the given project. Many systems exist today facilitating easy search in a project. For Java, eclipse is best used for that task, as it contains a strong in java class indexing, classification, and searching.

Some projects provide for you a portal to search their code base. Famous examples of these are LXR for the Linux kernel, and it's customized version MXR used for Mozilla projects.

When all the above options are not available, the alternative exists natively in most linux systems. This is namely "grep". Grep is a strong command for regular expression matching. On if it's features is that it can also match to the content within files. Putting that in mind, the following command could be very useful:

grep -R "search query" .

This looks recursively for all mentions of search query in all files under the current directory. This prints output in the console the file name where this query was found, and the line that included it.

To be able to store this search result in a file, the output could be piped as follows:

grep -R "search query" . > log.txt

"Bug Free Zone"

As you can see from the title of my post, it's all about bug. But not bugs as in insects, but as in computer bugs/problems. In the open source development course, we were introduced to the mozilla development ecosystem.

This introduction was done via Thunderbird, the Mozilla email client. The first step was to compile a debug build for Thunderbird from source. Instructions for that could be found here. Along the process, several bumps along the way were met, but managed to get by them.

After the successful compilation, I had to start tackling theĀ  the bug famous in the open source educational community. The Thunderbird auto linkification bug. This bug is usually exists in Thunderbird, and usually mainly found under the test bugzilla.

Thunderbird Autolinkification Bug

To start on fixing that bug, a search of the source code is needed. Since Thunderbird is a Mozilla project, they offer an on-line portal for searching their code using MXR. This tool searches the content of the repository. After some searching, the best query to find the file related to this problem was "mailto". This is because the bug is related to the mail rendering component, when looking at it from purely a user's point of view.

At finding the correct file, the hacking started. Once that was done, I generated a patch of the file i changed from inside the comm-central/mozilla folder.

[caption id="attachment_41" align="aligncenter" width="288" caption="Thunderbird Bug"]Thunderbird Bug Fixed[/caption]

Once the results of my fixing looked promising, i decided to submit a bug report on the test bugzilla containing a patch. But before the submission, i decided to test my patch first on an automated reviewer. To my surprise, it showed me problems that would have never came up on my mind.

 

[caption id="attachment_42" align="aligncenter" width="450" caption="JST Review"]JST Review[/caption]

Passing the automated reviewer gives one a boost of confidence, even if it responds to you with the message of "Congratulations! I found no coding problems in your patch. That does not mean that it is ready for check in though... you still need to pass something more intelligent than a script. :-)".

Now, it was time to submit the bug, attach the patch, and request review. This was amazingly simple. Just filling a form, being very detailed, and attaching the patch file. After the submission of the bug report, I requested reviews from 2 parties, which I am still waiting for a response for.

On a final note, I found this exercise and experience very entertaining and thrilling. The rush of hacking the code, and submitting was a new feeling for me. This test experience drove me to submit an actual bug related to bespin's new refactoring code. Hopefully I get a good review on it too, and possibly a commit :D.

Converting System::String to char *

The official way to convert a visual c++ System::String to a char * could be found here. Such strings are everywhere in windows forms applications.

A simpler way (for people who are used to using standard c\c++) to do the conversion is as follows:

void string2charPtr(String ^orig, char *&out)
{
int length = orig->Length;
out = new char[length+1];
for(int i=0;i<length;i++)
out[i] = (char) orig[i];
out[length] = '';
}

Please feel free to comment on the function above.