Skip to content

Commit c61d51d

Browse files
peterealmkuznetsov
andcommitted
Detect buffer underflow in get_th()
Input with zero length can result in a buffer underflow when accessing *(num + (len - 1)), as (len - 1) would produce a negative index. Add an assertion for zero-length input to prevent it. This was found by ALT Linux Team. Reviewing the call sites shows that get_th() currently cannot be applied to an empty string: it is always called on a string containing a number we've just printed. Therefore, an assertion rather than a user-facing error message is sufficient. Co-authored-by: Alexander Kuznetsov <[email protected]> Discussion: https://www.postgresql.org/message-id/flat/[email protected]
1 parent df9133f commit c61d51d

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

src/backend/utils/adt/formatting.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,8 @@ get_th(char *num, int type)
15651565
int len = strlen(num),
15661566
last;
15671567

1568+
Assert(len > 0);
1569+
15681570
last = *(num + (len - 1));
15691571
if (!isdigit((unsigned char) last))
15701572
ereport(ERROR,

0 commit comments

Comments
 (0)