comp.lang.ada
 help / color / mirror / Atom feed
From: Bradd W. Szonye <bradds@ix.netcom.com>
Subject: RE: ANSI C and POSIX (was Re: C/C++ knocks the crap out of Ada)
Date: 1996/04/20
Date: 1996-04-20T10:55:36-05:00	[thread overview]
Message-ID: <01bb2ed2.425fb900$65c2b7c7@Zany.localhost> (raw)
In-Reply-To: dewar.829276268@schonberg


On Thursday, April 11, 1996, Robert Dewar wrote...
> Chuck said
> 
> "There are a lot of things that are intentionally not spelled
> out by standards.  Sometimes this is because the standard
> writers want to limit the scope of the document to keep it
> legible and usable, and sometimes it's because they don't want
> to preclude implementors from offering usable products
> based on current technology or from adding capabilities and
> value to future products."
> 
> This is a sorry excuse for an obvious oversight if you ask me. All that
> is needed for read is one of the following two sentences:
> 
> The buffer must be long enough to accomodate the data actually read
> 
> or
> 
> 
> The buffer must be at least the length corresponding to the value of
> the count parameter.
> 
> I don't really care which is chosen, I prefer the second but could 
> certainly live with the first, but I do NOT find it acceptable to
> leave this unstated. This kind of carelessness in specification
> which seems endemic in the C runtime library, is bound to lead
> to misunderstandings and portability problems. No one is asking
> for over-specification, or for exhaustive and exhausting formality,
> just for a clear informal statement of what is intended!
> 

Try to keep in mind the spirit of defensive programming:
If there's something ambiguous about the way you could implement
something, and one implementation is safe regardless of how you interpret
the ambiguity, the other implementation only works under one specific
interpretation, then defensive programming (and portable programming) will
encourage the code that works under all circumstances. Consider:

for (size_t i = 0; i < 10; i++) do_stuff();

versus

for (size_t i = 0; i != 10; i++) do_stuff();

Even though you *know* that i will never be greater than 10, even though
"not equals" should always stop the loop after the tenth iteration,
practically every programmer will write the first loop in preference to
the second. This has nothing to do with standards; the standards say that
i is a local, stack-based variable, not global, and since it is not
volatile or referenced by anything else, do_stuff() couldn't modify it,
even another thread couldn't modify it. But should your memory chips fail,
or do_stuff() accidentally trash the stack with a pointer, then the first
loop will never let i get out of the range of 0 <= i < 10, while the
second loop might.

Similarly, defensive/portable/paranoid code (which is what most of us
strive to write) will try to ensure that your buffer is big enough to
support the byte-count given to read. This is no cop-out; this is being
cautious. And there's even a good reason for it: the C run-time is allowed
to pad the end of a file with zero bytes. Just because you *know* that
file is only 68 bytes, you can't rely on getting back only 68 bytes. C can
pad that with zeroes to some system-defined line or page size (this is in
the standard mostly to support mainframe computer text modes). So you
could get back 80 or 132 bytes (the line size of most text files on a
mainframe), 512 or 1024 bytes (the sector size of most files on a PC or
workstation), or 4096 bytes (the memory page size under Win32), or any
value in between, including 68 bytes (what's actually in the file).

Now, whether a file is padded with zeroes is implementation-defined, which
means that your compiler manuals need to specify whether this happens. But
it's *intentionally* left out of the standards, not as a cop-out or
silliness, but because of real-world concerns. And it's just one more good
reason to consult your local manuals for C or POSIX and *not* the
standards documents. Standards documents are for vendors, not for
programmers.






  reply	other threads:[~1996-04-20  0:00 UTC|newest]

Thread overview: 177+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-04-12  0:00 ANSI C and POSIX (was Re: C/C++ knocks the crap out of Ada) Chuck Karish
1996-04-11  0:00 ` Robert Dewar
1996-04-20  0:00   ` Bradd W. Szonye [this message]
1996-04-21  0:00     ` Mike Rubenstein
1996-04-22  0:00       ` Tom Payne
1996-04-22  0:00         ` Peter Seebach
1996-04-21  0:00     ` Danette & Murray Root
1996-04-23  0:00       ` Bradd W. Szonye
     [not found] <JSA.96Feb16135027@organon.com>
1996-03-26  0:00 ` C/C++ knocks the crap out of Ada Ed Falis
1996-03-28  0:00   ` Kevin Cline
1996-03-30  0:00     ` Jon S Anthony
1996-04-01  0:00       ` Kevin Cline
1996-04-02  0:00         ` Lawrence Kirby
1996-04-02  0:00           ` ANSI C and POSIX (was Re: C/C++ knocks the crap out of Ada) David Emery
1996-04-02  0:00             ` The Right Reverend Colin James III
1996-04-03  0:00               ` David Emery
1996-04-03  0:00                 ` The Right Reverend Colin James III
1996-04-04  0:00                   ` Dan Pop
1996-04-03  0:00               ` Bill Clinton
1996-04-04  0:00             ` Lawrence Kirby
1996-04-05  0:00               ` Robert Dewar
1996-04-05  0:00                 ` Lawrence Kirby
1996-04-05  0:00                   ` Robert Dewar
1996-04-06  0:00                     ` Peter Seebach
1996-04-06  0:00                       ` Robert Dewar
1996-04-16  0:00                       ` Philip Brashear
1996-04-16  0:00                         ` Robert Dewar
1996-04-19  0:00                           ` Chuck Karish
1996-04-05  0:00                 ` Peter Seebach
1996-04-05  0:00                   ` Robert Dewar
1996-04-06  0:00                     ` Lawrence Kirby
1996-04-06  0:00                       ` Robert Dewar
1996-04-07  0:00                         ` Lawrence Kirby
1996-04-10  0:00                           ` halvin
1996-04-10  0:00                             ` Peter Seebach
1996-04-11  0:00                             ` Dan Pop
1996-04-12  0:00                             ` Chuck Karish
1996-04-12  0:00                         ` Chuck Karish
1996-04-12  0:00                           ` Robert Dewar
1996-04-13  0:00                             ` Chuck Karish
1996-04-13  0:00                               ` Robert Dewar
1996-04-13  0:00                                 ` Peter Seebach
1996-04-13  0:00                                   ` Robert Dewar
1996-04-14  0:00                                     ` Lawrence Kirby
1996-04-15  0:00                                     ` Chuck Karish
1996-04-15  0:00                                 ` Chuck Karish
1996-04-16  0:00                                   ` Robert Dewar
1996-04-16  0:00                                     ` Chuck Karish
1996-04-16  0:00                                       ` Robert Dewar
1996-04-12  0:00                     ` Chuck Karish
1996-04-11  0:00                       ` Kazimir Kylheku
1996-04-12  0:00                         ` Chuck Karish
1996-04-12  0:00                           ` Kazimir Kylheku
1996-04-12  0:00                         ` Tom Griest
1996-04-12  0:00                           ` Robert Dewar
1996-04-12  0:00                       ` Robert Dewar
1996-04-13  0:00                         ` Chuck Karish
1996-04-13  0:00                           ` David Emery
1996-04-22  0:00                         ` Mike McCarty
1996-04-22  0:00                           ` David Emery
1996-04-05  0:00               ` David Emery
1996-04-06  0:00                 ` Lawrence Kirby
1996-04-06  0:00 ` Dan Pop
1996-04-07  0:00   ` Robert Dewar
1996-04-07  0:00     ` Lawrence Kirby
1996-04-07  0:00       ` Robert Dewar
1996-04-08  0:00         ` Peter Seebach
1996-04-08  0:00           ` Robert Dewar
1996-04-08  0:00             ` Peter Seebach
1996-04-09  0:00             ` Dan Pop
1996-04-09  0:00               ` James McIninch
1996-04-10  0:00                 ` Dan Pop
1996-04-09  0:00                   ` Danette & Murray Root
1996-04-11  0:00                 ` Tom Wheeley
1996-04-07  0:00     ` Peter Seebach
1996-04-08  0:00       ` Robert Dewar
1996-04-08  0:00         ` Lawrence Kirby
1996-04-08  0:00           ` Robert Dewar
1996-04-08  0:00             ` Peter Seebach
1996-04-09  0:00               ` Robert Dewar
1996-04-09  0:00                 ` Peter Seebach
1996-04-09  0:00                 ` Kazimir Kylheku
1996-04-08  0:00             ` Kazimir Kylheku
1996-04-09  0:00               ` Robert Dewar
1996-04-09  0:00                 ` Kazimir Kylheku
1996-04-09  0:00                   ` Robert Dewar
1996-04-09  0:00                     ` Kazimir Kylheku
1996-04-10  0:00                       ` Robert Dewar
1996-04-10  0:00                         ` Kazimir Kylheku
1996-04-10  0:00                           ` Robert A Duff
1996-04-19  0:00                           ` Bradd W. Szonye
1996-04-19  0:00                             ` Robert Dewar
1996-04-23  0:00                               ` Bradd W. Szonye
1996-04-10  0:00                 ` Mike Shannon
1996-04-09  0:00                   ` Robert Dewar
1996-04-09  0:00                   ` Robert Dewar
1996-04-09  0:00                     ` Peter Seebach
1996-04-10  0:00                       ` Robert Dewar
1996-04-11  0:00                   ` Tom Wheeley
1996-04-11  0:00                     ` Robert Dewar
1996-04-11  0:00                     ` Robert A Duff
1996-04-12  0:00                       ` Tom Wheeley
1996-04-12  0:00                         ` Robert Dewar
1996-04-13  0:00                           ` Chuck Karish
1996-04-13  0:00                             ` Robert Dewar
1996-04-19  0:00                               ` Bradd W. Szonye
1996-04-19  0:00                                 ` David Emery
1996-04-19  0:00                                 ` Robert Dewar
1996-04-22  0:00                                   ` Peter Seebach
1996-04-23  0:00                                 ` Keith Thompson
1996-04-19  0:00                       ` Bradd W. Szonye
1996-04-19  0:00                         ` Robert Dewar
1996-04-23  0:00                           ` Bradd W. Szonye
1996-04-19  0:00                         ` Robert A Duff
1996-04-20  0:00                           ` Bradd W. Szonye
1996-04-09  0:00             ` Robert I. Eachus
1996-04-09  0:00               ` Kazimir Kylheku
1996-04-09  0:00               ` Peter Seebach
1996-04-25  0:00               ` BLUE
1996-04-08  0:00           ` Szu-Wen Huang
1996-04-08  0:00             ` James McIninch
1996-04-08  0:00               ` Szu-Wen Huang
1996-04-08  0:00                 ` Robert Dewar
1996-04-19  0:00                   ` Bradd W. Szonye
1996-04-08  0:00               ` Robert Dewar
1996-04-11  0:00                 ` Keith Thompson
1996-04-19  0:00                 ` Bradd W. Szonye
1996-04-08  0:00               ` Fergus Henderson
1996-04-08  0:00                 ` Robert Dewar
1996-04-19  0:00                 ` Bradd W. Szonye
1996-04-20  0:00                   ` Fergus Henderson
1996-04-20  0:00                     ` Bradd W. Szonye
1996-04-08  0:00         ` Peter Seebach
1996-04-08  0:00           ` Robert Dewar
1996-04-08  0:00             ` Kazimir Kylheku
1996-04-09  0:00               ` Robert Dewar
1996-04-09  0:00                 ` Lawrence Kirby
1996-04-09  0:00                   ` Robert Dewar
1996-04-09  0:00                     ` Kazimir Kylheku
1996-04-10  0:00                       ` Robert Dewar
1996-04-10  0:00                         ` Kazimir Kylheku
1996-04-10  0:00                     ` David Emery
1996-04-09  0:00                 ` Kazimir Kylheku
1996-04-09  0:00             ` Peter Seebach
1996-04-10  0:00             ` John Marshall
1996-04-10  0:00               ` Robert Dewar
1996-04-08  0:00           ` Fergus Henderson
1996-04-08  0:00             ` Robert Dewar
1996-04-08  0:00               ` Kazimir Kylheku
1996-04-09  0:00                 ` Robert Dewar
1996-04-11  0:00                   ` Tom Wheeley
1996-04-11  0:00                     ` Kazimir Kylheku
1996-04-12  0:00                       ` Peter Seebach
1996-04-13  0:00                       ` Tom Wheeley
1996-04-09  0:00               ` Peter Seebach
1996-04-08  0:00             ` Peter Seebach
1996-04-09  0:00               ` Robert Dewar
1996-04-09  0:00               ` Fergus Henderson
1996-04-09  0:00                 ` Kenneth Mays
1996-04-10  0:00                   ` Fergus Henderson
1996-04-10  0:00                 ` Tom Payne
1996-04-09  0:00               ` Fergus Henderson
1996-04-09  0:00                 ` Robert Dewar
1996-04-09  0:00                   ` Kazimir Kylheku
1996-04-10  0:00             ` Steve Summit
1996-04-10  0:00               ` Robert Dewar
1996-04-09  0:00           ` Steve Tynor
1996-04-09  0:00             ` Kazimir Kylheku
1996-04-10  0:00             ` Lawrence Kirby
1996-04-12  0:00               ` Dr S.J. Harris
1996-04-12  0:00                 ` Peter Seebach
1996-04-10  0:00             ` Chet
1996-04-10  0:00               ` Peter Seebach
1996-04-10  0:00               ` Kazimir Kylheku
1996-04-10  0:00               ` Tom Watson
1996-04-10  0:00               ` Robert Dewar
1996-04-12  0:00           ` Gareth Rees
1996-04-10  0:00         ` Matt Austern
1996-04-10  0:00           ` Robert Dewar
1996-04-10  0:00         ` Tom Payne
1996-04-07  0:00     ` Dan Pop
1996-04-07  0:00       ` Robert Dewar
replies disabled

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