Bug#263854: gcc-3.3: Generates bad code
Package: gcc-3.3
Version: 1:3.3.4-3
Severity: normal
-- System Information:
Debian Release: 3.1
  APT prefers testing
  APT policy: (500, 'testing')
Architecture: i386 (i686)
Kernel: Linux 2.4.24-bf2.4
Locale: LANG=C, LC_CTYPE=C
Versions of packages gcc-3.3 depends on:
ii  binutils                   2.14.90.0.7-8 The GNU assembler, linker and bina
ii  cpp-3.3                    1:3.3.4-3     The GNU C preprocessor
ii  gcc-3.3-base               1:3.3.4-3     The GNU Compiler Collection (base 
ii  libc6                      2.3.2.ds1-13  GNU C Library: Shared libraries an
ii  libgcc1                    1:3.3.4-3     GCC support library
-- no debconf information
The following code compiles badly for the my_strncpy_bug function : the
if( *tmp ) ++ tmp; is never generated.
The difference between both loops is the use of an inside loop temp
variable.
Execution :
sco@elton:/tmp$ gcc -O2 -o y y.c
sco@elton:/tmp$ ./y
t1:My First string for this test ... t2:
t1:My First string for this test ... t2:My First string for this test
....
t1:My First string for this test ... t2:
t1:My First string for this test ...
t2:MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMBefore buggy
sco@elton:/tmp$
--begining of source code for y.c
#include <stdio.h>
char *my_strncpy_bug( char *dst, const char *src, unsigned int n ) {
	char *work = dst;
	
	while( n ) {
		const char *tmp = src;
		*work = *tmp;
		++ work;
		if( *tmp )
			++ tmp;
		-- n;
	}
	return dst;
}
char *my_strncpy( char *dst, const char *src, unsigned int n ) {
	char *work = dst;
	
	while( n ) {
		*work = *src;
		++ work;
		if( *src )
			++ src;
		-- n;
	}
	return dst;
}
int main( int argc, char **argv ) {
	char tmp1[] = "My First string for this test ...";
	char tmp_bug3[] = "Before buggy";
	char tmp_bug2[256] = { 0 };
	char tmp_nobug3[] = "Before no bug";
	char tmp_nobug2[256] = { 0 };
	printf( "t1:%s t2:%s\n", tmp1, tmp_nobug2 );
	my_strncpy( tmp_nobug2, tmp1, 256 );
	printf( "t1:%s t2:%s\n", tmp1, tmp_nobug2 );
	printf( "t1:%s t2:%s\n", tmp1, tmp_bug2 );
	my_strncpy_bug( tmp_bug2, tmp1, 256 );
	printf( "t1:%s t2:%s\n", tmp1, tmp_bug2 );
	return 0;
}
--end of source code for y.c
Reply to: