Saturday, May 29, 2010

Perfect crime

So, there's a tiny logic bug in someone's code that you need to fix but the code has changed quite significantly and you don't want to have to go back through subversion logs?

  1. Download the existing dll from the production server
  2. Disassmble the dll using ildasm (ildasm /out:code.msil code.dll)
  3. Use notepad to insert the fix you need in MSIL. Ildasm conveniently pads line numbers in the disassembled code so if you're lucky you can insert code without having to change any existing line number references.
  4. Reassemble the code and deploy (ilasm /DLL code.msil)
  5. Try not to act surprised that all those years of writing compiler and reflection.emit code came in handy yet again!
  6. Hope no-one notices.

// Original C#

ViewData["CODE"] = string.IsNullOrEmpty(id)
? GetCookieValue("CODE") : id.ToUpper();

// Original IL

IL_001b: ldarg.0
IL_001c: ldstr "InviteCode"
IL_0021: call instance string GetCookieValue(string)
IL_0022: dup
IL_0023: starg 1
IL_0026: callvirt instance void set_Item(string, object)

// Fixed C#

id = string.IsNullOrEmpty(id)
? GetCookieValue("CODE") : id.ToUpper();

ViewData["CODE"] = id;

// Fixed C# (more compact)

ViewData["CODE"] = string.IsNullOrEmpty(id)
? id = GetCookieValue("CODE") : id.ToUpper();

// Fixed IL

IL_001b: ldarg.0
IL_001c: ldstr "InviteCode"
IL_0021: call instance string GetCookieValue(string)
IL_0022: dup
IL_0023: starg 1
IL_0026: callvirt instance void set_Item(string, object)

1 Comments:

At June 22, 2010 at 2:23 PM , Blogger Annie Luxton said...

Oohhh I noticed!

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home