Mailing List Archive

C eval order bug + fix, HP-UX 7.0 and CLIX 3.1
Hi,

On line 598 of scope.c in perl 5.001 patchlevel 1m, the statement

savestack_ix -= SSPOPINT;

which expands to

savestack_ix -= (savestack[--savestack_ix].any_i32);

relies on the right-hand operand of the -= being evaluated before the
left-hand operand: evaluating the left-hand operand first decrements
savestack_ix by one less than intended.

I found that both HP-UX 7.0's and CLIX 3.1's compilers evaluate the left-hand
operand first. This doesn't appear to violate the ANSI C standard, which
allows any operand evaluation order for -=, according to the second edition of
K&R's "The C Programming Language".

Here's a patch to remove this dependency on operand evaluation order:

*** scope.c.orig Fri Jun 9 10:26:10 1995
--- scope.c Wed Sep 13 18:29:15 1995
***************
*** 594,602 ****
ptr = SSPOPPTR;
(*SSPOPDPTR)(ptr);
break;
! case SAVEt_REGCONTEXT:
! savestack_ix -= SSPOPINT; /* regexp must have croaked */
! break;
default:
croak("panic: leave_scope inconsistency");
}
--- 594,603 ----
ptr = SSPOPPTR;
(*SSPOPDPTR)(ptr);
break;
! case SAVEt_REGCONTEXT: {
! int pop_count = SSPOPINT; /* regexp must have croaked */
! savestack_ix -= pop_count;
! break; }
default:
croak("panic: leave_scope inconsistency");
}

Nick Duffek
nsd@bbc.com
Re: C eval order bug + fix, HP-UX 7.0 and CLIX 3.1 [ In reply to ]
> From: nsd@bbc.com (Nick Duffek)
>
> which expands to
>
> savestack_ix -= (savestack[--savestack_ix].any_i32);
>
> relies on the right-hand operand of the -= being evaluated before the
> left-hand operand: evaluating the left-hand operand first decrements
> savestack_ix by one less than intended.
>
> I found that both HP-UX 7.0's and CLIX 3.1's compilers evaluate the left-hand
> operand first. This doesn't appear to violate the ANSI C standard, which
> allows any operand evaluation order for -=, according to the second edition of
> K&R's "The C Programming Language".
>

/*

I dont see the left-side being eval'd first in this test case.
I get the same results on many types/OSes of hps:

HP-UX A.09.01 A 9000/715
HP-UX A.09.01 A 9000/730
HP-UX A.09.01 A 9000/735
HP-UX A.09.01 A 9000/750

HP-UX A.09.05 A 9000/715
HP-UX A.09.05 A 9000/730
HP-UX A.09.05 A 9000/735
HP-UX A.09.05 A 9000/750

using both these compilers:
/bin/cc: HP92453-01 A.09.61 HP C Compiler
/bin/cc: HP92453-01 A.09.19 HP C (Bundled) Compiler

(not sure what it is you refer to: "HP-UX 7.0's ... compilers")

*/

main () {
struct { int i, j; } struk[2];
int i,ii, j,jj,jjj, k;
i=ii=3;
j=jj=1;

j -= (i+ (k=(--j)) ); /* simple parenthesized case */
printf ("j = %d, k = %d\n", j,k); /* should be -3 and 0 */

jj -= ii+ --jj; /* trust expected eval order */
printf ("jj = %d\n", jj); /* should be -3 */

jjj = 1;
struk[0].i = 100;
jjj -= (struk[--jjj].i); /* more complicated structure/array */
printf ("jjj = %d\n", jjj); /* should be -100 */
}

--
Chip Capelik
chip@gdwest.gd.com
Re: C eval order bug + fix, HP-UX 7.0 and CLIX 3.1 [ In reply to ]
> I dont see the left-side being eval'd first in this test case.
> I get the same results on many types/OSes of hps:
>
> HP-UX A.09.01 A 9000/715
[...]
> (not sure what it is you refer to: "HP-UX 7.0's ... compilers")

/bin/cc: HP92453-01 A.07.10 HP C Compiler. I agree that newer HP-UX compilers
evaluate the right-hand side first; the vendor-supplied compilers I tested on
OSF/1 V3.0, AIX 3.2, SunOS 4.1.3_U1, SCO 3.2, ULTRIX 4.2, and NCR SysV 4.0
also do so.

Nick
Re: C eval order bug + fix, HP-UX 7.0 and CLIX 3.1 [ In reply to ]
According to Chip Capelik:
> Someone wrote:
> > savestack_ix -= (savestack[--savestack_ix].any_i32);
> >
> > relies on the right-hand operand of the -= being evaluated before the
> > left-hand operand ...
>
> I dont see the left-side being eval'd first in this test case.

Not to disagree, but it doesn't matter what any given compiler does.
The quoted expression does not have predictable results based on the
ANSI C standard. So it should be fixed no matter what.
--
Chip Salzenberg, aka <chs@nando.net>
"Hey, it's the Miss Alternate Universe Pageant!"
-- Crow T. Robot, MST3K: "Stranded In Space"
Re: C eval order bug + fix, HP-UX 7.0 and CLIX 3.1 [ In reply to ]
> The quoted expression does not have predictable results based on the
> ANSI C standard. So it should be fixed no matter what.

'Sides, it's been fixed in my copy for quite some time.

Larry