You're a software engineer at RTX working on ground-side telemetry decoding for a sensor payload. Frames arrive over a serial link as raw bytes; a small C module validates each frame, decodes a big-endian sensor reading, accumulates a checksum recursively, and tracks the largest reading seen so far. In bench testing the decoder either hangs, crashes, or reports readings that don't match what the test rig actually sent.
#include <stdint.h>
#define FRAME_SIZE 8
#define IS_VALID_FRAME(f) f[0] == 0xAA && f[1] == 0x55
int32_t bytes_to_int32_be(uint8_t *bytes) {
return (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
}
int checksum_recursive(uint8_t *frame, int index) {
if (index == FRAME_SIZE) {
return 0;
}
return frame[index] + checksum_recursive(frame, index);
}
int32_t find_max_reading(int32_t *readings, int count) {
int32_t max = 0;
for (int i = 0; i < count; i++) {
if (readings[i] > max) {
max = readings[i];
}
}
return max;
}
void process_telemetry_frame(uint8_t *frame, int32_t *readings_out, int *count_out) {
if (IS_VALID_FRAME(frame) == 0) {
return;
}
int32_t reading = bytes_to_int32_be(&frame[2]);
readings_out[*count_out] = reading;
*count_out = *count_out + 1;
}
Find every bug in this program: there's a macro-expansion bug, a recursion bug, an endianness/casting bug, and a couple of logic bugs around the max-value and buffer tracking. Walk through each one, exactly how it manifests, and how you'd fix it.
Use audit_decoder(source_code) and return the findings in source order as a list of objects containing bug, manifestation, and fix. The supplied source text is the decoder shown above.
def audit_decoder(source_code):