Bug 2608 - Slim/Utils/Unicode.pm:encodingFromFileHandle: seek() assumed to return file position
: Slim/Utils/Unicode.pm:encodingFromFileHandle: seek() assumed to return file p...
Status: RESOLVED FIXED
Product: Logitech Media Server
Classification: Unclassified
Component: Misc
: 6.2.1
: All All
: P4 minor (vote)
: ---
Assigned To: Dan Sully
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2005-11-21 10:34 UTC by Pete
Modified: 2008-09-15 14:39 UTC (History)
1 user (show)

See Also:
Category: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Pete 2005-11-21 10:34:48 UTC
Code reads:
# Save the old position (if any)
# And find the file size.
#
# These must be seek() and not sysseek(), as File::BOM uses seek(),
# and they'll get confused otherwise.
my $pos  = tell($fh);
my $size = seek($fh, 0, SEEK_END);

Which is wrong, or at least very misleading, since seek() returns 1 on success, 0 on failure. (sysseek() on the other hand returns the new position!) Even though it is in special cases possible to guess the encoding using at most one byte, the guess will generally be incorrect.

Last line should be changed to something along the lines of:
my $size = seek($fh, 0, SEEK_END) ? tell($fh) : 4096;

An arbitrary value has been chosen in case seek() fails. For correctness, the error from seek() should be returned instead, and errors from tell() should be handled.

Also, is there any reason not to enforce a largest value on $size? ($size = 4096 if ( $size > 4096 ||�$size < 0 );) Trying to read a huge file in one read() might not be so good, and all we're trying to do is _guess_ the encoding.
Comment 1 Dan Sully 2005-11-21 11:40:09 UTC
Thanks - fixed in change 5271.