HeapCrash / Challenges / Packing a log record

Packing a log record

A logger packs records into a byte buffer before writing them to flash. It ran fine on the Cortex-M4F dev board in debug builds. The release build hits a UsageFault on the same board, and the cost-reduced Cortex-M0+ board HardFaults on the first record.

The code

Embedded C for Cortex-M. It compiles. There are 3 real bugs in it: the kind that pass a bench test and fail in the field.

static uint8_t rec[32];

static void put_u32(uint8_t *p, uint32_t v)
{
    *(uint32_t *)p = v;
}

size_t pack_record(uint8_t type, uint32_t ts, uint64_t id, float val)
{
    rec[0] = type;
    put_u32(&rec[1], ts);
    *(uint64_t *)&rec[5] = id;
    *(float *)&rec[13] = val;
    return 17;
}

What an interviewer expects

The bugs, reference fixes and the follow-up question are in the practice app, after you've tried it.

Companies that test this

Matched on the skills each company's embedded roles emphasise.

Related challenges

Flag the lines, say what breaks, then fix it and build it with clang in your browser.

Find the bugs