Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle Coverity error TAINTED_SCALAR in fread

Tags:

fread

coverity

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.

like image 507
coder Avatar asked Jul 16 '14 04:07

coder


1 Answers

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.

like image 176
Mark Robinson Avatar answered Oct 30 '22 08:10

Mark Robinson