[comp.lang.perl] Yet another perl portability bug

hoover@cs.UAlberta.CA (Jim Hoover) (12/11/90)

Larry:

On my SCO box, perl failed the lib.big test for big integers.  In tracking
down the failure I discovered a bug in the implementation of cmp or of
str_cmp (take your pick).

The cmp operator implementation assumes that str_cmp (in str.c) returns 
one of -1 0 +1. 

str_cmp is implemented with memcmp, which is specified (on my SCO box)
only to return a negative, zero, or positive value.  It says nothing about
the range of values.  SCO's version returns various postive and negative
values.

		Jim Hoover
Here is my fix to str_cmp:

741c741,742
< 	    return retval;
---
> 	    /* FIX - memcmp is not guaranteed to return -1 0 +1, just sign */
> 	    return (retval > 0) ? +1 : -1;
746c747,748
< 	return retval;
---
> 	    /* FIX - memcmp is not guaranteed to return -1 0 +1, just sign */
> 	    return (retval > 0) ? +1 : -1;

--
Prof. Jim Hoover                   | Office +1 403 492 5401 or 5290
Dept. of Computing Science         | FAX    +1 403 492 1071
University of Alberta              | hoover@cs.ualberta.ca
Edmonton, Alberta, Canada T6G 2H1  | 

gnb@bby.oz.au (Gregory N. Bond) (12/12/90)

>>>>> On 11 Dec 90 05:26:04 GMT, hoover@cs.UAlberta.CA (Jim Hoover) said:

Jim> 746c747,748
Jim> < 	return retval;
Jim> ---
Jim> 	    /* FIX - memcmp is not guaranteed to return -1 0 +1, just sign */
Jim> 	    return (retval > 0) ? +1 : -1;

Bzzt.  What it retval == 0?  You mean 
	return (retval > 0) ? 1 : (retval < 0) ? -1 : 0;
or perhaps
	return (retval < 0) ? -1 : (retval > 0);

But you knew that...

--
Gregory Bond, Burdett Buckeridge & Young Ltd, Melbourne, Australia
Internet: gnb@melba.bby.oz.au    non-MX: gnb%melba.bby.oz@uunet.uu.net
Uucp: {uunet,pyramid,ubc-cs,ukc,mcvax,prlb2,nttlab...}!munnari!melba.bby.oz!gnb

hoover@cs.UAlberta.CA (Jim Hoover) (12/12/90)

In article <1990Dec11.220809.20201@melba.bby.oz.au> gnb@bby.oz.au 
	(Gregory N. Bond) writes:
>>>>>> On 11 Dec 90 05:26:04 GMT, hoover@cs.UAlberta.CA (Jim Hoover) said:
>
>Jim> 746c747,748
>Jim> < 	return retval;
>Jim> ---
>Jim> 	    /* FIX - memcmp is not guaranteed to return -1 0 +1, just sign */
>Jim> 	    return (retval > 0) ? +1 : -1;
>
>Bzzt.  What it retval == 0?  You mean 
>	return (retval > 0) ? 1 : (retval < 0) ? -1 : 0;
> ...

Sorry, I seem to have become affected by the current trend toward 
obscurity disguised as cleverness.  I could have at least practiced 
what I preach and given more context :)

Within the context of the fix, retval cannot be 0, so the single 
test suffices.  Here is the full context:

    if (str1->str_cur < str2->str_cur) {
	if (retval = memcmp(str1->str_ptr, str2->str_ptr, str1->str_cur))
	    /* HJH - memcmp is not guaranteed to return -1 0 +1, just sign */
	    return (retval > 0) ? +1 : -1;
	else
	    return -1;
    }
    else if (retval = memcmp(str1->str_ptr, str2->str_ptr, str2->str_cur))
	    /* HJH - memcmp is not guaranteed to return -1 0 +1, just sign */
	    return (retval > 0) ? +1 : -1;
    else if (str1->str_cur == str2->str_cur)
	return 0;
    else
	return 1;

(The context, by the way, reflects a two's complement bias that is ubiquitous
in C code.  How much stuff breaks if you switch to a signed-magnitude machine
with +- 0?)


>--
>Gregory Bond, Burdett Buckeridge & Young Ltd, Melbourne, Australia
>Internet: gnb@melba.bby.oz.au    non-MX: gnb%melba.bby.oz@uunet.uu.net
>Uucp: {uunet,pyramid,ubc-cs,ukc,mcvax,prlb2,nttlab...}!munnari!melba.bby.oz!gnb


--
Prof. Jim Hoover                   | Office +1 403 492 5401 or 5290
Dept. of Computing Science         | FAX    +1 403 492 1071
University of Alberta              | hoover@cs.ualberta.ca
Edmonton, Alberta, Canada T6G 2H1  |