How to handle Coverity error TAINTED_SCALAR in fread

coder picture coder · Jul 16, 2014 · Viewed 14.9k times · Source

While reading a value from file for an integer, coverity check is giving following error

Calling function "fread" taints argument "readval"

//coverity note: Calling function "fread" taints argument "readval".
if(fread(&readval, sizeof(int), 1, fp) < 1) {
    return;
} else {
    //coverity note: Passing tainted variable "readval" to a tainted sink.
    f1(&readval);
}

How to handle this error? What sanity checks I need to perform for 'readval' to ensure it is not corrupt.

Answer

Mark Robinson picture Mark Robinson · Jul 21, 2014

So the problem is that you're using a tainted value ;)

In more detail, readval is set once by outside data and then potentially used as an argument to fseek. This argument could put you past the end of the file and cause your program to crash.

You need to put in some checks to make sure you aren't walking off the end of the file.