Skip to content

Commit

Permalink
Implement bounds checking of TTY RX buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
alanjian85 committed Jul 4, 2024
1 parent 3f154a8 commit 5033069
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/drivers/ldisc.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@ void ldisc_recv_byte(u32 num, u8 byte) {
switch (byte) {
case '\x7F':
if (ctrl_blk->cursor_pos > 0) {
if (tty_ctrl_blk->rx_tail == 0) {
if (tty_ctrl_blk->rx_tail == 0)
tty_ctrl_blk->rx_tail = TTY_BUF_SIZE;
}
tty_ctrl_blk->rx_tail--;
ctrl_blk->cursor_pos--;
ldisc_write(num, (const u8 *) "\b \b", 3);
}
break;
default:
tty_ctrl_blk->rx_buf[tty_ctrl_blk->rx_tail++] = byte;
tty_ctrl_blk->rx_tail %= TTY_BUF_SIZE;
default: {
usize next_tail = (tty_ctrl_blk->rx_tail + 1) % TTY_BUF_SIZE;
if (next_tail == tty_ctrl_blk->rx_head)
break;
tty_ctrl_blk->rx_buf[tty_ctrl_blk->rx_tail] = byte;
tty_ctrl_blk->rx_tail = next_tail;
ctrl_blk->cursor_pos++;
if (byte == '\n') {
sem_signaln(&tty_ctrl_blk->rx_sem, ctrl_blk->cursor_pos);
Expand All @@ -78,6 +80,7 @@ void ldisc_recv_byte(u32 num, u8 byte) {
ldisc_write(num, &byte, 1);
break;
}
}
}

isize ldisc_write(u32 num, const u8 *buf, usize size) {
Expand Down

0 comments on commit 5033069

Please sign in to comment.