Subject: bin/6373: cc -O of if-clause conditional expression
To: None <gnats-bugs@gnats.netbsd.org>
From: None <john.firestone@nord-com.net>
List: netbsd-bugs
Date: 10/29/1998 12:06:49
>Number:         6373
>Category:       bin
>Synopsis:       cc -O of if-clause conditional expression evaluates as FALSE
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Oct 29 03:20:01 1998
>Last-Modified:
>Originator:     John Firestone
>Organization:
	
>Release:        gcc version 2.7.2.2+myc1
>Environment:
IIsi
              NetBSD 1.3.2 SBC
System: NetBSD eisbox 1.3.2 NetBSD 1.3.2 (GENERICSBC) #0: Sun May 17 00:31:35 CDT 1998 scottr@beech:/usr/src/sys/arch/mac68k/compile/GENERICSBC mac68k


>Description:
An if-clause containing a conditional expression that returns a null or
non-null integer value works as expected if compiled with optimization off
but is always FALSE when compiled with optimization on.
>How-To-Repeat:
Create test1.c:

#include <stdio.h>
#define savechar(c,n) (--n > 0) ? (c) : '\0'
main()
{
    char buf[80];
    char *bufp = buf;
    int n = 80;
    if (!(*bufp++ = savechar('a', n)))
        puts("failed.");
    else
        puts("succeeded.");
}

% cc -o test1 test1.c
% test1
succeeded.
% cc -O -o test1 test1.c
% test1
failed.
>Fix:
    if (--n <= 0) {
        *bufp++ = '\0';
        puts("failed.");
    } else
        *bufp++ = 'a';
        puts("succeeded.");
    }	
>Audit-Trail:
>Unformatted: