Re: INVALID_HANDLE_VALUE vs. NULL



"Gary Chanson" <gchanson@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:%23m4JX9KWFHA.2572@xxxxxxxxxxxxxxxxxxxxxxx

"Frank A. Uepping" <trash@xxxxxxxxxxxxxxx> wrote in message
news:d654rh$q10$1@xxxxxxxxxxxxxxxxxxxxx
Hello,

there seems to be two representations for an invalid handle:
INVALID_HANDLE_VALUE and NULL.
Can somebody explain this discrepancy?

NULL is a valid value for a file handle (although there are a few
handle
based functions which violate this). For file handles,
INVALID_HANDLE_VALUE
= -1.

On the other hand, GDI and user handles can never be NULL and use this
value for invalid handles.

--
-GJC [MS Windows SDK MVP]
-Software Consultant (Embedded systems and Real Time Controls)
- http://www.mvps.org/ArcaneIncantations/consulting.htm
-gchanson@xxxxxxxx


I may be getting my operating systems mixed up, but I thought that almost
all virtual-memory operating systems set the page at address zero
(0x000...0) to be an invalid page so that a page fault is generated by any
type of access to that address. The reason for this is that making a
reference through a NULL pointer is one of the most common programming bugs,
and by making this page invalid it will cause a crash or other exception if
you make this mistake. I have worked on a few non-VM systems where zero was
a valid address, and that would cause all sorts of system problems when a
program with NULL in a structure pointer would trash the low memory area of
the system, wiping out interrupt vectors, boot code, or other important
stuff!

So assuming that a "handle" is used internally as a structure (object)
pointer, I would be very surprised to find that NULL (0) would be a valid
value.


.



Relevant Pages

  • Re: null references
    ... To say that null and invalid references don't exist suggests that if you would just change to the function would be unable to fail and therefore correctly manages the semaphore, but this is a bogus argument. ... The reference version of the function can still throw an access violation and still needs to be coded for exception safety. ... If you define the above function as taking a reference arg then the undefined behaviour occurs the moment someone tries to dereference an invalid pointer. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: access_ok macor
    ... The problem which I have is that if I run socketpair, getsockname, getpeername LTP ... tests with invalid salen pointer there are addresses close to 0x0. ... This sounds like a classic NULL pointer dereference that is handled correctly ... err = get_user; ...
    (Linux-Kernel)
  • Re: interpreting a null pointer as an empty (null string)
    ... there requires doing something that has undefined behavior (or, at least, ... which is the usual destination of the null pointer. ... Looks like this is explicitly disallowed by the Standard, ... null pointers being "invalid" as operands of the indirection operator; ...
    (comp.lang.c)
  • Re: INVALID_HANDLE_VALUE vs. NULL
    ... > value for invalid handles. ... reference through a NULL pointer is one of the most common programming bugs, ... program with NULL in a structure pointer would trash the low memory area of ...
    (microsoft.public.win32.programmer.kernel)
  • Re: C Standard Regarding Null Pointer Dereferencing
    ... possible case in which a pointer *has* an invalid value. ... evaluation of the expression '*0', ... You and other kind responders have been deducting. ...
    (comp.lang.c)