Skip to content

Commit

Permalink
Add safety checks to ensure we don't overwrite the root block
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Nov 27, 2024
1 parent 6453928 commit 9ec4200
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions dependencies/lmdb/libraries/liblmdb/mdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -2820,7 +2820,11 @@ mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp)
mop[--i] = 0;
if (env->me_freelist_written_start > i || !env->me_freelist_written_start)
env->me_freelist_written_start = i;
goto search_done;
if (pgno == 0) {
fprintf(stderr, "found freespace entry pointing to root block of size %u at %u\n", block_size, i);
mdb_midl_print(stderr, mop);
} else
goto search_done;
} else if (block_size < best_fit_size || best_fit_size == 0) {
best_fit_start = i - 1;
best_fit_size = block_size;
Expand Down Expand Up @@ -2957,12 +2961,12 @@ mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp)
}
goto fail;
} else {
if (key.mv_size == 0) {
fprintf(stderr, "Invalid zero size key\n");
rc = MDB_BAD_VALSIZE;
goto fail;
}
}
if (key.mv_size == 0) {
fprintf(stderr, "Invalid zero size key\n");
rc = MDB_BAD_VALSIZE;
goto fail;
}
}
last = *(txnid_t *) key.mv_data;
}
if (rc == MDB_NOTFOUND) break;
Expand Down Expand Up @@ -3017,13 +3021,18 @@ mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp)
// for sequential writing (faster)
env->me_freelist_position = -best_fit_start;
pgno = mop[++best_fit_start];
mop[best_fit_start] += num; // update position
if (env->me_freelist_written_end < best_fit_start) env->me_freelist_written_end = best_fit_start;
//fprintf(stderr, "\nusing best fit size %u of %u\n", num, best_fit_size);
//env->me_block_size_cache[best_fit_size] = 0; // clear this out of the cache (TODO: could move it)
if (pgno == 0) {
fprintf(stderr, "found best-fit freespace entry pointing to root block of size %u at %u\n", best_fit_size, best_fit_start);
mdb_midl_print(stderr, mop);
} else {
mop[best_fit_start] += num; // update position
if (env->me_freelist_written_end < best_fit_start) env->me_freelist_written_end = best_fit_start;
//fprintf(stderr, "\nusing best fit size %u of %u\n", num, best_fit_size);
//env->me_block_size_cache[best_fit_size] = 0; // clear this out of the cache (TODO: could move it)

i = 1; // indicate that we found something
goto search_done;
i = 1; // indicate that we found something
goto search_done;
}
}
/* Use new pages from the map when nothing suitable in the freeDB */
i = 0;
Expand Down Expand Up @@ -3283,13 +3292,13 @@ fprintf(stderr, "mdb_page_touch error\n");

#ifdef _WIN32
uint64_t get_time64() {
return GetTickCount64();
return GetTickCount64();
}
#else
uint64_t get_time64() {
struct timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
return time.tv_sec * 1000000000ll + time.tv_nsec;
struct timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
return time.tv_sec * 1000000000ll + time.tv_nsec;
}
#endif

Expand Down

0 comments on commit 9ec4200

Please sign in to comment.