cybersecurity · intermediate · ~15 min · safe pentest lab

Read stays within input?

Validate every slice against the fuzz input.

Challenge

Validate that a slice read stays inside the fuzzer's buffer, so the fuzzer can't trigger an out-of-bounds read.

Task

Implement int read_within(int offset, int len, int size) that returns 1 if reading len bytes starting at offset stays within size, else 0.

The read is in bounds when offset >= 0, len >= 0, and offset + len <= size.

Input

  • offset: the start position of the read.
  • len: the number of bytes to read.
  • size: the total buffer length.

Output

Returns int: 1 if the read is fully in bounds, else 0.

Example

read_within(4, 8, 16)    ->   1
read_within(12, 8, 16)   ->   0   (12 + 8 > 16)
read_within(-1, 4, 16)   ->   0   (negative offset)

Edge cases

  • A negative offset or len is out of bounds (return 0).
  • A read ending exactly at size is in bounds.

Rules

  • Check all three conditions; this is the per-slice bounds check a fuzz harness applies.

Input format

Three ints: offset, len, and the buffer size.

Output format

An int: 1 if the slice is fully in bounds, else 0.

Constraints

In bounds iff offset>=0, len>=0, and offset+len<=size.

Starter code

int read_within(int offset, int len, int size) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.