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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,b41415bcc9748556 X-Google-Attributes: gid103376,public From: "David C. Hoos, Sr." Subject: Re: Use of C.Strings.Value Function Considered Harmful Date: 1999/12/03 Message-ID: <829115$b6h$1@hobbes2.crc.com>#1/1 X-Deja-AN: 556291094 References: <3847E5CE.4F9E@dera.gov.uk> X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 X-Complaints-To: abuse@crc.com X-Trace: hobbes2.crc.com 944244581 11473 198.175.145.56 (3 Dec 1999 18:09:41 GMT) Organization: CRC: A wholly owned subsidiary of Thermo Electron NNTP-Posting-Date: 3 Dec 1999 18:09:41 GMT Newsgroups: comp.lang.ada Date: 1999-12-03T18:09:41+00:00 List-Id: Anton Gibbs wrote in message news:3847E5CE.4F9E@dera.gov.uk... > Dear Ada Community, > > Now here is a funny thing - though I doubt whether I am the first to > experience it. > > I have been passed a C structure one of whose fields purports to be a > pointer to a standard C null-terminated string. I want to copy the > string into the Ada world so I thought I would use the Value function in > B.3.1 (37): > > function Value( Item : in chars_ptr ) return String; > > which is defined as being equivalent to: > > To_Ada( Value( Item ), Trim_Nul => False ); > > This works fine except that I want to guard against the C pointer being > bad and serving me up with a potentially limitless string of rubbish. So > I thought I would use the Value function from B.3.1 (35): > > function Value( Item : in chars_ptr; Length : in size_t ) > return String; > > with Length set to some maximum string size that I would be willing to > handle (say 20). The function from B.3.1 (35) has the profile: function Value (Item : in chars_ptr; Length : in size_t) return char_array; _not_ the function you described above. That function is from B.3.1 (39), which is defined as equivalent to Equivalent to To_Ada(Value(Item, Length), Trim_Nul=>True). > > The trouble is that if the pointer designates a string containing 20 or > more characters before its first Nul, then, far from truncating the > result as I had expected, all Value does is raise Terminator_Error. What > is the use of that ? The definition of To_Ada specifies the raising of Terminator_Error when Item contains no nul. You can use the function Interfaces.C.To_Ada, to convert the char_array result of the function from B.3.1 (35) to an Ada String. > > Presumably I am missing something here. > > Thanks for whatever help and advice you can give. Read the manual??