comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: C chars_ptr STORAGE_ERROR in Imported function
Date: Sat, 23 Aug 2008 13:11:46 +0300
Date: 2008-08-23T13:11:46+03:00	[thread overview]
Message-ID: <48afe1c5$0$23598$4f793bc4@news.tdc.fi> (raw)
In-Reply-To: <gSDrk.11330$Mh5.10289@bgtnsc04-news.ops.worldnet.att.net>

(Top posting by "anon" changed to bottom style...)

anon wrote:
 > In <g8ltqo$7ir$1@jacob-sparre.dk>, Kim Rostgaard Christensen
 > <krc@greenpc.dk> writes:
 >
 >>Hello there
 >>
 >>I am in progress of binding the pcap c library to ada, it is a
 >>part of a school project. And for that i need a funtion that 
 >>modifies a parameter to a function like so:
 >>
 >>char    *pcap_lookupdev(char *);
 >>
 >>I have figured out this much:
 >>
 >> function pcap_lookupdev(errbuff :
       Interfaces.C.Strings.Chars_Ptr)
 >> return Interfaces.C.Strings.Chars_Ptr;
 >> pragma Import (C, pcap_lookupdev, "pcap_lookupdev");
 >>
 >>This works in the sense that running it as root returns the
 >>device found, but when I run it as normal user I get
 >>raised STORAGE_ERROR : stack overflow (or erroneous memory
 >>access)
 >>
 >>Do I need to declare the buffer and then then pass its c pointer
 >>to the function?

anon wrote:
 > Because Char_Prt in "Interfaces.C.Strings" is a private Ada
 > type that  C function does not understand.

No, that's wrong. RM B.3.1(1) says that

...the private type chars_ptr corresponds to a common use of "char
*" in C programs, and an object of this type can be passed to a
subprogram to which pragma Import(C,...) has been applied, and for
which "char *" is the type of the argument of the C function.

I think Kim's choice of chars_ptr for this parameter is correct.
The error was firstly in the use of an uninitialized parameter of
this type (default initialized to Null_Ptr), and secondly in using
New_Char_Array on an uninitialized char_array, making
New_Char_Array allocate a smaller new buffer than was needed to
hold the error message from pcap_lookupdev(). Thus,
pcap_lookupdev() was called with incorrect values of its parameter,
not with an incorrect type of parameter. Similar errors could have
been made in a C function calling pcap_lookupdev() with a C
parameter of type "char *".

 > You need to use "Interfaces.C.Pointer"  package to build a C
 > Char_Ptr pointer package.

That might work, too, but Interfaces.C.Strings is the right tool
for pcap_lookupdev() since the parameter really is a NUL-terminated
C-style string.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



      parent reply	other threads:[~2008-08-23 10:11 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-22  8:36 C chars_ptr STORAGE_ERROR in Imported function Kim Rostgaard Christensen
2008-08-22  9:03 ` Niklas Holsti
2008-08-22  9:55   ` Kim Rostgaard Christensen
2008-08-22 11:43     ` Niklas Holsti
2008-08-22 22:54       ` Kim Rostgaard Christensen
2008-08-22 18:48 ` anon
2008-08-22 21:55   ` tmoran
2008-08-23 10:11   ` Niklas Holsti [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox