Subject: Re: pkg/37062: hnb segvfaults with curses, doesn't look right
To: None <pkg-manager@netbsd.org, gnats-admin@netbsd.org,>
From: Julian Coleman <jdc@coris.org.uk>
List: pkgsrc-bugs
Date: 10/18/2007 09:35:02
The following reply was made to PR pkg/37062; it has been noted by GNATS.

From: Julian Coleman <jdc@coris.org.uk>
To: gnats-bugs@NetBSD.org
Cc: jdc@NetBSD.org
Subject: Re: pkg/37062: hnb segvfaults with curses, doesn't look right
Date: Thu, 18 Oct 2007 10:27:38 +0100

 > hnb is not compatible with curses library used with NetBSD. After pressing
 > Escape it segvfaults. Included patch fixes that.
 
 The former looks like a bug in hnb.  The curses keyname() function is
 allowed to return NULL and hnb doesn't check for this.  However, ncurses
 returns "-1" for keyname(-1), which is what hnb does here.
 
 The appended patch makes NetBSD curses compatible with ncurses (and
 incompatible with Solaris' curses) and also updates keyname() and
 key_name() to use static space (otherwise applications will leak a small
 amount of memory each time they call keyname() or key_name().  I will
 probably commit this, as more applications are written to ncurses than
 the curses standard.
 
 The latter problem of the hnb display may well be a NetBSD curses bug -
 I'll investigate further.
 
 J
 
   - - 8< - - - - - - - - - - - - - Cut here - - - - - - - - - - - - - >8 - -
 diff -ur /usr/src/lib/libcurses/curses_keyname.3 ./curses_keyname.3
 --- /usr/src/lib/libcurses/curses_keyname.3	2004-03-21 11:21:19.000000000 +0000
 +++ ./curses_keyname.3	2007-10-17 13:22:00.000000000 +0100
 @@ -28,7 +28,7 @@
  .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  .\" POSSIBILITY OF SUCH DAMAGE.
  .\"
 -.Dd June 13, 2003
 +.Dd October 17, 2007
  .Dt CURSES_KEYNAME 3
  .Os
  .Sh NAME
 @@ -57,9 +57,14 @@
  .It Li "Meta + visible character" Ta "159 - 254" Ta "M-X"
  .It Li "Meta + delete character" Ta "255" Ta "M-^?"
  .It Li "Named key" Ta "KEY_MIN - KEY_MAX" Ta "KEY_EXIT"
 +.It Li "Unknown key" Ta "" Ta "-1"
  .El
  .Sh SEE ALSO
  .Xr curses_input 3
 +.Sh NOTE
 +The return value of
 +.Fn keyname
 +is a static buffer, which will be overwritten on a subsequent call.
  .Sh STANDARDS
  The
  .Nx
 diff -ur /usr/src/lib/libcurses/keyname.c ./keyname.c
 --- /usr/src/lib/libcurses/keyname.c	2007-08-04 09:36:49.000000000 +0100
 +++ ./keyname.c	2007-10-17 12:17:04.000000000 +0100
 @@ -48,6 +48,8 @@
  #include "curses_private.h"
  
  #define KEYNAMEMAX (size_t) 14	/* "KEY_BACKSPACE\0" */
 +static char name[KEYNAMEMAX + 1];
 +
  /*
   * keyname --
   *	Return name of key or NULL;
 @@ -57,19 +59,19 @@
  {
  /* We don't bother with the large keyname table if SMALL is defined. */
  #ifdef SMALL
 -	return NULL;
 +	strcpy(name, "-1\0");
 +	return name;
  #else
 -	char *name;
 -
 -	if (key < 0)
 -		return NULL;
 +	if (key < 0) {
 +		strcpy(name, "-1\0");
 +		return name;
 +	}
  
  	/* No name. */
 -	if (key == 0x100)
 -		return NULL;
 -
 -	if ((name = malloc(KEYNAMEMAX + 1)) == NULL)
 -		return NULL;
 +	if (key == 0x100) {
 +		strcpy(name, "-1\0");
 +		return name;
 +	}
  
  	/* Control codes */
  	if (key < 0x20) {
 @@ -500,20 +502,14 @@
  #ifndef HAVE_WCHAR
  	return NULL;
  #else
 -/* We don't bother with the large keyname table if SMALL is defined. */
 -#ifdef SMALL
 -	return NULL;
 -#else
 -	char *name = keyname(( int )key );
 +	(void) keyname((int) key);
  
 -	if ( !name )
 -		return NULL;
 -	if (!strncmp( name, "M-", 2 )) {
 -		free( name );
 -		name = NULL;
 +	if (!strncmp(name, "M-", 2)) {
 +		/* Remove the "M-" */
 +		name[0] = name[2];
 +		name[1] = '\0';
  	}
  	return name;
 -#endif
  #endif /* HAVE_WCHAR */
  }
  
   - - 8< - - - - - - - - - - - - - Cut here - - - - - - - - - - - - - >8 - -
 -- 
   My other computer also runs NetBSD    /        Sailing at Newbiggin
         http://www.netbsd.org/        /   http://www.newbigginsailingclub.org/