Skip to main content

Load a Possibly-Corrupt Database

Challenge on lesson: Checkpoint: Trust Nothing on Disk

The checkpoint: a reader for the record store from this module's first challenge — hardened. The boilerplate declares: ``c /* same format as rs_*: "CJR1" | u16 count LE | count * 8-byte records (id:u8, qty:i32 LE, 3 pad). BUT the file may be truncated, corrupted, or missing. The reader must: - return -1 for any structural violation (missing/bad magic, count beyond file size, short records) - return the number of VALID records read into out (capacity cap), filling only records whose full 8 bytes exist - not allocate, not crash on any input */ long db_load(const char *path, unsigned char *out, size_t cap); `` out is a flat buffer of cap * 8 bytes; record i lives at out + i*8.

Difficulty: intermediate

Back to lesson: Checkpoint: Trust Nothing on Disk