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

From: Ranier Vilela <[email-masked]>
Date: Sat, 29 Jul 2023 09:37:19 -0300

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.

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

patch attached.

best regards,
Ranier Vilela

[1]
https://learn.microsoft.com/en-us/cpp/intrinsics/bitscanreverse-bitscanreverse64?view=msvc-170

---

From: John Naylor <[email-masked]>
Date: Sun, 30 Jul 2023 15:07:25 +0700

> 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.

> 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