cybersecurity · intermediate · ~15 min
Replace strcpy with a size-aware copy.
Implement int safe_copy(char *dst, int dstsz, const char *src) copying at most dstsz-1 bytes, always NUL-terminating, returning 0 if everything fit or -1 if truncated. Return -1 if dstsz <= 0.
int safe_copy(char *dst, int dstsz, const char *src) {
/* TODO */
return -1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.