# Controlling readline

**URL:** <https://www.ruby-forum.com/t/controlling-readline/75665>\
**Category:** Ruby\
**Created:** [November 12, 2006, 3:09am UTC](https://www.ruby-forum.com/t/controlling-readline/75665 "2006-11-12T03:09:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luo\_Yong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@Luo\_Yong](https://www.ruby-forum.com/u/Luo_Yong)\
**Post date:** [November 12, 2006, 3:09am UTC](https://www.ruby-forum.com/t/controlling-readline/75665/1 "2006-11-12T03:09:07Z")

</div>

Hi all

Is there anyway to control the maxium length of the IO.readline can  
read?

Thanks

---

<div class="post-metadata">

**Author:** ![Luo\_Yong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@Luo\_Yong](https://www.ruby-forum.com/u/Luo_Yong)\
**Post date:** [November 12, 2006, 7:17am UTC](https://www.ruby-forum.com/t/controlling-readline/75665/2 "2006-11-12T07:17:21Z")

</div>

Luo Yong wrote:

> Hi all
> 
> Is there anyway to control the maxium length of the IO.readline can read?

Not really, not from the command line. You can throw away all the length  
you  
don’t want once it has been entered. It is way better to expect more  
characters than you get than to get more than you expect (long sad story  
about C/C++ “gets” snipped here).

Maybe you should describe the problem, rather than asking about this way  
of  
solving it.

---

<div class="post-metadata">

**Author:** ![Luo\_Yong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@Luo\_Yong](https://www.ruby-forum.com/u/Luo_Yong)\
**Post date:** [November 12, 2006, 11:37am UTC](https://www.ruby-forum.com/t/controlling-readline/75665/3 "2006-11-12T11:37:33Z")

</div>

On 11/12/06, Paul L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Maybe you should describe the problem, rather than asking about this way of  
> solving it.
> 
> –  
> Paul L.  
> [http://www.arachnoid.com](http://www.arachnoid.com)

I’m writing an RPC program.I use readline to read the command.I’m  
afraid that my program can be attacked by others if I can’t limit the  
maxium length the readline can read.So I’m finding a way to control  
it.

---

<div class="post-metadata">

**Author:** ![Luo\_Yong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@Luo\_Yong](https://www.ruby-forum.com/u/Luo_Yong)\
**Post date:** [November 12, 2006, 12:51pm UTC](https://www.ruby-forum.com/t/controlling-readline/75665/4 "2006-11-12T12:51:38Z")

</div>

Luo Yong wrote:

> > characters than you get than to get more than you expect (long sad story
> 
> I’m writing an RPC program.I use readline to read the command.I’m  
> afraid that my program can be attacked by others if I can’t limit the  
> maxium length the readline can read.So I’m finding a way to control  
> it.

As written, “readline” won’t read more than it can safely store, which  
is  
what you are after. Your concern arose from such things as “gets”,  
mentioned in my last post, that wrote beyond its allocated buffer  
because  
it didn’t know its length.

Why don’t you read a character at a time, then stop reading characters  
after  
you have what you want? Like this:

* * *

#!/usr/bin/ruby -w

max = 16

while true

print “Type something:”  
STDOUT.flush

input = STDIN.sysread(max)

puts “\nYou entered (in chunks of #{max} chars) #{input}.”

end

* * *

This routine reads the input in chunks of “max” characters, and  
continues to  
cycle through similarly sized chunks indefinitely. Linefeeds are not  
special to it, although that would be easy to add. This gives you a  
limit  
on input size, but you then have to sort out for yourself what a line  
is,  
and where one ends and another begins.

---

<div class="post-metadata">

**Author:** ![Luo\_Yong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@Luo\_Yong](https://www.ruby-forum.com/u/Luo_Yong)\
**Post date:** [November 12, 2006, 5:04pm UTC](https://www.ruby-forum.com/t/controlling-readline/75665/5 "2006-11-12T17:04:13Z")

</div>

On 11/12/06, Paul L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > Not really, not from the command line. You can throw away all the length
> 
> ## it didn’t know its length. while true

This may be the best way.

Thanks a lot.
