Skip to content

Commit 3c18d90

Browse files
committed
Null-terminate the output buffer of LZ4Stream_gets
LZ4Stream_gets did not null-terminate its output buffer. The callers expected the buffer to be null-terminated and passed it around to functions such as sscanf with unintended consequences. Author: Georgios Kokolatos <gkokolatos@pm.me> Reported-by: Alexander Lakhin <exclusion@gmail.com> Discussion: https://postgr.es/m/94ae9bca-5ebb-1e68-bb7b-4f32e89fefbe@gmail.com
1 parent d8c3106 commit 3c18d90

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/bin/pg_dump/compress_lz4.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,10 @@ LZ4Stream_read_internal(LZ4State *state, void *ptr, int ptrsize, bool eol_flag)
459459
if (!LZ4Stream_init(state, size, false /* decompressing */ ))
460460
return -1;
461461

462+
/* No work needs to be done for a zero-sized output buffer */
463+
if (size <= 0)
464+
return 0;
465+
462466
/* Verify that there is enough space in the outbuf */
463467
if (size > state->buflen)
464468
{
@@ -636,14 +640,20 @@ LZ4Stream_gets(char *ptr, int size, CompressFileHandle *CFH)
636640
LZ4State *state = (LZ4State *) CFH->private_data;
637641
int ret;
638642

639-
ret = LZ4Stream_read_internal(state, ptr, size, true);
643+
ret = LZ4Stream_read_internal(state, ptr, size - 1, true);
640644
if (ret < 0 || (ret == 0 && !LZ4Stream_eof(CFH)))
641645
pg_fatal("could not read from input file: %s", LZ4Stream_get_error(CFH));
642646

643647
/* Done reading */
644648
if (ret == 0)
645649
return NULL;
646650

651+
/*
652+
* Our caller expects the return string to be NULL terminated
653+
* and we know that ret is greater than zero.
654+
*/
655+
ptr[ret - 1] = '\0';
656+
647657
return ptr;
648658
}
649659

0 commit comments

Comments
 (0)