Re: [Lit.] Buffer overruns

From: Mok-Kong Shen (mok-kong.shen_at_t-online.de)
Date: 12/27/04


Date: Mon, 27 Dec 2004 18:06:34 +0100


infobahn wrote:
> Mok-Kong Shen wrote:
>
>> infobahn wrote:
>>
>>> [...] The C Standard does not forbid such code; it merely does
>>> not define the behaviour of the code under the relevant
>>> circumstance.

>> O.k. I have used too strong a word. The fact remains that
>> the C standard declares the contruct to be undefined and so
>> the user is apparently expected not to employ it, just like
>> in common language the case where a word (typo or self-made)
>> is not in any dictionary.
>
>
> Nothing wrong with the construct, in this case. Just a reminder:
> we're talking about a[a[i]] = 1, in the special case where
> a[i] == i.
>
> The construct in question is "array indexing", and there's nothing
> undefined about it whatsoever (except in certain circumstances).

But the result of the statement is 'undefined' in that situation.
So, if the condition a[i]==i is not checked, then this could
lead to something wrong. Are you suggesting that it is the
user's responsibility to explicitly check that to avoid such
undefined cases?

>> Don't you think that a good compiler
>> should flag such usages instead of silently compile without
>> any warning?
>
>
> I think you expect more than is reasonable from your compiler.
>
> Consider this code:
>
> void f(int *m, int *n)
> {
> *m = *n++;
> }
>
> Would you require the compiler to diagnose this code? If so, why?
> If not, why not?

Using pointers in the C way is inherently unsafe. This may
be understood as a broad critque of the PL, which some others
in this thread have also expressed. On the other hand, this
example is in a sense particular. For people using pointer
arithmetics could (in general practice) be expected to be more
conscious of the dangers involved than in situations like
a[a[i]]=1, where more careful reading of the C standard
document, eventually needing help from experts. (The semantics
of pointers and their use is generally properly understood.)
That's why I called it a 'pitfall'.

>
>>> I think you need to get to grips with the meaning of the
>>> word "undefined". You seem to think that it means "won't
>>> compile" or something. It doesn't.
>>
>>
>>
>> See above. Maybe I as user expect too much from a good compiler.
>
>
> Yes, you do. No compiler is a substitute for a skilled programmer.

Perhaps it is also the case that no skilled programmer is
a substitute for a PL designed for safety of programming and
void of pitfalls.

>> But anyway the example is a pitfall for the average user
>> (who are not experts of the C standard document). Do you agree?
>
>
> I think average users will struggle with C. C programmers, however,
> should not. It's a great pity when they don't live up to expectations.

You are 'defining' the problems away.

M. K. Shen



Relevant Pages

  • Re: Is C99 the final C? (some suggestions)
    ... > that someone will try compile their stuff on an old compiler. ... > because the ANSI standard obsoleted them, and everyone picked up the ANSI ... fixed by using another language. ... >>are multiplying two expressions of the widest type supported by your ...
    (comp.lang.c)
  • Re: Statement on Schildt submitted to wikipedia today
    ... to working programmers and more with being "right all the time, ... so that compiler developers could be shed ... The major corporate interests were compiler developers, ... processors, committed to standard division semantics, and otherwise ...
    (comp.lang.c.moderated)
  • Re: #define and (brackets)
    ... Minor compiler vendors are free to join if they are so inclined, ... analysis hasn't changed between the two versions of the standard. ... This bug is a minor bug in an obscure ...
    (microsoft.public.vc.language)
  • Re: interesting use of NEXT SENTENCE vs. CONTINUE
    ... Program name in quotes (allowed in '02 Standard) ... > If J can be made an independent item which the compiler can put wherever it ... > has to be associated with a hardware device in SPECIAL-NAMES. ... > that ALTER *always* modifies the address parameter of the hardware branch ...
    (comp.lang.cobol)
  • Re: interesting use of NEXT SENTENCE vs. CONTINUE
    ... These days I don't do compiler maintenance, but I HAVE done in the past, so ... You had a chance to point out how adherence to the standard can improve ... >> compile it WITH MINOR MODIFICATIONS for a specific platform. ... > platform-specific extension to COBOL ...
    (comp.lang.cobol)