Found an Interesting Bug in the PList Parsing in CKit

CKit.jpg

I came in today to see a problem in one of my apps that uses the plist parsing in CKit for preferences - much like the old Mac OS X plist files. I did this because I really liked them, and they afforded me a great deal of flexibility in the setting of the parameters for this application. But there was a problem. I hadn't found it because up to this point I edited the file and then reloaded the file into the app. But if the app wrote out the file and then tried to reload it, there was a problem.

The problem today was that I looked at the plist file and it had clearly been changed. Yet they probably hadn't changed it. What probably happened was that the app itself had written it out and then failed to read it back in properly. There was another interesting complication in the works that lead me to believe that it was the writing part and that was a feature I had put into the code from the beginning.

If I had a section of the plist that looked like this:

    backup = {
        interval_mins = 5;
        filename = "../logs/<hostname>/backup.dat";
    }

then the same could would write out:

    backup = {
        filename = ../logs/<hostname>/backup.dat; interval_mins = 5;
    }

and there's a good reason for it. First, the quotation marks are gone because it's obviously a string and there's nothing that needs escaping in the string. Secondly, for maps with less than three elements, I decided to place them all on a line so that things like points and lines are more easily visualized. What happened then was the bug.

The bug was in the reading of the converted plist. Specifically, it wasn't looking at the semi-colon as the terminator for the value on the first key/value pair. In fact, it was only looking at the line-endings as terminations and that was meaning that the filename had the semi-colon as well as the second key/value pair. That's not going to work.

The solution was simple - recognize that the semi-colon was the terminator and everything worked perfectly. Simple fix, but finding it took longer than I expected.