comp.lang.ada
 help / color / mirror / Atom feed
From: "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com>
Subject: Re: Use of C.Strings.Value Function Considered Harmful
Date: 1999/12/03
Date: 1999-12-03T18:09:41+00:00	[thread overview]
Message-ID: <829115$b6h$1@hobbes2.crc.com> (raw)
In-Reply-To: 3847E5CE.4F9E@dera.gov.uk


Anton Gibbs <agibbs@dera.gov.uk> 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??








  reply	other threads:[~1999-12-03  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-12-03  0:00 Use of C.Strings.Value Function Considered Harmful Anton Gibbs
1999-12-03  0:00 ` David C. Hoos, Sr. [this message]
1999-12-06  0:00   ` Anton Gibbs
1999-12-06  0:00     ` Lutz Donnerhacke
replies disabled

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