nocfbot-0024-Tighten-the-RPR-frame-boundary-check-from-to-per-.txt

text/plain

Filename: nocfbot-0024-Tighten-the-RPR-frame-boundary-check-from-to-per-.txt
Type: text/plain
Part: 21
Message: Re: Row pattern recognition
From 381e30a5b89e088da66a7a60dde3239455b508d4 Mon Sep 17 00:00:00 2001
From: Henson Choi <assam258@gmail.com>
Date: Fri, 29 May 2026 19:07:40 +0900
Subject: [PATCH 24/26] Tighten the RPR frame-boundary check from >= to == per
 Jian He's review

currentPos advances by exactly one row per call, and a finalized context
is skipped by the states == NULL guard, so it can only ever reach
ctxFrameEnd, never overshoot it; >= and == behave identically here, and
== states the intent.  The >= was a defensive guard against an overshoot
that cannot happen -- move that defense into Assert(currentPos <=
ctxFrameEnd) so a future change that breaks the invariant fails
immediately instead of silently slipping past the boundary, and change
the comment from "exceeded" to "reached".  No behavior change.
---
 src/backend/executor/execRPR.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/backend/executor/execRPR.c b/src/backend/executor/execRPR.c
index 88c59cf3276..4463cfe0a5c 100644
--- a/src/backend/executor/execRPR.c
+++ b/src/backend/executor/execRPR.c
@@ -1706,7 +1706,7 @@ ExecRPRProcessRow(WindowAggState *winstate, int64 currentPos,
 		if (ctx->states == NULL)
 			continue;
 
-		/* Check frame boundary - finalize if exceeded */
+		/* Check frame boundary - finalize the context when it is reached */
 		if (hasLimitedFrame)
 		{
 			int64		ctxFrameEnd;
@@ -1716,9 +1716,18 @@ ExecRPRProcessRow(WindowAggState *winstate, int64 currentPos,
 									&ctxFrameEnd))
 				ctxFrameEnd = PG_INT64_MAX;
 
-			if (currentPos >= ctxFrameEnd)
+			/*
+			 * currentPos advances by exactly one per call, and a finalized
+			 * context is skipped by the states == NULL guard above, so it can
+			 * only ever reach ctxFrameEnd, never overshoot it.  The Assert
+			 * turns a future change that broke that invariant into an
+			 * immediate failure rather than a silent slip past the boundary.
+			 */
+			Assert(currentPos <= ctxFrameEnd);
+
+			if (currentPos == ctxFrameEnd)
 			{
-				/* Frame boundary exceeded: force mismatch */
+				/* Frame boundary reached: force mismatch */
 				nfa_match(winstate, ctx, NULL);
 				continue;
 			}
-- 
2.50.1 (Apple Git-155)