cybersecurity · intermediate · ~12 min · safe pentest lab
Bounded text-token extraction with character allow-list.
Implement:
int parse_sip_method(const char *msg, char *out, size_t cap);
Copy the leading method token (everything up to the first space) into
out, NUL-terminating. Return the number of bytes written.
Return -1 on any of these:
msg or NULL out or cap == 0cap (need room for the NUL too)A-Z"INVITE sip:alice@... SIP/2.0\r\n", cap=8 → out="INVITE", return 6"REGISTER ...", cap=8 → out="REGISTER"? cap-1=7 chars + NUL doesn't fit → return -1"register sip:..." → -1 (lowercase)"INVALID\0..." (NUL before space) → -1for (i = 0; i < 16; ++i) if (msg[i] == ' ') break;
then check i < 16.SIP gateways live on the public internet. Rejecting malformed methods at the door is cheap; missing the check is expensive.
A NUL-terminated SIP message, a bounded output buffer, and its capacity.
Bytes written (positive int) on success, -1 on any failure.
Allow-list A-Z only. Reject if method >= 16 bytes (no method is that long).
#include <stddef.h>
int parse_sip_method(const char *msg, char *out, size_t cap) {
/* TODO */
(void)msg; (void)out; (void)cap;
return -1;
}
Forgetting room for the NUL. Allowing the empty method. Not capping the search at 16 bytes (potential read past a small buffer).
cap exactly len+1 (just fits). First byte is space → empty method → -1. NULL inputs.
O(1) — bounded loop over at most 16 bytes.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.