Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers/slipdev: fix off-by-one error in _recv() #18229

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 30 additions & 36 deletions drivers/slipdev/slipdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,52 +162,46 @@ static int _send(netdev_t *netdev, const iolist_t *iolist)
return bytes;
}

static void _drop_frame(slipdev_t *dev)
{
int byte;
do {
byte = tsrb_get_one(&dev->inbuf);
} while (byte > 0 && byte != SLIPDEV_END);
}

static int _recv(netdev_t *netdev, void *buf, size_t len, void *info)
{
slipdev_t *dev = (slipdev_t *)netdev;
int res = 0;

(void)info;
if (buf == NULL) {
if (len > 0) {
/* remove data */
for (; len > 0; len--) {
int byte = tsrb_get_one(&dev->inbuf);
if ((byte == (int)SLIPDEV_END) || (byte < 0)) {
/* end early if end of packet or ringbuffer is reached;
* len might be larger than the actual packet */
break;
}
}
} else {
/* the user was warned not to use a buffer size > `INT_MAX` ;-) */
res = (int)tsrb_avail(&dev->inbuf);
}
return (int)tsrb_avail(&dev->inbuf);
}
else {
int byte;
bool escaped = false;
uint8_t *ptr = buf;

do {
int tmp;
int byte = 0;
bool escaped = false;
uint8_t *ptr = buf;

do {
int tmp;

if ((unsigned)res == len) {
/* clear out unreceived packet */
_drop_frame(dev);
return -ENOBUFS;
}

if ((byte = tsrb_get_one(&dev->inbuf)) < 0) {
/* something went wrong, return error */
return -EIO;
}
tmp = slipdev_unstuff_readbyte(ptr, byte, &escaped);
ptr += tmp;
res += tmp;
} while (byte != SLIPDEV_END);

if ((byte = tsrb_get_one(&dev->inbuf)) < 0) {
/* something went wrong, return error */
return -EIO;
}
tmp = slipdev_unstuff_readbyte(ptr, byte, &escaped);
ptr += tmp;
res += tmp;
if ((unsigned)res > len) {
while (byte != SLIPDEV_END) {
/* clear out unreceived packet */
byte = tsrb_get_one(&dev->inbuf);
}
return -ENOBUFS;
}
} while (byte != SLIPDEV_END);
}
return res;
}

Expand Down