Skip to content

Commit

Permalink
Limit input buffer to 512
Browse files Browse the repository at this point in the history
No reason to have a larger input buffer than what the linux kernel can
take for kernel params, as that's the largest we would ever need to
support.
  • Loading branch information
jmbaur committed Jul 21, 2024
1 parent 9eef1d4 commit 5717c11
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
33 changes: 12 additions & 21 deletions src/console.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@ const Console = @This();

const CONSOLE = "/dev/char/5:1";

const IO_BUFFER_SIZE = 4096;

pub const IN = posix.STDIN_FILENO;

var out = std.io.bufferedWriter(std.io.getStdOut().writer());

arena: std.heap.ArenaAllocator = std.heap.ArenaAllocator.init(std.heap.page_allocator),
input_cursor: u16 = 0,
input_end: u16 = 0,
input_buffer: [IO_BUFFER_SIZE]u8 = undefined,
input_cursor: u9 = 0,
input_end: u9 = 0,
input_buffer: [std.math.maxInt(u9)]u8 = undefined,
context: ?*BootLoader = null,
tty: ?system.Tty = null,

Expand Down Expand Up @@ -122,14 +120,7 @@ pub fn handleStdin(self: *Console, boot_loaders: []*BootLoader) !?Event {
try self.prompt();
}

// We should only ever get 1 byte of data from stdin since we put the
// terminal in raw mode.
var buf = [_]u8{0};
if (try std.io.getStdIn().read(&buf) != 1) {
return null;
}

const char = buf[0];
const char = try std.io.getStdIn().reader().readByte();

var done = false;

Expand All @@ -154,7 +145,7 @@ pub fn handleStdin(self: *Console, boot_loaders: []*BootLoader) !?Event {
0x02 => b: {
if (self.input_cursor > 0) {
cursorLeft(1);
self.input_cursor -= 1;
self.input_cursor -|= 1;
break :b true;
}

Expand All @@ -176,7 +167,7 @@ pub fn handleStdin(self: *Console, boot_loaders: []*BootLoader) !?Event {
self.input_buffer[self.input_cursor .. self.input_end - 1],
self.input_buffer[self.input_cursor + 1 .. self.input_end],
);
self.input_end -= 1;
self.input_end -|= 1;
writeAll(self.input_buffer[self.input_cursor..self.input_end]);
eraseToEndOfLine();
cursorLeft(self.input_end - self.input_cursor);
Expand All @@ -199,7 +190,7 @@ pub fn handleStdin(self: *Console, boot_loaders: []*BootLoader) !?Event {
0x06 => b: {
if (self.input_cursor < self.input_end) {
cursorRight(1);
self.input_cursor += 1;
self.input_cursor +|= 1;
break :b true;
}

Expand All @@ -213,8 +204,8 @@ pub fn handleStdin(self: *Console, boot_loaders: []*BootLoader) !?Event {
self.input_buffer[self.input_cursor - 1 .. self.input_end - 1],
self.input_buffer[self.input_cursor..self.input_end],
);
self.input_cursor -= 1;
self.input_end -= 1;
self.input_cursor -|= 1;
self.input_end -|= 1;
cursorLeft(1);
writeAll(self.input_buffer[self.input_cursor..self.input_end]);
eraseToEndOfLine();
Expand Down Expand Up @@ -257,7 +248,7 @@ pub fn handleStdin(self: *Console, boot_loaders: []*BootLoader) !?Event {
&self.input_buffer[self.input_cursor],
);
cursorLeft(1);
self.input_cursor += 1;
self.input_cursor +|= 1;
writeAll(self.input_buffer[self.input_cursor - 2 .. self.input_cursor]);
break :b true;
}
Expand Down Expand Up @@ -290,9 +281,9 @@ pub fn handleStdin(self: *Console, boot_loaders: []*BootLoader) !?Event {
self.input_buffer[self.input_cursor..self.input_end],
);
self.input_buffer[self.input_cursor] = char;
self.input_end += 1;
self.input_end +|= 1;
writeAll(self.input_buffer[self.input_cursor..self.input_end]);
self.input_cursor += 1;
self.input_cursor +|= 1;
cursorLeft(self.input_end - self.input_cursor);
break :b true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/system.zig
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ pub fn setupTty(fd: posix.fd_t, mode: Tty.Mode) !Tty {
termios.cflag.CREAD = true;
termios.cflag.CLOCAL = true;

// http://www.unixwiz.net/techtips/termios-vmin-vtime.html
termios.cc[VMIN] = 0;
termios.cc[VTIME] = 50;
// https://www.unixwiz.net/techtips/termios-vmin-vtime.html
termios.cc[VMIN] = 0; // allow timeout with zero bytes obtained
termios.cc[VTIME] = 50; // 5-second timeout

setBaudRate(&termios, posix.speed_t.B3000000);
},
Expand Down

0 comments on commit 5717c11

Please sign in to comment.