8000 Fix several one-byte buffer over-reads in to_number · patchsoft/postgres@04cee8f · GitHub
[go: up one dir, main page]

Skip to content

Commit 04cee8f

Browse files
committed
Fix several one-byte buffer over-reads in to_number
Several places in NUM_numpart_from_char(), which is called from the SQL function to_number(text, text), could accidentally read one byte past the end of the input buffer (which comes from the input text datum and is not null-terminated). 1. One leading space character would be skipped, but there was no check that the input was at least one byte long. This does not happen in practice, but for defensiveness, add a check anyway. 2. Commit 4a3a1e2 apparently accidentally doubled that code that skips one space character (so that two spaces might be skipped), but there was no overflow check before skipping the second byte. Fix by removing that duplicate code. 3. A logic error would allow a one-byte over-read when looking for a trailing sign (S) placeholder. In each case, the extra byte cannot be read out directly, but looking at it might cause a crash. The third item was discovered by Piotr Stefaniak, the first two were found and analyzed by Tom Lane and Peter Eisentraut.
1 parent 4da812f commit 04cee8f

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/backend/utils/adt/formatting.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4173,12 +4173,12 @@ NUM_numpart_from_char(NUMProc *Np, int id, int input_len)
41734173
(id == NUM_0 || id == NUM_9) ? "NUM_0/9" : id == NUM_DEC ? "NUM_DEC" : "???");
41744174
#endif
41754175

4176-
if (*Np->inout_p == ' ')
4177-
Np->inout_p++;
4178-
41794176
#define OVERLOAD_TEST (Np->inout_p >= Np->inout + input_len)
41804177
#define AMOUNT_TEST(_s) (input_len-(Np->inout_p-Np->inout) >= _s)
41814178

4179+
if (OVERLOAD_TEST)
4180+
return;
4181+
41824182
if (*Np->inout_p == ' ')
41834183
Np->inout_p++;
41844184

@@ -4316,7 +4316,7 @@ NUM_numpart_from_char(NUMProc *Np, int id, int input_len)
43164316
* next char is not digit
43174317
*/
43184318
if (IS_LSIGN(Np->Num) && isread &&
4319-
(Np->inout_p + 1) <= Np->inout + input_len &&
4319+
(Np->inout_p + 1) < Np->inout + input_len &&
43204320
!isdigit((unsigned char) *(Np->inout_p + 1)))
43214321
{
43224322
int x;

0 commit comments

Comments
 (0)
0