Code RoomLogged IP address corrupted
MediumPrep Room Coding #1999

Logged IP address corrupted

Code reviewAlgorithms & data structuresMid–Senior~18 min

Review this C helper that formats an IPv4 address into a string for logging.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 18 min
Mark a line and say what kind of problem it is.0 findings
1const char *ip_to_str(uint32_t ip) {
2 char buf[16];
3 snprintf(buf, sizeof buf, "%u.%u.%u.%u",
4 (ip >> 24) & 0xff, (ip >> 16) & 0xff,
5 (ip >> 8) & 0xff, ip & 0xff);
6 return buf; // hand the formatted string back
7}
8 
9// caller:
10log_msg("src=%s", ip_to_str(pkt->src));
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.