json-ada icon indicating copy to clipboard operation
json-ada copied to clipboard

JSON_Value invalid after Parser out of scope

Open mosteo opened this issue 8 months ago • 0 comments
trafficstars

I found a situation that can be reproduced thus:

with JSON.Types;
with JSON.Parsers;

with Ada.Text_IO; use Ada.Text_IO;

procedure Json_Bug is

   package Types is new JSON.Types (Long_Long_Integer, Float);
   package Parsers is new JSON.Parsers (Types);

   ---------
   -- Get --
   ---------

   function Get return Types.JSON_Value is
      Parser : Parsers.Parser := Parsers.Create ("[1, 2, 3]");
   begin
      return Parser.Parse;
   end Get;
            
begin
   Put_Line (Get.Image);
end Json_Bug;

This fails with

raised CONSTRAINT_ERROR : Json_Bug.Types.Array_Vectors.Element: Index is out of range
[/home/jano/tmp/json_bug/bin/json_bug]
0x407f4d Json_Bug.Types.Array_Vectors.Element at a-convec.adb:607
0x40801d Json_Bug.Types.Constant_Reference.3 at json-types.adb:318
0x41974f Json_Bug.Types.Image_Array at json-types.adb:463
0x419588 Json_Bug.Types.Image at json-types.adb:495
0x41f31e Json_Bug at json_bug.adb:22
0x41f8a2 Main at b__json_bug.adb:318
[/lib/x86_64-linux-gnu/libc.so.6]
0x712ed402a1c8
0x712ed402a289

It boils down to the parser going out of scope before the value is used. Is this intended operation? Are values tied to their parser?

Thanks!

mosteo avatar Feb 25 '25 13:02 mosteo