From 2081902e0a722e8e4a0b8132aae380887d71d33e Mon Sep 17 00:00:00 2001 From: Pavel Borisov Date: Mon, 28 Mar 2022 16:23:28 +0400 Subject: [PATCH] Fix text_substring for correct processing of zero or negative start position It is expected that zero or negative start position should default to 1, and string end to be caclulated accordingly. Probably this change is a valid addition to recent 4bd3fad80e5c and is proposed to be backpatched into all versions since v11 which is mentioned commit committed to. --- src/backend/utils/adt/varlena.c | 4 ++-- src/test/regress/expected/strings.out | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 22ab5a4329f..59ab32a0ad5 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -890,7 +890,7 @@ text_substring(Datum str, int32 start, int32 length, bool length_not_specified) errmsg("negative substring length not allowed"))); L1 = -1; /* silence stupider compilers */ } - else if (pg_add_s32_overflow(S, length, &E)) + else if (pg_add_s32_overflow(S1, length, &E)) { /* * L could be large enough for S + L to overflow, in which case @@ -953,7 +953,7 @@ text_substring(Datum str, int32 start, int32 length, bool length_not_specified) errmsg("negative substring length not allowed"))); slice_size = L1 = -1; /* silence stupider compilers */ } - else if (pg_add_s32_overflow(S, length, &E)) + else if (pg_add_s32_overflow(S1, length, &E)) { /* * L could be large enough for S + L to overflow, in which case diff --git a/src/test/regress/expected/strings.out b/src/test/regress/expected/strings.out index 0f95b9400b6..afb119d0355 100644 --- a/src/test/regress/expected/strings.out +++ b/src/test/regress/expected/strings.out @@ -1810,10 +1810,10 @@ insert into toasttest values(repeat('1234567890',10000)); SELECT substr(f1, -1, 5) from toasttest; substr -------- - 123 - 123 - 123 - 123 + 12345 + 12345 + 12345 + 12345 (4 rows) -- If the length is less than zero, an ERROR is thrown. -- 2.24.3 (Apple Git-128)