]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
pg_mblen_range, pg_mblen_with_len: Valgrind after encoding ereport.
authorNoah Misch <noah@leadboat.com>
Sat, 14 Feb 2026 20:16:16 +0000 (12:16 -0800)
committerNoah Misch <noah@leadboat.com>
Sat, 14 Feb 2026 20:16:16 +0000 (12:16 -0800)
The prior order caused spurious Valgrind errors.  They're spurious
because the ereport(ERROR) non-local exit discards the pointer in
question.  pg_mblen_cstr() ordered the checks correctly, but these other
two did not.  Back-patch to v14, like commit
1e7fe06c10c0a8da9dd6261a6be8d405dc17c728.

Reviewed-by: Thomas Munro <thomas.munro@gmail.com>
Discussion: https://postgr.es/m/20260214053821.fa.noahmisch@microsoft.com
Backpatch-through: 14

src/backend/utils/mb/mbutils.c

index a5a734839aff29b797ce0ac2360258f756d18b6e..f3f94d46542c58cb233832c409ce089d6c7b11c6 100644 (file)
@@ -1086,15 +1086,16 @@ pg_mblen_range(const char *mbstr, const char *end)
        int                     length = pg_wchar_table[DatabaseEncoding->encoding].mblen((const unsigned char *) mbstr);
 
        Assert(end > mbstr);
+
+       if (unlikely(mbstr + length > end))
+               report_invalid_encoding_db(mbstr, length, end - mbstr);
+
 #ifdef VALGRIND_EXPENSIVE
        VALGRIND_CHECK_MEM_IS_DEFINED(mbstr, end - mbstr);
 #else
        VALGRIND_CHECK_MEM_IS_DEFINED(mbstr, length);
 #endif
 
-       if (unlikely(mbstr + length > end))
-               report_invalid_encoding_db(mbstr, length, end - mbstr);
-
        return length;
 }
 
@@ -1109,15 +1110,16 @@ pg_mblen_with_len(const char *mbstr, int limit)
        int                     length = pg_wchar_table[DatabaseEncoding->encoding].mblen((const unsigned char *) mbstr);
 
        Assert(limit >= 1);
+
+       if (unlikely(length > limit))
+               report_invalid_encoding_db(mbstr, length, limit);
+
 #ifdef VALGRIND_EXPENSIVE
        VALGRIND_CHECK_MEM_IS_DEFINED(mbstr, limit);
 #else
        VALGRIND_CHECK_MEM_IS_DEFINED(mbstr, length);
 #endif
 
-       if (unlikely(length > limit))
-               report_invalid_encoding_db(mbstr, length, limit);
-
        return length;
 }