Skip site navigation (1) Skip section navigation (2)

Re: BUG #5237: strange int->bit and bit->int conversions

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Postgres-Bugs <pgsql-bugs(at)postgresql(dot)org>, kononov(at)ftml(dot)net
Subject: Re: BUG #5237: strange int->bit and bit->int conversions
Date: 2009-12-12 19:17:48
Message-ID: 603c8f070912121117n7c181c83q69850647612bd08e@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-bugspgsql-hackers
Woops, forgot to copy the list.

On Sat, Dec 12, 2009 at 2:15 PM, Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
> On Sat, Dec 12, 2009 at 2:00 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> Roman Kononov <kononov(at)ftml(dot)net> writes:
>>> The bitfromint8() and bitfromint4() are hosed. They produce wrong
>>> results when the BIT size is more than 64 and 32 respectively, and the
>>> BIT size is not multiple of 8, and the most significant byte of the
>>> integer value is not 0x00 or 0xff.
>>
>> Hm, you're right, it's definitely busted ...
>>
>>> The patch re-implements the conversion functions.
>>
>> ... but I don't much care for this patch.  It's unreadable, uncommented,
>> and doesn't even try to follow Postgres coding conventions.
>>
>> It looks to me like the actual bug is that the "first fractional byte"
>> case ought to shift by destbitsleft - 8 not srcbitsleft - 8.  A
>> secondary problem (which your patch fails to fix) is that if the
>> compiler chooses to implement signed >> as zero-fill rather than
>> sign-bit-fill (which it is allowed to do per C99) then we'll get
>> wrong, or at least not the desired, results.  So I arrive at
>> the attached patch.
>
> I'm not sure this fixes it, although I haven't tested.  When we take
> the /* store first fractional byte */ branch, destbitsleft is between
> 1 and 7 bits greater than srcbitsleft.  We then subtract 8 from
> destbitsleft, and the comment on the next line now asserts that the
> two are equal.  That doesn't seem right.
>
> Also, I thought about the sign extension problem, but aren't we
> chopping those bits off anyway on the next line?
>
> ...Robert
>

In response to

Responses

pgsql-hackers by date

Next:From: Tom LaneDate: 2009-12-12 19:28:39
Subject: Re: BUG #5237: strange int->bit and bit->int conversions
Previous:From: Tom LaneDate: 2009-12-12 19:00:20
Subject: Re: BUG #5237: strange int->bit and bit->int conversions

pgsql-bugs by date

Next:From: Tom LaneDate: 2009-12-12 19:28:39
Subject: Re: BUG #5237: strange int->bit and bit->int conversions
Previous:From: Nagy DanielDate: 2009-12-12 19:08:03
Subject: Re: BUG #5238: frequent signal 11 segfaults

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group