~nytpu/tlsada

edbfb13611908bf9a58869a4cdd819bd0bcccff5 — nytpu 1 year, 11 months ago 3e2864a
TLS.Contexts.Read: Make compliant with Ada.Streams specification

Read previously would raise End_Error if no items were read, modify it to
instead properly set Last to Stream_Element_Offset'First when no items were
read.  Also fixes a potential bounds violation in the array offset calculation.
1 files changed, 9 insertions(+), 5 deletions(-)

M src/tls-contexts.adb
M src/tls-contexts.adb => src/tls-contexts.adb +9 -5
@@ 259,11 259,15 @@ package body TLS.Contexts is
			end case;
		end loop;

		-- Subtract one from Item'First since C is zero-indexed while Item
		-- isn't necessarily.
		Last := (Item'First - 1) + Stream_Element_Offset(size_t(Sz) / Stream_Element_Size);
		if Last < Item'First then
			raise End_Error;
		if Sz = 0 then
			Last := Stream_Element_Offset'First;
		elsif Sz < 0 then
			raise Device_Error with "unable to read data: " &
				Retrieve_Error_Message(Ctx.Context);
		else
			-- Subtract one from Item'First since C is zero-indexed while Item
			-- isn't necessarily.
			Last := (Item'First - 1) + Stream_Element_Offset(size_t(Sz) / Stream_Element_Size);
		end if;
	end Read;