From ebeba2d0e49021034d5847f79000f6f1a10406f2 Mon Sep 17 00:00:00 2001 From: msepga Date: Thu, 14 Dec 2023 20:18:19 -0500 Subject: [PATCH] Replace use of mmap with plain read We pass the file contents to the Postgres scanner, which assumes it is given a string as input. As such, it uses `strlen`. `mmap` doesn't provide us with a terminal null character, so we rely on empty pages to provide this. In the case of the `incremental_sort.sql` test, it is 12288 bytes, a multiple of exactly 3 of the typical 4096 page size. As such, this test ends up not getting *any* null terminators from `mmap`, so reading can overrun the buffer. Now, we just use a plain read and append `0` to the buffer to avoid the problem. --- test/deparse.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/deparse.c b/test/deparse.c index 396315fd..e03c5a44 100644 --- a/test/deparse.c +++ b/test/deparse.c @@ -120,7 +120,10 @@ int run_tests_from_file(const char * filename) { return EXIT_FAILURE; } fstat(fd, &sample_stat); - sample_buffer = mmap(0, sample_stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + + sample_buffer = malloc(sample_stat.st_size + 1); + read(fd, sample_buffer, sample_stat.st_size); + sample_buffer[sample_stat.st_size] = 0; if (sample_buffer == (void *) -1) { @@ -162,7 +165,7 @@ int run_tests_from_file(const char * filename) { pg_query_free_split_result(split_result); - munmap(sample_buffer, sample_stat.st_size); + free(sample_buffer); close(fd); return ret_code;