Skip to content

Commit 737ee07

Browse files
VA05-011: Make lsp_client resilient to invalid json
Add a custom exception and it's handler.
1 parent bb7d6ec commit 737ee07

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

source/client/lsp-raw_clients.adb

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ with Spawn.Processes; use Spawn.Processes;
2929

3030
package body LSP.Raw_Clients is
3131

32+
Parse_Exception : exception;
33+
-- Local exception raised when parsing invalid data
34+
3235
New_Line : constant String :=
3336
(Ada.Characters.Latin_1.CR, Ada.Characters.Latin_1.LF);
3437

@@ -297,7 +300,7 @@ package body LSP.Raw_Clients is
297300
Content_Length := Positive'Value (Buffer (From .. Next - 1));
298301
end;
299302
else
300-
raise Constraint_Error with "Unexpected header:" & Buffer;
303+
raise Parse_Exception with "Unexpected header:" & Buffer;
301304
end if;
302305

303306
Next := Next + New_Line'Length;
@@ -325,12 +328,24 @@ package body LSP.Raw_Clients is
325328
Start := Index (Client.Buffer, New_Line & New_Line);
326329

327330
if Start /= 0 then
328-
Parse_Headers
329-
(Slice (Client.Buffer, 1, Start + 1),
330-
Client.To_Read);
331-
332-
Delete
333-
(Client.Buffer, 1, Start + 2 * New_Line'Length - 1);
331+
begin
332+
Parse_Headers
333+
(Slice (Client.Buffer, 1, Start + 1),
334+
Client.To_Read);
335+
Delete
336+
(Client.Buffer, 1, Start + 2 * New_Line'Length - 1);
337+
exception
338+
when E : Parse_Exception =>
339+
Client.On_Exception (E);
340+
-- Delete the first line
341+
Start :=
342+
Index (Client.Buffer,
343+
"" & Ada.Characters.Latin_1.LF);
344+
Delete
345+
(Client.Buffer, 1, Start);
346+
-- Reset start to 0 => we didn't find a header
347+
Start := 0;
348+
end;
334349
end if;
335350
end if;
336351

0 commit comments

Comments
 (0)