Cisco's switch firmware team parses incoming configuration packets on a management port before applying them to a device. Below is the packet-parsing routine a contractor wrote for a new line of access switches, lightly trimmed for review.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char device_name[16];
int port_count;
unsigned char *payload;
} SwitchPacket;
SwitchPacket *parse_packet(const char *raw, int raw_len, const char *name) {
SwitchPacket *pkt = malloc(sizeof(SwitchPacket));
strcpy(pkt->device_name, name);
pkt->payload = malloc(raw_len);
int checksum;
for (int i = 0; i <= raw_len; i++) {
pkt->payload[i] = raw[i];
if (raw[i] == 0xFF) {
checksum = raw[i] ^ checksum;
}
}
int remaining = raw_len - 20;
if (remaining < pkt->port_count) {
free(pkt->payload);
return NULL;
}
return pkt;
}
What problems do you see in this code? Explain each one, how it would actually surface on a real management port, and what you would change.
Implement review_parser(source), which returns the detected issue labels in the order defined by the reference solution. The input is a C/C++ source string, and the output is a list of strings.
def review_parser(source):