Q054FreeFirmware
Measure the final UART command token
Interview prompt
Question
Measure the last token in an explicitly sized UART command buffer after ignoring trailing ASCII space bytes. Nonnull pointers refer to valid storage for their stated extents. The token_len output object must not overlap the input byte span, and the input is immutable during the call. These storage preconditions preserve the explicit null/empty-input cases below.

Candidate starting point
Implementation scaffold
#include <stddef.h>
#include <stdint.h>
typedef enum { TOKEN_OK, TOKEN_EINVAL } token_rc_t;
token_rc_t last_token_len(const uint8_t *buf, size_t len,
size_t *token_len) {
// Implement here: validate arguments, find the final bounded token,
// and publish its length only after complete success.
}
Reviewed example
Trace one case
Input
buffer bytes="run test ", explicit_length=11Expected output
last_token_length=4 ("test")Trailing ASCII spaces are skipped, then the scan stops at the separator before test without relying on a terminator.
What to cover
Requirements
- Treat only byte 0x20 as a separator; embedded zero and nonprinting bytes remain token data.
- Return zero for an empty or all-space input.
- Scan with unsigned indexes without decrementing below zero, calling strlen, allocating, or mutating the buffer.
- Reject a null output, length above 128, or null nonempty input without changing the caller's old length.
