cybersecurity · beginner · ~15 min

Safe append position

Check capacity before appending.

Challenge

Implement int append_pos(int curlen, int addlen, int cap) returning the offset to append at if curlen + addlen fits in cap (leaving room for a NUL: curlen + addlen < cap), else -1.

Starter code

int append_pos(int curlen, int addlen, int cap) {
    /* TODO */
    return -1;
}

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