What am I getting at here? The main reason I'm posting this is because I recently saw some code which reads in data from a file using fgets(). This code then checks for the first character to see if its a comment character ('#'). If it is it ignores that line and reads the next line. However, the code later looks a little something like this:
buff[strlen(buff)-1] = '\0';
If the first character of the line was a NULL byte the strlen() would return 0 as the first char marks the end of the data. This obviously leads to a simple off-by-one issue.
A more correct version of this would look something like this:
size_t len = strlen(buff);
fgets()
...
if(!len)
return ERROR;
else
buff[len - 1] = '\0';
Of course this is just a simple example of some of the dangers of reading in binary data and treating it as a character array.