comp.lang.ada
 help / color / mirror / Atom feed
From: ok@goanna.cs.rmit.edu.au (Richard A. O'Keefe)
Subject: Re: Two questions
Date: 1996/11/13
Date: 1996-11-13T00:00:00+00:00	[thread overview]
Message-ID: <56btal$jpt$1@goanna.cs.rmit.edu.au> (raw)
In-Reply-To: 561dfo$h2g@news2.delphi.com


tmoran@bix.com writes:
>The Ada idiom for
>  char str[20];
>  sprintf(&str, "variable %s = %d", name, x);
>would be something like:
>declare
>  str:constant string := "variable " & name & " =" & integer'image(x);
>begin
> ...
>Or, since you can return whole strings from functions, you can have
> ... return "variable " & name & " =" & integer'image(x);

He doesn't point out that the C version has a _major_ bug which the
Ada version is completely free of:  sprintf() is vulnerable to buffer
overflow.  In this particular example, the string
	"variable %s = %d"
contains 12 non-NUL non-format-item characters.  sprintf() will write
a NUL to the buffer, so we have 20 - (12 + 1) = 7 characters to hold
both the name and the value.  If the value x is 100 and the name is
"foobar", you are out of luck.

The Ada version, in contrast, returns a string which is just the
right size to hold the answer.

There's one other difference, which is that to get an Ada equivalent
of %d you need Trim(Integer'Image(x)) to get rid of the leading blanks.
(There's a lot of string handling stuff in Ada 95.)

-- 
Mixed Member Proportional---a *great* way to vote!
Richard A. O'Keefe; http://www.cs.rmit.edu.au/%7Eok; RMIT Comp.Sci.




  parent reply	other threads:[~1996-11-13  0:00 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-11-09  0:00 Two questions tmoran
1996-11-11  0:00 ` Adam Beneschan
1996-11-13  0:00 ` Richard A. O'Keefe [this message]
  -- strict thread matches above, loose matches on Subject: below --
2002-07-04 22:25 Mark
2002-07-04 22:40 ` Jeffrey Creem
2001-03-12 10:59 Christoph Grein
2001-03-12 17:43 ` Stephen Leake
2001-03-09 18:27 chris.danx
2001-03-09 20:22 ` Mark Lundquist
2001-03-09 20:56 ` Randy Brukardt
2001-03-12 15:36 ` John English
2001-03-12 18:11   ` chris.danx
1996-11-07  0:00 Ding-yuan Sheu
1996-11-07  0:00 ` Robert Dewar
1996-11-08  0:00 ` Jon S Anthony
1996-11-08  0:00 ` Norman H. Cohen
1996-11-08  0:00 ` Robert I. Eachus
1996-05-01  0:00 Bernard Banner
1996-05-01  0:00 W. Wesley Groleau (Wes)
1996-05-01  0:00 Ed Seidewitz
1989-04-11 13:32 Piercarlo Grandi
1989-04-14 17:14 ` callen
1989-03-30 11:53 Piercarlo Grandi
1989-04-13  0:46 ` Paul Stachour
1989-03-29  9:16 HansM
1989-03-29 18:35 ` Michael Peirce
1989-03-31 13:10 ` stt
1989-03-31 18:59 ` Scott Simpson
1989-04-03 14:44 ` callen
replies disabled

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