Your question is Debug and Fix C Code. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
Teradata's batch loaders parse fixed-width customer transaction records out of a raw buffer before they're inserted into the warehouse. A junior engineer's first pass at this parser crashes intermittently on real production files.
#include <stdio.h>
#include <string.h>
void parse_record(char *buffer, char *out_customer_id, char *out_amount) {
char customer_id[10];
strcpy(customer_id, buffer);
char *amount_field = buffer + 10;
int amount = atoi(amount_field);
strcpy(out_customer_id, customer_id);
sprintf(out_amount, "%d", amount);
}
int process_batch(char **records, int count) {
int total = 0;
for (int i = 0; i <= count; i++) {
char customer_id[10];
char amount_str[20];
parse_record(records[i], customer_id, amount_str);
total += atoi(amount_str);
}
return total;
}
Identify all bugs in this C code and describe what problems each one causes. Explain what you'd change to make this safe to run against real production files, without necessarily rewriting every line.