GCC char* bug

Geoff Keating geoffk@geoffk.org
Fri Jul 20 22:13:00 GMT 2001


Bruce Korb <bkorb@pacbell.net> writes:

> When the input character is 0xF5 and char is signed,
> the following code fails to compare equal:
> 
>         1257	static void test_for_changes PARAMS ((int));
>  	1258	static void
>  	1259	test_for_changes (read_fd)
>  	1260	  int read_fd;
> -	1261	{
> -	1262	  FILE *in_fp = fdopen (read_fd, "r");
> -	1263	  FILE *out_fp = (FILE *) NULL;
> -	1264	  char *pz_cmp = pz_curr_data;
>  	1265	
> 
> unsigned char *pz_cmp = (unsigned char*)pz_curr_data;
> 
>  	1266	#ifdef DO_STATS
>  	1267	  fixed_ct++;
>  	1268	#endif
> -	1269	  for (;;)
>  	1270	    {
> -	1271	      int ch;
>  	1272	
> -	1273	      ch = getc (in_fp);
> -	1274	      if (ch == EOF)
> -	1275	        break;
>  	1276	
> 
> ch &= 0xFF; /* bytes will only ever be 8 bits */
> 
>  	1277	      /*  IF we are emitting the output
>  	1278	          THEN emit this character, too.
>  	1279	      */
> -	1280	      if (out_fp != (FILE *) NULL)
> -	1281	        putc (ch, out_fp);
>  	1282	
>  	1283	      /*  ELSE if this character does not match the original,
>  	1284	          THEN now is the time to start the output.
>  	1285	      */
> -	1286	      else if (ch != *pz_cmp)
>  	1287	        {
> 	1288	          out_fp = create_file ();
> 
> Line 1286 compares "ch"(-10) with "*pz_cmp"(246) and says that they are
> not equal.  The workaround is obvious, but by golly chars
> are either signed or they are not signed.  If they are not
> signed, then the compare should show equal.  On the other
> hand, if they *are* signed, then 0xF5 should compare equal
> to 0xFFFFFFF5.  You cannot just do it one way sometimes
> and another way other times.

I'm not sure what code precisely you're reporting a bug about,
but if you write

char pz_cmp_star = -10;
int ch;

ch = getc (in_fp);
ch &= 0xFF;
if (ch != pz_cmp_star)
  abort();

you will always hit the abort() since '(int)x & 0xFF' is never
going to be -10.

-- 
- Geoffrey Keating <geoffk@geoffk.org>



More information about the Gcc-bugs mailing list