cybersecurity · beginner · ~15 min

Would this write overflow?

Compare a write size against the buffer capacity.

Challenge

Decide whether a write would spill past the end of a fixed buffer — the check that prevents the classic overflow.

Task

Implement int will_overflow(int write_len, int buf_size) that returns 1 if writing write_len bytes into a buf_size buffer overflows it, else 0.

Input

  • write_len: number of bytes to be written.
  • buf_size: capacity of the destination buffer.

Output

Returns int: 1 if write_len > buf_size, else 0.

Example

will_overflow(20, 16)   ->   1
will_overflow(16, 16)   ->   0   (exact fit)
will_overflow(8, 16)    ->   0

Edge cases

  • An exact fit (write_len == buf_size) does not overflow.

Input format

Two ints: write_len and buf_size.

Output format

An int: 1 if write_len > buf_size, else 0.

Constraints

An exact fit is not an overflow.

Starter code

int will_overflow(int write_len, int buf_size) {
    /* TODO */
    return 0;
}

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