How do you detect if a text file is damaged before the program crashes?

I am writing a command line program in ANSI C to parse a Quake 2 map file to report how many objects and textures are being used. My development machine is a MacBook. I am testing OS X Snow Leopard (32-bit), Windows XP (32-bit) and Vista (64-bit) and Ubuntu 9.10 (32-bit).

I had an error with Vista where the program crashed with a specific map file. It took time to understand that this is not a program, but the map file itself. I did not notice anything unusual in the text file. Reopening and saving the map file is resolved.

My code loads the entire map file into memory, uses strtok () to split the lines with "\ n", parses each line and loads the data into one list of links for processing. Is there any way to detect if the map (text) file is damaged?

The simplest non-programmable solution is to add a FAQ file with the problem and solution.

+4
source share
3 answers

I think I fixed the error. I took a few steps to get there, and the testing went well.

  • Added -Wconversion for my debugging combination for GCC. This reported some useful warnings and not very useful warnings. For the most part, adding unsigned to variable types and a few minor (int) casts.
  • Although my data structures had the correct types (i.e., unsigned long int), the output variables that added everything together were the wrong types (i.e., int). Reinstall all my variable types to make sure they all match.
  • Check is added if the file has zero or negative byte size to stop the program with an error.
  • A check was added if there were zero nodes in the data lists (i.e., parsing did not return any valid match) to stop the program with a message that the file does not have usable data.

I left only the syntax functions. If a damaged or damaged map file has the correct match, data will eventually be output. Garbage In / Garbage Out (GIGO) is still a factor. Something to revise later. The released version of my program can be found here .

0
source

When you read each line, analyze it to determine if it is true or not. If your method fails, you can simply tell the user that the data is corrupted, but you still have a graceful exit.

+2
source

With parser generator tools, you can easily spot syntax errors.

However, even if the syntax is in order, you should always consider that the contents may be out of order.

For example, if the file format is as follows:

  • n: number of records
  • record 1
  • record 2
  • ...
  • end condition

your code should not just select an array of size n and read the entries in the array to the final condition. Instead, you should make sure that n entries were actually read (in which case, never read more than n entries to avoid overflow).

Thus, create the code so that it does not blindly trust the input.

0
source

Source: https://habr.com/ru/post/1302204/


All Articles