Posts Tagged ‘Horrible Coding Practices’

You.Can’t.Parse.HTML.with.Regex

November 25, 2009

Well, I’ve been away for ages haven’t I? I’ve been a little busy, and truth be told, the well of inspiration has been a little dry for a while now. Anyhoo, I decided that I needed to *just start* writing again and the ideas will begin to flow. Today’s entry is about humour. In fact, it is downright hilarious.

In programming circles, parsing HTML with Regular Expressions is considered a bad idea. “Exactly how bad?”, you ask. Bad enough that one user on Stack Overflow says it’ll bring about the end of the world. Or something like that. You can’t really make sense of the crazy in the later bits!

“You can’t parse [X]HTML with regex. Because HTML can’t be parsed by regex. Regex is not a tool that can be used to correctly parse HTML. As I have answered in HTML-and-regex questions here so many times before, the use of regex will not allow you to consume HTML. Regular expressions are a tool that is insufficiently sophisticated to understand the constructs employed by HTML. HTML is not a regular language and hence cannot be parsed by regular expressions. Regex queries are not equipped to break down HTML into its meaningful parts. so many times but it is not getting to me. Even enhanced irregular regular expressions as used by Perl are not up to the task of parsing HTML. You will never make me crack. HTML is a language of sufficient complexity that it cannot be parsed by regular expressions. Even Jon Skeet cannot parse HTML using regular expressions. Every time you attempt to parse HTML with regular expressions, the unholy child weeps the blood of virgins, and Russian hackers pwn your webapp. Parsing HTML with regex summons tainted souls into the realm of the living. HTML and regex go together like love, marriage, and ritual infanticide. The <center> cannot hold it is too late. The force of regex and HTML together in the same conceptual space will destroy your mind like so much watery putty. If you parse HTML with regex you are giving in to Them and their blasphemous ways which doom us all to inhuman toil for the One whose Name cannot be expressed in the Basic Multilingual Plane, he comes. HTML-plus-regexp will liquify the n​erves of the sentient whilst you observe, your psyche withering in the onslaught of horror. Rege̿̔̉x-based HTML parsers are the cancer that is killing StackOverflow it is too late it is too late we cannot be saved the trangession of a chi͡ld ensures regex will consume all living tissue (except for HTML which it cannot, as previously prophesied) dear lord help us how can anyone survive this scourge using regex to parse HTML has doomed humanity to an eternity of dread torture and security holes using regex as a tool to process HTML establishes a breach between this world and the dread realm of c͒ͪo͛ͫrrupt entities (like SGML entities, but more corrupt) a mere glimpse of the world of reg​ex parsers for HTML will ins tantly transport a programmer’s consciousness into a world of ceaseless screaming, he comes, the pestilent slithy regex-infection wil​l devour your HT ML parser, application and existence for all time like Visual Basic only worse he comes he comes do not fi ght he com̡e̶s, ̕h̵i s un̨ho͞ly radiańcé destro҉ying all enli̍̈́̂̈́ghtenment, HTML tags lea͠ki̧n͘g fr̶ǫm ̡yo​͟ur eye͢s̸ ̛l̕ik͏e liq uid pain, the song of re̸gular exp​ression parsing will exti nguish the voices of mor​tal man from the sp here I can see it can you see ̲͚̖͔̙î̩́t̲͎̩̱͔́̋̀ it is beautiful t he final snuffing of the lie​s of Man ALL IS LOŚ͖̩͇̗̪̏̈́T ALL I​S LOST the pon̷y he comes he c̶̮omes he comes the ich or permeates all MY FACE MY FACE ᵒh god no NO NOO̼O​O NΘ stop the an​*̶͑̾̾​̅ͫ͏̙̤g͇̫͛͆̾ͫ̑͆l͖͉̗̩̳̟̍ͫͥͨe̠̅s ͎a̧͈͖r̽̾̈́͒͑e n ot rè̑ͧ̌aͨl̘̝̙̃ͤ͂̾̆ ZA̡͊͠͝LGΌ ISͮ̂҉̯͈͕̹̘̱ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ”

The rant had me literally rolling on the ground convulsing in laughter. Good Stuff. Thank you Jeff Atwood, for showing me this rant!

P.S: I have some posts lined up, so don’t go deleting me from your RSS just yet :). From now on, I’ll try to keep it one post per week!

Advertisements

The WORST bit of code I ever wrote….

January 29, 2008

Over the years, I have written quite a bit of code. Not mind boggling or anything, but a bit more than most other people I reckon. And looking back at it all, I’ve written some truly crappy stuff. I laugh at it, because I like to think I “know” stuff now. But today I was brought crashing down to earth.

Here is what I wrote:

/*….some code that initialised stuff…*/
Major=register_chrdev(253,”abc” , &fops);
if(Major < 0)
{
printk(“Registering failed !!\n”);
return Major;
}
printk(“Reading cruel world!!\n”);
return 0;
handle_list_head = (struct alloc_handle_list *)kmalloc (sizeof(struct alloc_handle_list), GFP_KERNEL);
if(handle_list_head==NULL)
{
printk(“Unable to allocate head”);
return 1;
}
INIT_LIST_HEAD(&handle_list_head->ptr);
if(handle_list_head->ptr.prev==NULL || handle_list_head->ptr.next==NULL)
/*….other init code… initialisation module ends…*/
/*…………………………………………….*/

/*……some other function….*/
list_for_each_safe(temp_list, temp_for_temp_list, &handle_list_head->ptr)
{
fun = list_entry(temp_list, struct alloc_handle_list, ptr);
if(fun->handle != handle) continue;
co_passage_page_assert_valid();
co_passage_page_acquire(&flags);
co_passage_page->operation=CO_OPERATION_UNREGISTER_HANDLE;
co_passage_page->params[0]=fun->handle;
co_switch_wrapper();
if(co_passage_page->params[0]==0)
{
co_passage_page_release(flags);
printk(“Did not unreg Handle (no such handle)!!\n”);
return -1;
}
co_passage_page_release(flags);
/*……….and some more…..*/

It isn’t important to know what this does, only that we were at a crucial point in the general proceedings. See if you can locate the flaw. It took poor Jitesh 4 hours to debug this little baby. Every time we executed the code, it would give us a Segmentation Fault. The kernel stack dump would tell us that the module was “Unable to deference pointer to kernel memory at virtual address…”.

We went through every detail of the pointer manipulation. We resorted to commenting the code line by line to figure out the offending snippet. Finally, unable to take it anymore, I called it a day and headed home. Jitesh, being the stubborn kind, struggled on. When he found the error, he called me up to dish out a few choice words and home truths. Here it is…

/*…in the initialisation function…*/
if(Major < 0)
{
printk(“Registering failed !!\n”);
return Major;
}
printk(“Reading cruel world!!\n”);
return 0;
handle_list_head = (struct alloc_handle_list *)kmalloc (sizeof(struct alloc_handle_list), GFP_KERNEL);
/*…continues…*/

As you can see, the head of our list structure was NEVER BEING INITIALIZED!!! This, my friends, is the absolute worst bit of code I have ever typed. And my punishment for doing this is to write truthfully about it in this post! Hopefully, this will remain the worst bit of code I ever write, and I won’t have to write another post like this ever!