cybersecurity · intermediate · ~15 min
Implement SQL single-quote escaping safely.
Apply the SQL single-quote escaping rule — doubling each ' — into a bounded output buffer, as a last-resort defence when prepared statements aren't available.
Implement int escape_quotes(const char *in, char *out, int outsz) that copies in into out, replacing every single quote ' with two (''). NUL-terminate out and return the output length (excluding the NUL), or -1 if the result would not fit in outsz.
in: a fixed NUL-terminated string the grader provides.out: the destination buffer.outsz: the size of out in bytes.Returns the length of the escaped string (excluding the NUL), or -1 if it would overflow outsz.
escape_quotes("O'Brien", out, 32) -> 8, out = "O''Brien"
escape_quotes("a'b'c", out, 4) -> -1 (won't fit)
A fixed NUL-terminated string in, a destination out, and its size outsz.
The escaped length excluding the NUL, or -1 if it would not fit.
Double every single quote; NUL-terminate; return -1 before overflowing outsz.
int escape_quotes(const char *in, char *out, int outsz) {
/* TODO */
return -1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.