cybersecurity · intermediate · ~15 min · safe pentest lab
Bounds-safe offset/length parsing of a record-style binary format.
Implement:
#include <stdint.h>
#include <stddef.h>
int read_mft_name(const uint8_t *rec, size_t n, char *out, size_t cap);
Layout:
'F','I','L','E'rec[name_offset], are name_length ASCII charsReturn:
out (excluding NUL) on success-1 if any input is NULL, cap == 0, n < 8, signature mismatch,
offset+length walks past n, or name would not fit in cap.end = (size_t)name_offset + (size_t)name_length.name_offset and name_length are 16-bit. Add
them as size_t to avoid integer overflow.Real MFT parsing is complex. The defensive shape — magic, bounds-checked offsets, bounded copy — is the same in the mock and the real format.
A byte buffer + its length, an output buffer + capacity.
Non-negative byte count, or -1 on any failure.
Signature must match. Cast to size_t before adding. NUL-terminate output.
#include <stdint.h>
#include <stddef.h>
int read_mft_name(const uint8_t *rec, size_t n, char *out, size_t cap) {
/* TODO */
(void)rec; (void)n; (void)out; (void)cap;
return -1;
}
Adding off + len as u16 (overflows). Forgetting that len == 0 is a legal empty name. Reading rec[4..7] when n < 8.
Name length 0. Offset right at the end of the buffer. NULL inputs.
O(len) per call.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.