NetBSD Problem Report #50863

From www@NetBSD.org  Sat Feb 27 17:24:08 2016
Return-Path: <www@NetBSD.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(Client CN "mail.netbsd.org", Issuer "Postmaster NetBSD.org" (verified OK))
	by mollari.NetBSD.org (Postfix) with ESMTPS id A9E0B7ABE4
	for <gnats-bugs@gnats.NetBSD.org>; Sat, 27 Feb 2016 17:24:08 +0000 (UTC)
Message-Id: <20160227172407.468447ACAF@mollari.NetBSD.org>
Date: Sat, 27 Feb 2016 17:24:07 +0000 (UTC)
From: a6funxh77z@snkmail.com
Reply-To: a6funxh77z@snkmail.com
To: gnats-bugs@NetBSD.org
Subject: libedit el_end() messes up term settings if piped
X-Send-Pr-Version: www-1.0

>Number:         50863
>Category:       lib
>Synopsis:       libedit el_end() messes up term settings if piped
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    lib-bug-people
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat Feb 27 17:25:00 +0000 2016
>Closed-Date:    Mon Feb 29 14:59:40 +0000 2016
>Last-Modified:  Mon Feb 29 14:59:40 +0000 2016
>Originator:     John Hein
>Release:        head
>Organization:
none
>Environment:
Noticed with freebsd ports version of libedit (basically 201503 netbsd version).  Reproduced with libedit code from netbsd head on freebsd.

FreeBSD foo.example.com 9.3-STABLE-20150728 FreeBSD 9.3-STABLE-20150728 #8: Fri Jul 31 09:01:31 MDT 2015     foo@example.com:/usr/obj/usr/src/sys/FOO  i386


Sorry I don't have a netbsd box to use at the moment.  But it's easy to confirm if you do.  And code inspection of head netbsd code seems to show the problem still exists.
>Description:
==================
% cat foo.c
#include <stdio.h>
#include <histedit.h>

static EditLine *el_e = 0;

int
main(int argc, char **argv)
{
        printf("start\n");
        el_e = el_init("foo", stdin, stdout, stderr);
        if (el_e)
                el_end(el_e);
        else
                printf("el_init failed\n");
        printf("end\n");
        printf("end\n");
        return 0;
}
====================

cc foo.c -ledit -o foo && ./foo | cat

The output after el_end() has stair stepped lines (no CR), 'stty -a' shows the baud rate at 50, and on freebsd at least, that messes up things like vi and such.

Looking at the libedit code, it looks like there's a case where code in el_end() tries to restore the original tty settings.  But if the output is not a tty, the original tty settings were never saved.  So it restores invalid termios settings.

The problem is that tty_setup() checks if the output file descriptor is a tty and does a short circuit exit before the termios settings can be saved.  Then el_end(), unaware that the t_or struct is not correctly populated, happily restores the t_or settings that are bogus.
>How-To-Repeat:
See description.
>Fix:
Record original termios settings in tty_setup() even if not isatty().  Or don't try to restore termios settings in tty_end() if tty_setup() never saved them (set a flag or use a pointer for t_or that's null unless the struct is valid or something like that).


>Release-Note:

>Audit-Trail:
From: "Christos Zoulas" <christos@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/50863 CVS commit: src/lib/libedit
Date: Sat, 27 Feb 2016 13:13:21 -0500

 Module Name:	src
 Committed By:	christos
 Date:		Sat Feb 27 18:13:21 UTC 2016

 Modified Files:
 	src/lib/libedit: tty.c tty.h

 Log Message:
 PR/50863: John Hein: libedit el_end() messes up term settings if piped
 Keep track if we initialized the tty, and only reset it if we did.


 To generate a diff of this commit:
 cvs rdiff -u -r1.57 -r1.58 src/lib/libedit/tty.c
 cvs rdiff -u -r1.18 -r1.19 src/lib/libedit/tty.h

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: open->closed
State-Changed-By: dholland@NetBSD.org
State-Changed-When: Mon, 29 Feb 2016 14:59:40 +0000
State-Changed-Why:
Christos fixed it, thanks!


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2014 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.