Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Read a line with max length ?

by Zaxo (Archbishop)
on Feb 27, 2003 at 11:43 UTC ( #239069=note: print w/replies, xml ) Need Help??

in reply to Read a line with max length ?

Be careful with local $/ = \$maxlength;. That changes what the diamond operator thinks of as a line. If the input stream contains "foo\nbar\nbaz", then the line read will have as many "\n" as $maxlength will contain.

You may want to keep the default record seperator and limit length with something like this:

while (<FILEHANDLE>) { $_ = substr $_, 0, $maxlength if length > $maxlength; # ... }

Update: graff is right that this does not avoid problems with extra-long lines. Buffer overflow should not be a problem on most OS's, but forcing the machine into swap and OOM could be an attack.

After Compline,

Replies are listed 'Best First'.
Re: Re: Read a line with max length ?
by graff (Chancellor) on Feb 27, 2003 at 22:44 UTC
    At the point where this statement executes, with $/ having any sort of string value (including the default line terminator):
    while (<SOCKETHANDLE>) { ...
    I think the potential damage will already be done, if the process at the other end of the socket happens to write, say, 4 GB of data with nothing that matches $/.

    (Then again, I could be wrong about that, 'cuz I haven't tested it... does the <> mechanism provide some sort of safe buffering or allocation method to avoid stuffing an impossible amount of data into $_? If so, this seems magical and quite unexpected.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://239069]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (12)
As of 2020-01-29 16:52 GMT
Find Nodes?
    Voting Booth?