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. The null and empty-input cases below still apply.

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. Keep embedded zero and nonprinting bytes as token data.
- Return zero for an empty or all-space input.
- Use unsigned indexes. Do not decrement below zero, call strlen, allocate memory, or change the buffer.
- Reject the call if token_len is null, len is above 128, or buf is null while len is nonzero. Leave the caller’s previous output length unchanged on rejection.
