Avoid overflow (src/backend/utils/adt/formatting.c)

Ranier Vilela <ranier.vf@gmail.com>

From: Ranier Vilela <ranier.vf@gmail.com>
To: Pg Hackers <pgsql-hackers@postgresql.org>
Date: 2025-11-02T14:59:35Z
Lists: pgsql-hackers

Attachments

Hi.

Per Coverity.

Coverity raised the follow report:

CID 1642824: (#1 of 1): Overflowed constant (INTEGER_OVERFLOW)
37. overflow_const: Expression pattern_len - 1ULL, where pattern_len is
known to be equal to 0, underflows the type of pattern_len - 1ULL, which is
type unsigned long long.

This is because the function *pg_mbstrlen* can return zero.
And the comment is clear, *just in case there are MB chars*.

patch attached.

best regards,
Ranier Vilela