Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/pager.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ typedef enum {
PAGER_OK = 0,
PAGER_INVALID_ARGUMENT,
PAGER_IO_ERROR,
PAGER_OUT_OF_PAGES
PAGER_OUT_OF_PAGES,
PAGER_CORRUPT
} PagerResult;

typedef struct {
Expand Down
2 changes: 1 addition & 1 deletion src/database.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Table *table_open(const char *filename, SqliteResult *result)
PagerResult pager_result;
Pager *pager = pager_open(filename, &pager_result);
if (pager == NULL) {
*result = SQLITE_IO_ERROR;
*result = pager_result == PAGER_CORRUPT ? SQLITE_CORRUPT : SQLITE_IO_ERROR;
return NULL;
}

Expand Down
10 changes: 9 additions & 1 deletion src/pager.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

static size_t page_count_for_length(size_t file_length)
{
return (file_length + PAGE_SIZE - 1U) / PAGE_SIZE;
return file_length / PAGE_SIZE;
}

Pager *pager_open(const char *filename, PagerResult *result)
Expand Down Expand Up @@ -38,6 +38,12 @@ Pager *pager_open(const char *filename, PagerResult *result)
return NULL;
}

if ((size_t)length % PAGE_SIZE != 0U) {
fclose(file);
*result = PAGER_CORRUPT;
return NULL;
}

Pager *pager = calloc(1, sizeof(*pager));
if (pager == NULL) {
fclose(file);
Expand Down Expand Up @@ -154,6 +160,8 @@ const char *pager_result_string(PagerResult result)
return "I/O error";
case PAGER_OUT_OF_PAGES:
return "database exceeds maximum page count";
case PAGER_CORRUPT:
return "database file has a partial page";
default:
return "unknown error";
}
Expand Down
16 changes: 16 additions & 0 deletions tests/test_database.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,21 @@ static void test_corrupt_node_count_is_rejected(void)
remove(filename);
}

static void test_partial_page_database_is_reported_as_corrupt(void)
{
const char *filename = "build/test_partial_page.db";
FILE *file = fopen(filename, "wb");
assert(file != NULL);
assert(fputc(0, file) != EOF);
assert(fclose(file) == 0);

SqliteResult result = SQLITE_OK;
Table *table = table_open(filename, &result);
assert(table == NULL);
assert(result == SQLITE_CORRUPT);
remove(filename);
}

static void test_corrupt_metadata_page_is_rejected(void)
{
const char *filename = "build/test_corrupt_metadata.db";
Expand Down Expand Up @@ -271,6 +286,7 @@ int main(void)
test_validation();
test_corrupt_node_count_is_rejected();
test_corrupt_metadata_page_is_rejected();
test_partial_page_database_is_reported_as_corrupt();
test_corrupt_child_reference_is_rejected();
return 0;
}
28 changes: 28 additions & 0 deletions tests/test_pager.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,33 @@
#include <stdio.h>
#include <string.h>

static void write_file_of_size(const char *filename, size_t size)
{
unsigned char bytes[PAGE_SIZE + 1U] = {0};
FILE *file = fopen(filename, "wb");
assert(file != NULL);
assert(size <= sizeof(bytes));
assert(fwrite(bytes, 1U, size, file) == size);
assert(fclose(file) == 0);
}

static void test_partial_page_files_are_rejected(void)
{
const char *filename = "build/test_pager_partial.db";
const size_t invalid_sizes[] = {1U, PAGE_SIZE - 1U, PAGE_SIZE + 1U};

for (size_t i = 0; i < sizeof(invalid_sizes) / sizeof(invalid_sizes[0]); ++i) {
write_file_of_size(filename, invalid_sizes[i]);

PagerResult result = PAGER_OK;
Pager *pager = pager_open(filename, &result);
assert(pager == NULL);
assert(result == PAGER_CORRUPT);
}

remove(filename);
}

static void test_pager_round_trip(void)
{
const char *filename = "build/test_pager.db";
Expand Down Expand Up @@ -39,5 +66,6 @@ static void test_pager_round_trip(void)
int main(void)
{
test_pager_round_trip();
test_partial_page_files_are_rejected();
return 0;
}
Loading