From 361af5f930f4bb49804a1378df3c3840f7f62a1c Mon Sep 17 00:00:00 2001 From: Yuya Watari Date: Fri, 9 Jun 2023 18:35:25 +0900 Subject: [PATCH v2 3/4] Fixed a bug where the result Bitmapset was incorrectly truncated If the result Bitmapset is longer than the other, we must not get rid of its trailing words because they have at least one non-zero word. --- src/backend/nodes/bitmapset.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/backend/nodes/bitmapset.c b/src/backend/nodes/bitmapset.c index aec7392cff..1839292d4b 100644 --- a/src/backend/nodes/bitmapset.c +++ b/src/backend/nodes/bitmapset.c @@ -319,8 +319,14 @@ bms_difference(const Bitmapset *a, const Bitmapset *b) lastnonzero = i; } while (++i < shortlen); - /* get rid of trailing zero words */ - result->nwords = lastnonzero + 1; + /* + * get rid of trailing zero words. + * + * We cannot do this if 'a' is longer than 'b' because 'a' can never be + * empty in such cases. + */ + if (shortlen == a->nwords) + result->nwords = lastnonzero + 1; Assert(result->nwords != 0); /* Need not check for empty result, since we handled that case above */ @@ -966,8 +972,11 @@ bms_del_members(Bitmapset *a, const Bitmapset *b) lastnonzero = i; } while (++i < shortlen); - /* If we computed an empty result, we must return NULL */ - if (lastnonzero == -1) + /* + * If we computed an empty result, we must return NULL. + * Note that 'a' can never be empty if it is longer than 'b'. + */ + if (lastnonzero == -1 && shortlen == a->nwords) { pfree(a); return NULL; -- 2.41.0.windows.1