Article View: net.bugs.usg
Article #46System V.2 Curses Bug #1
From: Mike Laman
Date: Fri, 10 Aug 1984 23:42
Date: Fri, 10 Aug 1984 23:42
55 lines
2218 bytes
2218 bytes
There are two bugs in the "portable" routine "getsh()" in the file
screen/setupterm.c. The first bug is that the comparison "if (*p == 0377)"
is incorrect if characters are signed by default. If *p contained the
BYTE value '\377', sign extension would make it so *p != 0377 since 0377
is an integer and will NOT be sign extended.
The second bug is that "rv = *p++" will do sign extension (for systems with
characters being signed). What we want is to sign extend on ONLY the
"high part"; more precisely, we want to sign extend ONLY on the assignment
that follows which puts the high byte in and NOT on the assignment that puts
the low byte in.
[ I suspect that if this routine was used, it was used on a machine where
characters are unsigned. ]
The original getsh() follows:
getsh(p)
register char *p;
{
register int rv;
if(*p == 0377)
return -1;
rv = *p++;
rv += *p * 256;
return rv;
}
the following should do the trick:
getsh(p)
register char *p;
{
register int rv;
rv = *((unsigned char *) p);
if(rv == 0377)
return -1; /* This is a pretty common case */
return rv + (*++p * 256);
}
Instead of this though, I use the following macro (since this is for a machine
with signed characters):
#define getsh(ip) (*((unsigned char *) ip) | (*(ip+1) << 8))
Folks on machines with unsigned character implementations could use:
#define getsh(ip) ((*ip == 0377) ? -1 : (*ip | (*(ip+1) << 8))))
[ This should serve the perpose required as stated in the comment above
the macro definitions. ]
Mike Laman, NCR @ Torrey Pines
UUCP: {ucbvax,philabs,sdcsla}!sdcsvax!laman
Technical Details
Message-ID:
<1112@sdcsvax.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!mhuxl!houxm!houxz!vax135!cornell!uw-beaver!tektronix!hplabs!sdcrdcf!sdcsvax!lamanShow Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site sdcsvax.UUCP Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!mhuxl!houxm!houxz!vax135!cornell!uw-beaver!tektronix!hplabs!sdcrdcf!sdcsvax!laman From: laman@sdcsvax.UUCP (Mike Laman) Newsgroups: net.bugs.usg Subject: System V.2 Curses Bug #1 Message-ID: <1112@sdcsvax.UUCP> Date: Fri, 10-Aug-84 19:42:59 EDT Article-I.D.: sdcsvax.1112 Posted: Fri Aug 10 19:42:59 1984 Date-Received: Mon, 13-Aug-84 00:16:08 EDT Organization: EECS Dept. U.C. San Diego Lines: 55