Check Zed.next_error index value is within the string length (#442)

This fixes Zed.Out_of_bound errors

Co-authored-by: Etienne Millon <me@emillon.org>
This commit is contained in:
Thibaut Mattio 2023-06-23 15:09:05 +02:00 committed by GitHub
parent 87871f442e
commit 351741b820
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 18 deletions

View File

@ -849,6 +849,9 @@ let prompt_comment = ref (S.const [| |])
module Private = struct
let fix_string str =
let len = String.length str in
if len = 0 then
str
else
let ofs, _, _ = Zed_utf8.next_error str 0 in
if ofs = len then
str
@ -858,6 +861,7 @@ module Private = struct
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);
@ -865,6 +869,8 @@ module Private = struct
loop ofs2
else
Buffer.contents buf
else
Buffer.contents buf
in
loop ofs
end

View File

@ -7,6 +7,7 @@ let test_fix_string =
in
( "fix_string"
, [ test ~name:"small" "x" ~expected:"x"
; test ~name:"empty" "" ~expected:""
]
)