[comp.sys.mac.programmer] Checking the validity of a refCon

tim@hoptoad.uucp (Tim Maroney) (12/07/89)

In article <17662@dartvax.Dartmouth.EDU> ari@eleazar.dartmouth.edu
(Ari Halberstadt) writes:
>Here's a question for you folks out on NetLand. I'm trying
>to figure out, with a reasonable degree of success, if the refCon
>field of a window contains a valid handle. This must be determined
>by a driver, which is separate from the the program which actually
>created the window.

But it *is* your program, right?  All the windows whose refCons you wil
be looking for (if not at) were created by an application you wrote?
Then probably the best check involves a little-known feature of the
WindowRecord.  You can put any value of 8 or greater into your own
application's windows' windowKind field (IM I-276).  Pick some
arbitrary large number and the collisions with other applications using
the same trick become unlikely.  If your first check is this, then you
should still do some other checks, but they're really just formalities.
You will almost certainly never find another application which both uses
this obscure trick and picks the same number for its windowKinds.

I'm kind of confused about when it is you expect to be looking at other
windows.  You should be able to check whether it's your application
running in a variety of ways.  You can check for the presence of your
signature resource; you can have the application call your driver to
tell it when the application is running, or even when it creates or
destroys a window; you can install a 'STR ' with a particular ID that
contains some distinctive string; and so on.

>The tests currently performed are:
>
>Boolean ValidHandle(Handle hndl)
>{
>	hndl = (Handle) GetWRefCon(window); /* copied here for brevity */
>	if (! hndl)
>		return(false);
>	if ((long) hndl & 1L) /* check for odd address */
>		return(false);
>	if (GetHandleSize(hndl) != expected_size)
>		return(false);
>}

This looks mostly OK; it wouldn't be a bad idea to combine this kind of
testing with one of the other kinds I mentioned.  I do have a problem
with the GetHandleSize.  It could conceivably be called on a
non-handle; the "hndl & 1" test doesn't establish that it's a handle.
For instance, suppose the window belongs to an application that sticks
the number 0xfffffffe into the refCon.  Boom.  You should check that the
handle is even, then that it is a pointer into the application heap,
then that what it points at is also an even pointer into the
application heap, and then dereference the pointer to find out whether
the longword at it is equal to some distinctive value you've stashed in
all your refcon records at the beginning.

>Actually, I've got three questions.
>1. Is it ok to check for odd adresses on a 680x0? I assume Apple
>wouldn't write NewHandle to create something on an odd adress, since
>access times are slower.

NewHandle will always create it on an even address, regardless of
machine.  Heaps are divided into blocks which are size-rounded up to
even numbers (IM II-25).

>2. I would like to check that the handle is within a reasonable area
>of memory, i.e., within the application heap. However, since the
>application may have several heap zones, what is the correct way to
>check this? Is it sufficient to use ApplicZone and GetApplLimit?

Sure.  Be sure to convert them to unsigned longwords and call
StripAddress on them before you do your arithmetic.  The same goes for
the pointers you're checking.  Unless you do the (in my opinion,
dreadful) trick of putting a heap on the stack, all your heaps will
fall within the application heap.  (You could also put a heap in
temporary memory, but then it's not really temporary, is it?)

>3. Any more ideas for useful checks? I've already got a special
>long int stuck at the head of the place where the correct handle
>would point to, and I've thought of making sure the handle is
>below RomBase.

This shouldn't hurt, but it's redundant if you're already checking
against the application heap.

>--
>Ari Halberstadt '91, "Long live succinct signatures"
-- 
Tim Maroney, Mac Software Consultant, sun!hoptoad!tim, tim@toad.com

FROM THE FOOL FILE:
"Those Mayas were sacrificing not only pagan children, but baptized
 Christian children, for crying out loud!  And they were carrying out
 those sacrifices, those barbarities, with great savagery, without
 giving the victims the benefit of the humane types of death that the
 European Church accorded even to heretics and witches during that
 century, such as burning at the stake."
		-- Matthew Rosenblatt, rec.arts.books