The MD5 checksum bug identified.

Happily it’s easy to fix and exactly what I thought it was. When retrieving values from an NSManaged object like so:

NSString *foo = recipe.value;

or

NSString *foo = [recipe valueForKey:@"value"];

If the value is not present you get a nil. Messages to nil go nowhere but do not crash, so if value should have been an NSString the following is OK:

[foo isEqualToString:@"bar"]

However if the object you are retrieving from is an NSDictionary you will get an NSNull. NSNull is a class and a valid object so sending it messages it does not understand will fail:

NSString *foo = [dictionary valueForKey:@"value"];
[foo isEqualToString:@"bar"]; // CRASH

Seeing as I need to compute the md5 sum for a dictionary based recipe and an managed object based one and the sums need to be the same the code that produces the string to hash must produce identical results. In the case of a null value the code to produce the key to hash from the recipe managed object inserted this:

(null)

The code to produce it from the dictionary simply crashed. So I changed it in both the dictionary and the managed object routines to check:

if ([object isEqualToClass:[NSNull class]]) {
object = @"";
}

Which meant the keys changed and I didn’t update the initial data set to reflect that as I didn’t run OCUnit and thus didn’t get bashed upside the head that something had changed.

Always run your unit tests folks.

This entry was posted in Chef's Book, Programming and tagged , , , , . Bookmark the permalink.