Fread() error

software development

#1

Ok, I’ve got an ugly PHP bug to squash. Take a look here

The error is: Warning: fread(): Length parameter must be greater than 0. in /home/.paddle/scrpbk/flavorandflair.com/calendar/ffdb.inc.php on line 2881

The line(s) in question is:
function read_str($fp)
{
$strlen = $this->bin2dec(fread($fp, 4), 4);
return fread($fp, $strlen);
}

Can anyone help me?


#2

[quote]The line(s) in question is:
function read_str($fp)
{
$strlen = $this->bin2dec(fread($fp, 4), 4);
return fread($fp, $strlen);
}[/quote]
So let me get this straight, you read 4 bytes as the length of a string into $strlen, and then read $strlen bytes from the file.

So what happens if the 4 bytes indicate the string is zero-length? Apparently you can’t read 0 bytes from a file using fread()

:cool: Perl / MySQL / HTML+CSS


#3

Atropos7,

Shall I change the 4 to something else? I’m not sure how to address this…

Your help is so totally appreciated!

:slight_smile: Elite

[color=#6600CC]Elite Bautista[/color]
designer/developer
v: 310.922.4865
f: 310.943.1758
e: design@gomoxie.net


#4

No, thats not the problem. Here is what the code does in English™:

read four bytes from a file
convert the four bytes to a decimal number
read this number of bytes from the file

The problem is with decimal number. You are not allowed to read zero bytes from a file. Well, the code does not check to see if the decimal number is zero (or less).

It needs to be something like:

function read_str($fp) { $strlen = $this->bin2dec(fread($fp, 4), 4); if ($strlen > 0) { return fread($fp, $strlen); } return ''; }Though if I were you, I’d contact the programmer(s) and make them aware of this bug in their code as well. Its possible that the file could also be corrupted if there are zero-length strings and the programmers assumed there never would be, etc.

Also, what happens if $strlen is 32, and there are only 20 bytes left in the file? Where is that error caught?

:cool: Perl / MySQL / HTML+CSS


#5

Atropos7,

Thanks for the edits! Worked like a charm. I’ve also e-mailed the programmer.

:slight_smile: Elite

[color=#6600CC]Elite Bautista[/color]
designer/developer
v: 310.922.4865
f: 310.943.1758
e: design@gomoxie.net