This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/13271] New: Wrong code with -O2 optimization - 3.2 only - 3.2.2 OK


// This problem is apparently fixed for 3.2.2 but I couldn't find a 
// report of a similar problem - and I tried quite hard. 
//
// Platform Redhat Linux 8 - "out of the box"
// This throws a segmentation fault when compiled with gcc 3.2, command line:
// gcc -O2 -o getToken getToken.c
// run with: ./getToken anytextyoulike
// Code below is the simplest I could make it. E.g. remove just one of the
// tests in while() and it works OK. Also OK with -O1.
// The code itself is derived from a function that has been in use for many 
// years on the Windows platform and I am near as sure as possible is correct
//
// **text gets corrupted second time around the loop
//
// Its apparently fixed for 3.2.2 but I couldn't find a report of a similar 
// problem - and I tried quite hard. 
/* Here is an excerpt from the -S assembler output
	movb	-1(%edx), %al
	testb	%al, %al
	je	.L10
	cmpb	$32, %al
	je	.L10
	cmpb	$9, %al
	je	.L10
	cmpb	$13, %al
	je	.L10
	cmpb	$10, %al
	je	.L10
	cmpl	$1022, %esi
	jg	.L10
	.loc 1 24 0
	movb	%al, -1048(%esi,%ebp)
	.loc 1 26 0
// **text is in eax but it gets trashed here
	movl	%edx, %eax
	movl	%edx, (%edi)
	.loc 1 25 0
	incl	%esi
	.loc 1 26 0
	leal	2(%ecx), %edx
	movl	%eax, %ecx
	jmp	.L9
*/

#include <stdio.h>
#include <string.h>

char * getToken(const char **text)
{
    char buf[1024],
         *result ;


    int i = 0 ;

    if (**text=='*')
    {   
        (*text)++ ;
    }
    else
    {
	    while(**text && **text!=' ' && **text!='\t' && **text!='\r' && 
**text!='\n' && i<1023)
	    {
	        buf[i] = **text ;
	        i++ ;
	        (*text)++ ;
	    }
	
	    buf[i]=0 ;
	}
	
    result = strdup(buf) ;
    return result ;

}

int main(int argc, char **argv)
{
    printf("Result: %s\n", getToken((const char**)&argv[1])) ;
    return 0 ;
}

-- 
           Summary: Wrong code with -O2 optimization - 3.2 only - 3.2.2 OK
           Product: gcc
           Version: 3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: jrw at catena dot uk dot com
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13271


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]