Re: Avoid undefined behavior with msvc compiler (src/include/port/pg_bitutils.h)

From: John Naylor <john(dot)naylor(at)enterprisedb(dot)com>
To: Ranier Vilela <ranier(dot)vf(at)gmail(dot)com>
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Avoid undefined behavior with msvc compiler (src/include/port/pg_bitutils.h)
Date: 2023-07-30 08:07:25
Message-ID: CAFBsxsFph6BoRB8PwZxbHQCx9+=hAcF9tk1_Xh=ichq7hMzC6A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, Jul 29, 2023 at 7:37 PM Ranier Vilela <ranier(dot)vf(at)gmail(dot)com> wrote:
>
> Hi,
>
> The pg_leftmost_one_pos32 function with msvc compiler,
> relies on Assert to guarantee the correct result.
>
> But msvc documentation [1] says clearly that
> undefined behavior can occur.

It seems that we should have "Assert(word != 0);" at the top, which matches
the other platforms anyway, so I'll add that.

> Fix by checking the result of the function to avoid
> a possible undefined behavior.

No other platform does this, and that is by design. I'm also not
particularly impressed by the unrelated cosmetic changes.

--
John Naylor
EDB: http://www.enterprisedb.com

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrey M. Borodin 2023-07-30 10:08:41 Re: UUID v7
Previous Message Nathan Bossart 2023-07-30 04:51:05 Re: Faster "SET search_path"