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:
parent
87871f442e
commit
351741b820
|
@ -849,23 +849,29 @@ let prompt_comment = ref (S.const [| |])
|
||||||
module Private = struct
|
module Private = struct
|
||||||
let fix_string str =
|
let fix_string str =
|
||||||
let len = String.length str in
|
let len = String.length str in
|
||||||
let ofs, _, _ = Zed_utf8.next_error str 0 in
|
if len = 0 then
|
||||||
if ofs = len then
|
|
||||||
str
|
str
|
||||||
else begin
|
else
|
||||||
let buf = Buffer.create (len + 128) in
|
let ofs, _, _ = Zed_utf8.next_error str 0 in
|
||||||
if ofs > 0 then Buffer.add_substring buf str 0 ofs;
|
if ofs = len then
|
||||||
let rec loop ofs =
|
str
|
||||||
Zed_utf8.add buf (Uchar.of_char str.[ofs]);
|
else begin
|
||||||
let ofs1 = ofs + 1 in
|
let buf = Buffer.create (len + 128) in
|
||||||
let ofs2, _, _ = Zed_utf8.next_error str ofs1 in
|
if ofs > 0 then Buffer.add_substring buf str 0 ofs;
|
||||||
if ofs1 < ofs2 then
|
let rec loop ofs =
|
||||||
Buffer.add_substring buf str ofs1 (ofs2 - ofs1);
|
Zed_utf8.add buf (Uchar.of_char str.[ofs]);
|
||||||
if ofs2 < len then
|
let ofs1 = ofs + 1 in
|
||||||
loop ofs2
|
if ofs1 < len then
|
||||||
else
|
let ofs2, _, _ = Zed_utf8.next_error str ofs1 in
|
||||||
Buffer.contents buf
|
if ofs1 < ofs2 then
|
||||||
in
|
Buffer.add_substring buf str ofs1 (ofs2 - ofs1);
|
||||||
loop ofs
|
if ofs2 < len then
|
||||||
end
|
loop ofs2
|
||||||
|
else
|
||||||
|
Buffer.contents buf
|
||||||
|
else
|
||||||
|
Buffer.contents buf
|
||||||
|
in
|
||||||
|
loop ofs
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,6 +7,7 @@ let test_fix_string =
|
||||||
in
|
in
|
||||||
( "fix_string"
|
( "fix_string"
|
||||||
, [ test ~name:"small" "x" ~expected:"x"
|
, [ test ~name:"small" "x" ~expected:"x"
|
||||||
|
; test ~name:"empty" "" ~expected:""
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue