Reputation: 115
I'm writing a command line program in ANSI C to parse a Quake 2 map file to report how many entities and textures are being used. My development machine is MacBook. I'm testing on OS X Snow Leopard (32-bit), Windows XP (32-bit) and Vista (64-bit), and Ubuntu 9.10 (32-bit).
I had a crashed bug on Vista where the program would hanged with a certain map file. Took a while to figure out that it wasn't the program but the map file itself. I didn't noticed anything unusual about the text file. Re-opening and saving the map file fixed that issue.
My code loads the entire map file into memory, uses strtok() to separate the lines using '\n', parses each line, and loads the data into a single-link list for processing. Is there a way to detect if the map (text) file is corrupt?
The easiest non-programming solution is to add a FAQ file with the problem and solution.
Upvotes: 4
Views: 2468
Reputation: 115
I think I fixed the bug. I took a number of steps to get there and testing went fine.
I left the parsing functions alone for now. If a corrupt or mangled map file has a valid match, that "data" will eventually be outputted. Garbage In/Garbage Out (GIGO) is still a factor. Something to revisit later. The released version of my program can be found here.
Upvotes: 0
Reputation: 528
With parser generator tools, you can detect syntactical errors easily.
However, even if the syntax is ok, you should always assume that the contents might not be ok.
For example, if the file format is as follows:
your code should not just allocate n sized array and read the entries into the array until the end condition. Instead, you should verify that n entries were actually read (and in this case, never read more than n entries to avoid overflow).
Thus, design the code so that it does not blindly trust the input.
Upvotes: 0
Reputation: 876
As you read each line parse it, to determine whether it is valid or not. If your method fails, you can simply let the user know that the data is corrupt, yet you still have a graceful exit.
Upvotes: 2