From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,ebad38e7bffe4881 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news2.google.com!news.glorb.com!feed.xsnews.nl!border-1.ams.xsnews.nl!border1.nntp.ams.giganews.com!nntp.giganews.com!uio.no!newsfeed1.fi.sn.net!fi.sn.net!newsfeed2.fi.sn.net!news.song.fi!not-for-mail Date: Sat, 23 Aug 2008 13:11:46 +0300 From: Niklas Holsti User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20060628 Debian/1.7.8-1sarge7.1 X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: C chars_ptr STORAGE_ERROR in Imported function References: In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <48afe1c5$0$23598$4f793bc4@news.tdc.fi> Organization: TDC Internet Services NNTP-Posting-Host: laku61.adsl.netsonic.fi X-Trace: 1219486149 news.tdc.fi 23598 81.17.205.61:32882 X-Complaints-To: abuse@tdcnet.fi Xref: g2news2.google.com comp.lang.ada:7505 Date: 2008-08-23T13:11:46+03:00 List-Id: (Top posting by "anon" changed to bottom style...) anon wrote: > In , Kim Rostgaard Christensen > 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 . @ .