NetBSD Problem Report #45370

From www@NetBSD.org  Fri Sep 16 18:44:55 2011
Return-Path: <www@NetBSD.org>
Received: from mail.netbsd.org (mail.netbsd.org [204.152.190.11])
	by www.NetBSD.org (Postfix) with ESMTP id 9FED663B89A
	for <gnats-bugs@gnats.NetBSD.org>; Fri, 16 Sep 2011 18:44:55 +0000 (UTC)
Message-Id: <20110916184454.7E70963B884@www.NetBSD.org>
Date: Fri, 16 Sep 2011 18:44:54 +0000 (UTC)
From: tnozaki@netbsd.org
Reply-To: tnozaki@netbsd.org
To: gnats-bugs@NetBSD.org
Subject: termcap emulation tget{flag,num,str} want nul terminated string
X-Send-Pr-Version: www-1.0

>Number:         45370
>Category:       lib
>Synopsis:       termcap emulation tget{flag,num,str} want nul terminated string
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    christos
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Sep 16 18:45:00 +0000 2011
>Closed-Date:    Sun Nov 13 19:03:59 +0000 2011
>Last-Modified:  Sun Nov 13 19:03:59 +0000 2011
>Originator:     Takehiko NOZAKI
>Release:        NetBSD  5.99.55
>Organization:
N/A
>Environment:
NetBSD  5.99.55 NetBSD 5.99.55 (XXX) #6: Mon Aug 29 06:35:08 JST 2011  root@:/usr/obj.amd64/sys/arch/amd64/compile/XXX amd64
>Description:
termcap's function tget{flag,num,str} traditionaly declared as following prototype:

    int tgetflag(char id[2]);
    int tgetnum(char id[2]);
    char *tgetstr(char id[2], char **area);

(http://pubs.opengroup.org/onlinepubs/007908799/xcurses/tgetent.html)

so that, id string is not expected to be nul terminated.
if id's length more than 2, we have to cut it first 2byte, and ignore after 3.

our old termcap library(and also GNU ncurses) considering such case.

http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/lib/libterm/Attic/termcap.c?rev=1.55&content-type=text/plain

(snip)
char *
tgetstr(const char *id, char **area)
{
...
	char ids[3];

...
	/*
	 * XXX
	 * This is for all the boneheaded programs that relied on tgetstr
	 * to look only at the first 2 characters of the string passed...
	 */
	ids[0] = id[0];
	ids[1] = id[1];
	ids[2] = '\0';
(snip)
>How-To-Repeat:
see "boneheaded programs" traditioanl ex/vi's usage of tget{flag,str}:

http://ex-vi.cvs.sourceforge.net/viewvc/ex-vi/ex-vi/ex_tty.c?revision=1.4&view=markup

(snip)
void
zap(void)
{
        register char *namp;
        register bool **fp;
        register char ***sp;
        int flag;
        char *string;

#ifndef UCVISUAL
        namp = "ambsdadbeohcinmincnsosulxbxnxtxx";
#else
        namp = "ambsdadbeohchzinmincnsosulxbxnxtxx";
#endif
        fp = sflags;
        do {
                flag = tgetflag(namp);
                *(*fp++) = flag;
                namp += 2;
        } while (*namp);
        namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullndnlpcrcscsesfsosrtatetiupvbvsveALDLUPDOLERI";
        sp = sstrs;
        do {
                string = tgetstr(namp, &aoftspace);
                *(*sp++) = string;
                namp += 2;
        } while (*namp);
}
(snip)
>Fix:
Index: termcap.c
===================================================================
RCS file: /cvsroot/src/lib/libterminfo/termcap.c,v
retrieving revision 1.14
diff -u -r1.14 termcap.c
--- termcap.c   18 Mar 2011 10:42:54 -0000      1.14
+++ termcap.c   16 Sep 2011 18:04:23 -0000
@@ -74,17 +74,19 @@
 }

 int
-tgetflag(const char *id)
+tgetflag(const char *_id)
 {
        uint32_t ind;
        size_t i;
        TERMUSERDEF *ud;
+       char id[3];

        _DIAGASSERT(id != NULL);

        if (cur_term == NULL)
                return 0;


+       strlcpy(&id[0], _id, sizeof(id));
        ind = _t_flaghash((const unsigned char *)id, strlen(id));
        if (ind <= __arraycount(_ti_cap_flagids)) {
                if (strcmp(id, _ti_cap_flagids[ind].id) == 0)
@@ -99,18 +101,20 @@
 }

 int
-tgetnum(const char *id)
+tgetnum(const char *_id)
 {
        uint32_t ind;
        size_t i;
        TERMUSERDEF *ud;
        const TENTRY *te;
+       char id[3];

        _DIAGASSERT(id != NULL);

        if (cur_term == NULL)
                return -1;


+       strlcpy(&id[0], _id, sizeof(id));
        ind = _t_numhash((const unsigned char *)id, strlen(id));
        if (ind <= __arraycount(_ti_cap_numids)) {
                te = &_ti_cap_numids[ind];
@@ -132,12 +136,13 @@
 }

 char *
-tgetstr(const char *id, char **area)
+tgetstr(const char *_id, char **area)
 {
        uint32_t ind;
        size_t i;
        TERMUSERDEF *ud;
        const char *str;
+       char id[3];

        _DIAGASSERT(id != NULL);

@@ -145,6 +150,7 @@
                return NULL;

        str = NULL;
+       strlcpy(&id[0], _id, sizeof(id));
        ind = _t_strhash((const unsigned char *)id, strlen(id));
        if (ind <= __arraycount(_ti_cap_strids)) {
                if (strcmp(id, _ti_cap_strids[ind].id) == 0) {

>Release-Note:

>Audit-Trail:
From: "Christos Zoulas" <christos@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/45370 CVS commit: src/lib/libterminfo
Date: Fri, 16 Sep 2011 14:51:44 -0400

 Module Name:	src
 Committed By:	christos
 Date:		Fri Sep 16 18:51:44 UTC 2011

 Modified Files:
 	src/lib/libterminfo: termcap.c

 Log Message:
 PR/45370: Takehiko NOZAKI: termcap emulation tget{flag,num,str} should
 work with non-NUL terminated strings.


 To generate a diff of this commit:
 cvs rdiff -u -r1.14 -r1.15 src/lib/libterminfo/termcap.c

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

Responsible-Changed-From-To: lib-bug-people->christos
Responsible-Changed-By: wiz@NetBSD.org
Responsible-Changed-When: Fri, 16 Sep 2011 23:53:07 +0000
Responsible-Changed-Why:
Christos committed something, ok to close?


State-Changed-From-To: open->feedback
State-Changed-By: wiz@NetBSD.org
State-Changed-When: Fri, 16 Sep 2011 23:53:07 +0000
State-Changed-Why:


From: Takehiko NOZAKI <takehiko.nozaki@gmail.com>
To: gnats-bugs@netbsd.org
Cc: lib-bug-people@netbsd.org, gnats-admin@netbsd.org, netbsd-bugs@netbsd.org, 
	tnozaki@netbsd.org
Subject: Re: PR/45370 CVS commit: src/lib/libterminfo
Date: Sun, 13 Nov 2011 23:25:38 +0900

 hi, chirostos.

 your modification may lose following case:

   tgetstr("", &area);


 that's why i used strlcpy(3) in my patch.

 very truly yours.
 --
 Takehiko NOZAKI <tnozaki@NetBSD.org>

 2011/9/17 Christos Zoulas <christos@netbsd.org>:
 > The following reply was made to PR lib/45370; it has been noted by GNATS.
 >
 > From: "Christos Zoulas" <christos@netbsd.org>
 > To: gnats-bugs@gnats.NetBSD.org
 > Cc:
 > Subject: PR/45370 CVS commit: src/lib/libterminfo
 > Date: Fri, 16 Sep 2011 14:51:44 -0400
 >
 > =A0Module Name: =A0 src
 > =A0Committed By: =A0christos
 > =A0Date: =A0 =A0 =A0 =A0 =A0Fri Sep 16 18:51:44 UTC 2011
 >
 > =A0Modified Files:
 > =A0 =A0 =A0 =A0src/lib/libterminfo: termcap.c
 >
 > =A0Log Message:
 > =A0PR/45370: Takehiko NOZAKI: termcap emulation tget{flag,num,str} should
 > =A0work with non-NUL terminated strings.
 >
 >
 > =A0To generate a diff of this commit:
 > =A0cvs rdiff -u -r1.14 -r1.15 src/lib/libterminfo/termcap.c
 >
 > =A0Please note that diffs are not public domain; they are subject to the
 > =A0copyright notices on the relevant files.
 >
 >

From: christos@zoulas.com (Christos Zoulas)
To: Takehiko NOZAKI <takehiko.nozaki@gmail.com>, gnats-bugs@netbsd.org
Cc: lib-bug-people@netbsd.org, gnats-admin@netbsd.org, 
	netbsd-bugs@netbsd.org, tnozaki@netbsd.org
Subject: Re: PR/45370 CVS commit: src/lib/libterminfo
Date: Sun, 13 Nov 2011 10:24:24 -0500

 On Nov 13, 11:25pm, takehiko.nozaki@gmail.com (Takehiko NOZAKI) wrote:
 -- Subject: Re: PR/45370 CVS commit: src/lib/libterminfo

 | hi, chirostos.
 | 
 | your modification may lose following case:
 | 
 |   tgetstr("", &area);
 | 
 | 
 | that's why i used strlcpy(3) in my patch.
 | 
 | very truly yours.

 Good point!

 Thanks,

 christos

State-Changed-From-To: feedback->closed
State-Changed-By: tnozaki@NetBSD.org
State-Changed-When: Sun, 13 Nov 2011 19:03:59 +0000
State-Changed-Why:
christos fixed now, 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-2007 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.