Skip to content

Commit

Permalink
Replace use of mmap with plain read
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
msepga committed Dec 15, 2023
1 parent 06e7e62 commit 397ccc9
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions test/deparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 397ccc9

Please sign in to comment.