@@ -32,6 +32,8 @@ pub const MsgBuffer = struct {
3232 buf : []u8 ,
3333 pos : usize = 0 ,
3434
35+ const MaxSize = 1024 * 1024 ; // 1MB
36+
3537 pub fn init (alloc : std.mem.Allocator , size : usize ) std.mem.Allocator.Error ! MsgBuffer {
3638 const buf = try alloc .alloc (u8 , size );
3739 return .{ .buf = buf };
@@ -93,15 +95,20 @@ pub const MsgBuffer = struct {
9395 // get the new position of the cursor
9496 const new_pos = self .pos + _input .len ;
9597
98+ // check max limit size
99+ if (new_pos > MaxSize ) {
100+ return error .MsgTooBig ;
101+ }
102+
96103 // check if the current input can fit in MsgBuffer
97104 if (new_pos > self .buf .len ) {
98105 // we want to realloc at least:
99- // - a size equals to new_pos to fit the entire input
106+ // - a size big enough to fit the entire input (ie. new_pos)
100107 // - a size big enough (ie. current size + starting size)
101108 // to avoid multiple reallocation
102- const max_size = @max (self .buf .len + self .size , new_pos );
109+ const new_size = @max (self .buf .len + self .size , new_pos );
103110 // resize the MsgBuffer to fit
104- self .buf = try alloc .realloc (self .buf , max_size );
111+ self .buf = try alloc .realloc (self .buf , new_size );
105112 }
106113
107114 // copy the current input into MsgBuffer
0 commit comments