HeapCrash / Challenges / Serial GPS reader on Linux

Serial GPS reader on Linux

A Linux gateway reads NMEA sentences from a GPS on /dev/ttyS2 at 9600 baud. Most sentences parse. Some arrive cut in half, some arrive glued together, and on one board revision the program hangs at startup.

The code

Embedded Linux C. It compiles. There are 4 real bugs in it: the kind that pass a bench test and fail in the field.

int gps_open(void)
{
    int fd = open("/dev/ttyS2", O_RDWR);
    struct termios t;
    tcgetattr(fd, &t);
    cfsetispeed(&t, B9600);
    cfsetospeed(&t, B9600);
    tcsetattr(fd, TCSANOW, &t);
    return fd;
}

int gps_read_sentence(int fd, char *out, size_t cap)
{
    ssize_t n = read(fd, out, cap);
    if (n <= 0) return -1;
    out[n] = '\0';
    return (int)n;
}

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