Re: Text search segmentation fault - Mailing list pgsql-general

From Gregory Stark
Subject Re: Text search segmentation fault
Date
Msg-id 87fxj2p04m.fsf@oxford.xeocode.com
Whole thread Raw
In response to Re: Text search segmentation fault  (Gregory Stark <stark@enterprisedb.com>)
Responses Re: Text search segmentation fault  (Grzegorz Jaśkiewicz <gryzman@gmail.com>)
List pgsql-general
Gregory Stark <stark@enterprisedb.com> writes:

> Teodor Sigaev <teodor@sigaev.ru> writes:
>
>> I reproduced the bug with a help of Grzegorz's point for 64-bit box. So, patch
>> is attached and I'm going to commit it
> ...
>
>> !     Conf->flagval[(unsigned int) *s] = (unsigned char) val;
> ...
>> !     Conf->flagval[*(unsigned char*) s] = (unsigned char) val;
>
> Maybe I'm missing something but I don't understand how this fixes the problem.

Ah, I understand how this fixes the problem. You were casting to unsigned
*int* not unsigned char so it was sign extending first and then overflowing.
So char<255> was coming out as MAX_INT instead of 255.

#include <stdio.h>

main()
{
  volatile signed char a = -1;
  printf("ud=%ud\n", (unsigned int)a);
}

$ ./a.out
ud=4294967295d


If you just make these all casts to (unsigned char) it should work just as
well as the pointer type punning -- and be a whole lot less scary.

> What really boggles me is why you don't just use unsigned chars everywhere and
> remove all of these casts. or would that just move the casts to strcmp and
> company?

It still seems to me if you put a few "unsigned" in variable declarations you
could remove piles upon piles of casts and make all of the code more readable.


--
  Gregory Stark
  EnterpriseDB          http://www.enterprisedb.com
  Ask me about EnterpriseDB's 24x7 Postgres support!

pgsql-general by date:

Previous
From: Teodor Sigaev
Date:
Subject: Re: Text search segmentation fault
Next
From: Grzegorz Jaśkiewicz
Date:
Subject: Re: Text search segmentation fault