Re: Plz explain me the following code



Kenneth Brody <kenbrody@xxxxxxxxxxx> writes:
jameskuyper wrote:
[...]
The modification I'm talking about is the one Kenneth Brody suggested
in his message with the header "Date:Thu, 24 Sep 2009 13:17:54 -0400":
Perhaps replacing UB with something like "anything left undefined will, on
hosted implementations, behave in a manner consistent with the host"? So,
dereferencing a NULL pointer on Unix can't launch nuclear missiles, since
Unix says it should SEGV.

Basically, "I am not imposing any restrictions, but I do require that you
follow the restrictions placed on you by the host platform".

The essence of what I'm saying is that this modification is poorly
specified. I don't see any good way to improve it, either, or I would
have suggested one.

Note that I wasn't actually proposing a well-thought-out idea for an
actual change to C. It was mostly an off-the-cuff reply to a thread
which had started going towards "why does C even allow UB ... UB ==
bad language design".

And, thinking a little more about it, some UB doesn't even involve the
host. For example, fclose() on an invalid FILE*, or free() with a
pointer not from malloc() and friends. (Yes, the host may get
involved if the pointer itself is not valid, but what happens with a
valid pointer but just not pointing to what is expected?)

FILE* fp=fopen("foo", "r");
FILE* fp2=malloc(sizeof(*fp2));
memcpy(fp2, fp, sizeof(*fp2));
fclose(fp2);

fp2 of course does point to *what* was expected, it's just not *where*
it was expected.

Phil
--
Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1
.



Relevant Pages

  • Re: Plz explain me the following code
    ... Unix says it should SEGV. ... follow the restrictions placed on you by the host platform". ... For example, fcloseon an invalid FILE*, or freewith a pointer not from mallocand friends. ... (Yes, the host may get involved if the pointer itself is not valid, but what happens with a valid pointer but just not pointing to what is expected?) ...
    (comp.lang.c)
  • Re: Plz explain me the following code
    ... dereferencing a NULL pointer on Unix can't launch nuclear missiles, ... Unix says it should SEGV. ... follow the restrictions placed on you by the host platform". ... actually dereference a NULL pointer. ...
    (comp.lang.c)
  • Re: Pure and internal procedures
    ... inactive version of the host. ... The onlydifference is that for a label constant you don't have to ... prevent 'leaking' such a pointer upward out of the scope and thus ...
    (comp.lang.fortran)
  • Re: How to prevent other PC from scaning my machine?
    ... [compton ~]$ host 64.12.14.81 ... 81.14.12.64.IN-ADDR.ARPA domain name pointer mtc-cache001.edns.aol.com ... windoze, and it's trying to find who it can "share" your information with. ...
    (alt.computer.security)
  • Re: Plz explain me the following code
    ... dereferencing a NULL pointer on Unix can't launch nuclear missiles, ... Unix says it should SEGV. ... follow the restrictions placed on you by the host platform". ...
    (comp.lang.c)