Skip to content

Commit

Permalink
Check Zed.next_error index value is within the string length
Browse files Browse the repository at this point in the history
This fixes Zed.Out_of_bound errors
  • Loading branch information
tmattio committed Jun 23, 2023
1 parent fc32fa3 commit 9585749
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions src/lib/uTop_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -413,25 +413,31 @@ end

let fix_string str =
let len = String.length str in
let ofs, _, _ = Zed_utf8.next_error str 0 in
if ofs = len then
if len = 0 then
str
else begin
let buf = Buffer.create (len + 128) in
if ofs > 0 then Buffer.add_substring buf str 0 ofs;
let rec loop ofs =
Zed_utf8.add buf (Uchar.of_char str.[ofs]);
let ofs1 = ofs + 1 in
let ofs2, _, _ = Zed_utf8.next_error str ofs1 in
if ofs1 < ofs2 then
Buffer.add_substring buf str ofs1 (ofs2 - ofs1);
if ofs2 < len then
loop ofs2
else
Buffer.contents buf
in
loop ofs
end
else
let ofs, _, _ = Zed_utf8.next_error str 0 in
if ofs = len then
str
else begin
let buf = Buffer.create (len + 128) in
if ofs > 0 then Buffer.add_substring buf str 0 ofs;
let rec loop ofs =
Zed_utf8.add buf (Uchar.of_char str.[ofs]);
let ofs1 = ofs + 1 in
if ofs1 < len then
let ofs2, _, _ = Zed_utf8.next_error str ofs1 in
if ofs1 < ofs2 then
Buffer.add_substring buf str ofs1 (ofs2 - ofs1);
if ofs2 < len then
loop ofs2
else
Buffer.contents buf
else
Buffer.contents buf
in
loop ofs
end

let render_out_phrase term string =
if String.length string >= 100 * 1024 then
Expand Down

0 comments on commit 9585749

Please sign in to comment.