[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Bug-ed] ed EOF behavior
From: |
Andrew Moore |
Subject: |
Re: [Bug-ed] ed EOF behavior |
Date: |
Mon, 28 Jan 2013 17:42:22 -0800 |
Restoring GNU ed-1.7's get_tty_line() function to its GNU ed 0.2 equivalent
(patch below) exposes another bug, presumably in GNU ed-1.7's append_lines().
So while Andrea's patch does indeed appear to address the annoying behavior of
EOF in GNU ed-1.7, without looking deeper, I wonder whether it may not be
masking more problems?
A comment should also be added at the top of get_tty_line() that seems
relevant to Andrea' s efforts:
/* Read stdin one character at a time to avoid i/o contention
with shell escapes invoked by nonterminal input, e.g.,
ed - <<EOF
!cat
hello, world
EOF */
Finally, equating the null character '\0' with (int) 0 is asking for trouble...
-AM
--- ed-1.7/io.c 2013-01-28 17:07:45.000000000 -0800
+++ ed-0.2/io.c 2013-01-28 17:07:57.000000000 -0800
@@ -134,7 +134,7 @@
{
static char * buf = 0;
static int bufsz = 0;
- int i = 0, oi = -1;
+ int i = 0, oi = 0;
while( true )
{
@@ -150,16 +150,17 @@
else
{
clearerr( stdin ); if( i != oi ) { oi = i; continue; }
- if( i ) buf[i] = 0; if( sizep ) *sizep = i;
+ if( i ) buf[i] = '\0'; if( sizep ) *sizep = i;
return buf;
}
}
else
{
+ oi = 0;
if( !resize_buffer( &buf, &bufsz, i + 2 ) )
{ if( sizep ) *sizep = 0; return 0; }
buf[i++] = c; if( !c ) set_binary(); if( c != '\n' ) continue;
- buf[i] = 0; if( sizep ) *sizep = i;
+ buf[i] = '\0'; if( sizep ) *sizep = i;
return buf;
}
}
Andrea Monaco wrote:
> diff --git a/io.c b/io.c
> index 68956e1..820df15 100644
> --- a/io.c
> +++ b/io.c
> @@ -134,7 +134,7 @@ const char * get_tty_line( int * const sizep )
> {
> static char * buf = 0;
> static int bufsz = 0;
> - int i = 0, oi = -1;
> + int i = 0;
>
> while( true )
> {
> @@ -149,8 +149,9 @@ const char * get_tty_line( int * const sizep )
> }
> else
> {
> - clearerr( stdin ); if( i != oi ) { oi = i; continue; }
> - if( i ) buf[i] = 0; if( sizep ) *sizep = i;
> + clearerr( stdin );
> + /* when possible, it's better to put a null character */
> + if( bufsz > i ) buf[i] = 0; if( sizep ) *sizep = i;
> return buf;
> }
> }
>