NetBSD Problem Report #10705

Received: (qmail 1447 invoked from network); 28 Jul 2000 20:45:53 -0000
Message-Id: <200007282045.QAA02196@distress.stat.cmu.edu>
Date: Fri, 28 Jul 2000 16:45:51 -0400
From: "Johnny C. Lam" <lamj@stat.cmu.edu>
To: gnats-bugs@gnats.netbsd.org
Cc: lamj@stat.cmu.edu
Subject: libcurses keypad() segfaulting when reading from stdin
X-Send-Pr-Version: 3.95

>Number:         10705
>Category:       lib
>Synopsis:       libcurses keypad() segfaulting when reading from stdin
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    blymn
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Jul 28 20:46:00 +0000 2000
>Closed-Date:    Fri Jul 19 13:42:14 +0000 2002
>Last-Modified:  Fri Jul 19 13:42:14 +0000 2002
>Originator:     Johnny C. Lam
>Release:        NetBSD-20000723/i386
>Organization:
	Department of Statistics, Carnegie Mellon University
>Environment:
System: NetBSD mariposa.home.net 1.5C NetBSD 1.5C (MARIPOSA) #0: Mon Jul 24 19:27:52 EDT 2000 jlam@mariposa.home.net:/usr/src/sys/arch/i386/compile/MARIPOSA i386


>Description:
	keypad segfaults when the enclosing program reads from stdin.
It's possible I'm not understanding curses, having never programmed with
it before.  But the following doesn't segfault when I compile it with
ncurses.

#include <curses.h>

int
main ()
{
	initscr ();
	keypad (stdscr, TRUE);
	exit (0);
}

When the above program is compiled and invoked as:

	./foo

everything is alright.  When invoked as:

	echo xxx | ./foo

I get a Memory fault (core dumped).  gdb yields:

GNU gdb 4.17
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386--netbsd"...
Core was generated by `foo'.
Program terminated with signal 11, Segmentation fault.
Reading symbols from /usr/libexec/ld.elf_so...done.
Reading symbols from /usr/lib/libcurses.so.3...done.
Reading symbols from /usr/lib/libtermcap.so.0...done.
Reading symbols from /usr/lib/libc.so.12...done.
#0  0x48069ca5 in keypad ()
(gdb) bt
#0  0x48069ca5 in keypad ()
#1  0x8048889 in main () at foo.c:7
#2  0x804862d in ___start ()
(gdb) quit

>How-To-Repeat:
	Try the above.
>Fix:
	Unknown
>Release-Note:
>Audit-Trail:

From: Jun-ichiro itojun Hagino <itojun@itojun.org>
To: gnats-bugs@netbsd.org
Cc:  
Subject: Re: lib/10705
Date: Tue, 1 Aug 2000 01:08:19 +0900 (JST)

 this problem is not unique to keypad().  initscr() does not initialize
 stdscr if gettmode() fails, and all the functions that touch stdscr will
 choke (including getch() = wgetch(stdscr), keypad(stdscr, x), whatever).

 i'm not sure what is the right way to solve it, standard-wise.

 itojun

Responsible-Changed-From-To: lib-bug-people->blymn 
Responsible-Changed-By: blymn 
Responsible-Changed-When: Tue May 21 04:51:41 PDT 2002 
Responsible-Changed-Why:  
This is a curses issue.  I shall look into it. 

From: Brett Lymn <blymn@baesystems.com.au>
To: gnats-bugs@gnats.netbsd.org
Cc: itojun@itojun.org, lamj@stat.cmu.edu
Subject: Re: lib/10705
Date: Sat, 25 May 2002 22:01:09 +0930

 The analysis done by Itojun is correct, stdscr is not set if
 gettmode() fails, it fails because stdin is not a tty so the terminal
 modes cannot be retrieved.  In this case initscr() returns NULL,
 looking at it harshly, a well behaved program should be checking the
 return value of initscr() and dealing with the case where curses fails
 to initialise properly - the init failed so other curses functions
 cannot be expected to behave in a defined manner.

 Unfortunately, most curses programs do not check the return of
 initscr() and hence will barf if input is piped to the app.  The patch
 below tries retrieving the terminal characteristics firstly from
 stdin, then stdout and if both these fail it sets a flag that
 effectively disables the calls to the tty related functions.

 Can you please let me know if the following patch works ok for you?
 If it does I shall commit it.

 ==============================================================================
 Index: curses_private.h
 ===================================================================
 RCS file: /cvsroot/basesrc/lib/libcurses/curses_private.h,v
 retrieving revision 1.20
 diff -r1.20 curses_private.h
 235a236
 > 	int notty;
 Index: screen.c
 ===================================================================
 RCS file: /cvsroot/basesrc/lib/libcurses/screen.c,v
 retrieving revision 1.5
 diff -r1.5 screen.c
 139a140
 > 	new_screen->notty = FALSE;
 Index: setterm.c
 ===================================================================
 RCS file: /cvsroot/basesrc/lib/libcurses/setterm.c,v
 retrieving revision 1.31
 diff -r1.31 setterm.c
 354c354
 < 		__CTRACE("%2.2s = %d\n", namp, *vp[-1]);
 ---
 > 		__CTRACE("%2.2s = %d\n", namp, vp[-1]);
 370c370
 < 		__CTRACE("%2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\"");
 ---
 > 		__CTRACE("%2.2s = %s", namp, sp[-1] == NULL ? "NULL\n" : "\"");
 Index: tty.c
 ===================================================================
 RCS file: /cvsroot/basesrc/lib/libcurses/tty.c,v
 retrieving revision 1.28
 diff -r1.28 tty.c
 84c84,87
 <     return cfgetospeed(&_cursesi_screen->baset);
 ---
 > 	if (_cursesi_screen->notty == TRUE)
 > 		return 0;
 > 
 > 	return cfgetospeed(&_cursesi_screen->baset);
 112,113c115,124
 < 	if (tcgetattr(fileno(screen->infd), &screen->orig_termios))
 < 		return (ERR);
 ---
 > 	if (tcgetattr(fileno(screen->infd), &screen->orig_termios)) {
 > 		  /* if the input fd is not a tty try the output */
 > 		if (tcgetattr(fileno(screen->infd), &screen->orig_termios)) {
 > 			  /* not a tty ... we will disable tty related stuff */
 > 			screen->notty = TRUE;
 > 			__GT = 0;
 > 			__NONL = 0;
 > 			return (OK);
 > 		}
 > 	}
 166a178,179
 > 	if (_cursesi_screen->notty == TRUE)
 > 		return OK;
 179a193,194
 > 	if (_cursesi_screen->notty == TRUE)
 > 		return OK;
 193a209,210
 > 	if (_cursesi_screen->notty == TRUE)
 > 		return OK;
 208a226,227
 > 	if (_cursesi_screen->notty == TRUE)
 > 		return OK;
 222a242,243
 > 	if (_cursesi_screen->notty == TRUE)
 > 		return OK;
 240a262,263
 > 	if (_cursesi_screen->notty == TRUE)
 > 		return OK;
 258a282,283
 > 	if (_cursesi_screen->notty == TRUE)
 > 		return;
 269a295,296
 > 	if (_cursesi_screen->notty == TRUE)
 > 		return;
 284a312,313
 > 	if (_cursesi_screen->notty == TRUE)
 > 		return OK;
 305a335,336
 > 	if (_cursesi_screen->notty == TRUE)
 > 		return OK;
 346a378,379
 > 	if (_cursesi_screen->notty == TRUE)
 > 		return OK;
 366a400,401
 > 	if (_cursesi_screen->notty == TRUE)
 > 		return OK;
 386a422,423
 > 	if (_cursesi_screen->notty == TRUE)
 > 		return OK;
 462a500,501
 > 	if (_cursesi_screen->notty == TRUE)
 > 		return OK;
 469a509,510
 > 	if (_cursesi_screen->notty == TRUE)
 > 		return OK;
 482a524,525
 > 	if (_cursesi_screen->notty == TRUE)
 > 		return 0;
 492a536,537
 > 	if (_cursesi_screen->notty == TRUE)
 > 		return 0;


 -- 
 Brett Lymn
State-Changed-From-To: open->feedback 
State-Changed-By: blymn 
State-Changed-When: Sat May 25 06:00:26 PDT 2002 
State-Changed-Why:  
waiting for feedback about patch that fixes the problem. 

From: itojun@iijlab.net
To: Brett Lymn <blymn@baesystems.com.au>
Cc: gnats-bugs@gnats.netbsd.org, lamj@stat.cmu.edu
Subject: Re: lib/10705 
Date: Sun, 26 May 2002 00:43:56 +0900

 >The analysis done by Itojun is correct, stdscr is not set if
 >gettmode() fails, it fails because stdin is not a tty so the terminal
 >modes cannot be retrieved.  In this case initscr() returns NULL,
 >looking at it harshly, a well behaved program should be checking the
 >return value of initscr() and dealing with the case where curses fails
 >to initialise properly - the init failed so other curses functions
 >cannot be expected to behave in a defined manner.
 >
 >Unfortunately, most curses programs do not check the return of
 >initscr() and hence will barf if input is piped to the app.  The patch
 >below tries retrieving the terminal characteristics firstly from
 >stdin, then stdout and if both these fail it sets a flag that
 >effectively disables the calls to the tty related functions.
 >
 >Can you please let me know if the following patch works ok for you?
 >If it does I shall commit it.

 	works for me.  i would get rid of two leading spaces on the comment
 	line added below.

 itojun


 >---
 >> 	if (tcgetattr(fileno(screen->infd), &screen->orig_termios)) {
 >> 		  /* if the input fd is not a tty try the output */
 >> 		if (tcgetattr(fileno(screen->infd), &screen->orig_termios)) {
 >> 			  /* not a tty ... we will disable tty related stuff */
 >> 			screen->notty = TRUE;
 >> 			__GT = 0;
 >> 			__NONL = 0;
 >> 			return (OK);
 >> 		}
 >> 	}
State-Changed-From-To: feedback->closed 
State-Changed-By: blymn 
State-Changed-When: Fri Jul 19 06:40:36 PDT 2002 
State-Changed-Why:  
libcurses tty handling made more robust in the face of not having a tty. 
This has resolved this problem. 
>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.