Skip to content

Commit

Permalink
Fix incorrect free calls in FTP/HTTP responses
Browse files Browse the repository at this point in the history
The memory is inside the object and has to be freed using destroy instead.

New FTP test/example (to be used in RosettaCode.org)
  • Loading branch information
mgrojo committed May 31, 2024
1 parent 52ca4a2 commit 2462019
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 19 deletions.
2 changes: 1 addition & 1 deletion doc/docs/sf__network__ftp___listingresponse___spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ GNATdoc.Documentation = {
"children": [
{
"kind": "span",
"text": "Index of the name to get (in range [0 .. getCount])\n"
"text": "Index of the name to get (in range [0 .. getCount-1])\n"
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion doc/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
GNATdoc.Index = {
"project": "ASFML",
"timestamp": "2024-05-25 18:48:10"
"timestamp": "2024-05-31 22:38:31"
};
2 changes: 1 addition & 1 deletion doc/srcs/sf-network-ftp.ads.js
Original file line number Diff line number Diff line change
Expand Up @@ -5719,7 +5719,7 @@ GNATdoc.SourceFile = {
{
"kind": "span",
"cssClass": "comment",
"text": "--/ @param index Index of the name to get (in range [0 .. getCount])"
"text": "--/ @param index Index of the name to get (in range [0 .. getCount-1])"
}
]
},
Expand Down
15 changes: 5 additions & 10 deletions include/sf-network-ftp.adb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ package body Sf.Network.Ftp is
function GetMessage (FtpListingResponse : sfFtpListingResponse_Ptr) return String is
function Internal (FtpListingResponse : sfFtpListingResponse_Ptr) return chars_ptr;
pragma Import (C, Internal, "sfFtpListingResponse_getMessage");
Temp : chars_ptr := Internal (FtpListingResponse);
Temp : constant chars_ptr := Internal (FtpListingResponse);
R : constant String := Value (Temp);
begin
Free (Temp);
return R;
end GetMessage;

Expand All @@ -62,10 +61,9 @@ package body Sf.Network.Ftp is
function GetName (FtpListingResponse : sfFtpListingResponse_Ptr; Index : sfSize_t) return String is
function Internal (FtpListingResponse : sfFtpListingResponse_Ptr; Index : sfSize_t) return chars_ptr;
pragma Import (C, Internal, "sfFtpListingResponse_getName");
Temp : chars_ptr := Internal (FtpListingResponse, Index);
Temp : constant chars_ptr := Internal (FtpListingResponse, Index);
R : constant String := Value (Temp);
begin
Free (Temp);
return R;
end GetName;

Expand All @@ -84,10 +82,9 @@ package body Sf.Network.Ftp is
function GetMessage (FtpDirectoryResponse : sfFtpDirectoryResponse_Ptr) return String is
function Internal (FtpDirectoryResponse : sfFtpDirectoryResponse_Ptr) return chars_ptr;
pragma Import (C, Internal, "sfFtpDirectoryResponse_getMessage");
Temp : chars_ptr := Internal (FtpDirectoryResponse);
Temp : constant chars_ptr := Internal (FtpDirectoryResponse);
R : constant String := Value (Temp);
begin
Free (Temp);
return R;
end GetMessage;

Expand All @@ -102,10 +99,9 @@ package body Sf.Network.Ftp is
function GetDirectory (FtpDirectoryResponse : sfFtpDirectoryResponse_Ptr) return String is
function Internal (FtpDirectoryResponse : sfFtpDirectoryResponse_Ptr) return chars_ptr;
pragma Import (C, Internal, "sfFtpDirectoryResponse_getDirectory");
Temp : chars_ptr := Internal (FtpDirectoryResponse);
Temp : constant chars_ptr := Internal (FtpDirectoryResponse);
R : constant String := Value (Temp);
begin
Free (Temp);
return R;
end GetDirectory;

Expand All @@ -124,10 +120,9 @@ package body Sf.Network.Ftp is
function GetMessage (FtpResponse : sfFtpResponse_Ptr) return String is
function Internal (FtpResponse : sfFtpResponse_Ptr) return chars_ptr;
pragma Import (C, Internal, "sfFtpResponse_getMessage");
Temp : chars_ptr := Internal (FtpResponse);
Temp : constant chars_ptr := Internal (FtpResponse);
R : constant String := Value (Temp);
begin
Free (Temp);
return R;
end GetMessage;

Expand Down
2 changes: 1 addition & 1 deletion include/sf-network-ftp.ads
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ package Sf.Network.Ftp is
--/ @brief Return a directory/file name contained in a FTP listing response
--/
--/ @param ftpListingResponse Ftp listing response
--/ @param index Index of the name to get (in range [0 .. getCount])
--/ @param index Index of the name to get (in range [0 .. getCount-1])
--/
--/ @return The requested name
--/
Expand Down
6 changes: 2 additions & 4 deletions include/sf-network-http.adb
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,10 @@ package body Sf.Network.Http is
function Internal (HttpResponse : sfHttpResponse_Ptr; Field : chars_ptr) return chars_ptr;
pragma Import (C, Internal, "sfHttpResponse_getField");
Temp1 : chars_ptr := New_String (Field);
Temp2 : chars_ptr := Internal (HttpResponse, Temp1);
Temp2 : constant chars_ptr := Internal (HttpResponse, Temp1);
R : constant String := Value (Temp2);
begin
Free (Temp1);
Free (Temp2);
return R;
end GetField;

Expand All @@ -126,10 +125,9 @@ package body Sf.Network.Http is
function GetBody (HttpResponse : sfHttpResponse_Ptr) return String is
function Internal (HttpResponse : sfHttpResponse_Ptr) return chars_ptr;
pragma Import (C, Internal, "sfHttpResponse_getBody");
Temp : chars_ptr := Internal (HttpResponse);
Temp : constant chars_ptr := Internal (HttpResponse);
R : constant String := Value (Temp);
begin
Free (Temp);
return R;
end GetBody;

Expand Down
2 changes: 1 addition & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SUBDIRS:=clock example graphics music opengl pong renderwindow thread window vulkan
SUBDIRS:=clock example graphics music opengl pong renderwindow thread window vulkan ftp

.PHONY: all clean run

Expand Down
10 changes: 10 additions & 0 deletions tests/ftp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.PHONY: all clean run

all:
gprbuild

clean:
gprclean

run:
./main
57 changes: 57 additions & 0 deletions tests/ftp/main.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
with Ada.Text_IO;
with Sf.Network.Ftp;
with Sf.Network.IpAddress;
with Sf.System.Time;

procedure Main is
use Sf; use Sf.Network; use Sf.Network.Ftp;
FTP_Error : exception;

FTP_Object : constant sfFtp_Ptr := create;

procedure Check_Response (FTP_Response : sfFtpResponse_Ptr) is
Message : constant String := Response.getMessage (FTP_Response);
begin
Response.destroy (FTP_Response);
if not Response.isOk (FTP_Response) then
raise FTP_Error with Message;
else
Ada.Text_IO.Put_Line ("OK: " & Message);
end if;
end Check_Response;

procedure List_Directory (Path : String) is
Response : sfFtpListingResponse_Ptr;
begin
Response := getDirectoryListing (FTP_Object, Path);
if ListingResponse.isOk (Response) then
for Index in 0 .. ListingResponse.getCount (Response) - 1 loop
Ada.Text_IO.Put_Line (ListingResponse.getName (Response, Index));
end loop;
else
Ada.Text_IO.Put_Line (ListingResponse.getMessage (Response));
end if;
ListingResponse.destroy (Response);
end List_Directory;

begin

Check_Response
(connect (FTP_Object,
server => IpAddress.fromString ("speedtest.tele2.net"),
port => 21,
timeout => Sf.System.Time.sfSeconds (30.0)));

Check_Response (loginAnonymous (FTP_Object));

Check_Response (changeDirectory (FTP_Object, "/upload"));
Check_Response (changeDirectory (FTP_Object, "/"));

List_Directory (".");

Check_Response (download (FTP_Object,
remoteFile => "100KB.zip",
localPath => ".",
mode => sfFtpBinary));
destroy (FTP_Object);
end Main;
12 changes: 12 additions & 0 deletions tests/ftp/main.gpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
with "../../asfml.gpr";

project Main is

for Create_Missing_Dirs use "True";

for Source_Dirs use (".");
for Object_Dir use "obj";
for Main use ("main.adb");
for Exec_Dir use ".";

end Main;

0 comments on commit 2462019

Please sign in to comment.