Thread: Re: [PATCH] Missing Assert in the code

Re: [PATCH] Missing Assert in the code

From
Alvaro Herrera
Date:
Hello

On 2024-Nov-26, Dmitry Nikitin wrote:

> Monday, November 25, 2024, 10:51:31 PM, you wrote:
> 
> AH> Hmm, I think if we believe this to be really possible, we should have an
> AH> 'if/elog' test (or maybe a full ereport with ERRCODE_DATA_CORRUPTED
> AH> errcode) rather than an assertion.  I think the assertion adds nothing
> AH> of value here, but maybe an 'if' test would.

> I don't mind of 'if' however it's not clear for me what to follow on
> other (error) branch of that 'if'.

An elog(ERROR) doesn't continue execution of the function (it's a
longjmp to transaction abort and error recovery), so you don't have to
worry about that.

My point is that an Assert() is only executed in a debug build;
production builds don't have assertions.

> AH> Did you examine the other callers of PageGetMaxOffsetNumber()?  It's a
> AH> large bunch.

> Mostly it used in loops. So some code will be gracefully skipped and
> that's it.

Yeah, I noticed that in a few that I looked at, but it's over a hundred
of them.

> However the case discussed is different because -1 index of an array
> will be accessed. Which is much worse than a bare assertion at least.

Sure.

By the way, how did you find out about this problem?  Is this
hypothetical or was there an actual crash?  AFAICS (some of?) the
callers get to this code immediately after executing GinInitPage, so
it's not clear to me that there's an actual bug here.

... Ah, maybe ginFindLeafPage could be at risk, through ->isMoveRight.

-- 
Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/
"Porque Kim no hacía nada, pero, eso sí,
con extraordinario éxito" ("Kim", Kipling)



Re: [PATCH] Missing Assert in the code

From
Dmitry Nikitin
Date:
Hello Alvaro,

Tuesday, November 26, 2024, 2:40:39 PM, you wrote:

AH> By the way, how did you find out about this problem?  Is this
AH> hypothetical or was there an actual crash?  AFAICS (some of?) the
AH> callers get to this code immediately after executing GinInitPage, so
AH> it's not clear to me that there's an actual bug here.
Code analyzer. And we (our company) must somehow address this issue (a long story why).
I understand that sometimes some cases are pretty impossible because of the "business-logic" that
makes errors checking redundant. However sometimes that "business-logic" changes later on... So this
is a moment when debug asserts come to play and save a lot of time and sane.
Although I don't mind of elog/whatever. I'm just not that familiar with the "business-logic" to make
an appropriate choice.


--
Best regards,
 Dmitry                            mailto:pgsql-hackers@dima.nikitin.name