Skip to main content

Practice ยท 1 of 1

The Untrustworthy API

Implement defensive string/number helpers. The boilerplate declares: ``c /* copies at most cap-1 chars + '' from src to dst; returns copied char count, or -1 if dstcap == 0 / NULL args. NEVER reads more than src's length. */ int str_copy_bounded(char *dst, size_t dstcap, const char *src); /* parses a decimal in [lo, hi]; 0 ok, -1 bad syntax/out-of-range/ overflow (value must fit long via digit-count guard) */ int num_parse_bounded(const char *s, long lo, long hi, long *out); /* returns 1 if the n-byte region [p, p+n) is all-zero; 0 otherwise; 0 if p is NULL and n == 0, -1 if p is NULL and n > 0 */ int region_zero(const unsigned char *p, size_t n); ``

Difficulty: intermediate

Back to lesson: Practice: Defensive Gym