public inbox for systemtap@sourceware.org
 help / color / mirror / Atom feed
* kread() and NULL pointers
@ 2007-07-16 20:43 Mike Mason
  2007-07-16 22:53 ` Roland McGrath
  0 siblings, 1 reply; 6+ messages in thread
From: Mike Mason @ 2007-07-16 20:43 UTC (permalink / raw)
  To: systemtap

Does kread() generate an error if you pass it a NULL pointer?  I'm trying to determine if NULL pointer checks are still necessary if I use kread().  Some earlier email implies that they aren't, but I want to be certain.

Thanks,
Mike

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: kread() and NULL pointers
  2007-07-16 20:43 kread() and NULL pointers Mike Mason
@ 2007-07-16 22:53 ` Roland McGrath
  2007-07-16 23:10   ` Mike Mason
  0 siblings, 1 reply; 6+ messages in thread
From: Roland McGrath @ 2007-07-16 22:53 UTC (permalink / raw)
  To: Mike Mason; +Cc: systemtap

> Does kread() generate an error if you pass it a NULL pointer?  

Yes.  It's safely caught like all errors, but it only returns successfully
when given a valid pointer.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: kread() and NULL pointers
  2007-07-16 22:53 ` Roland McGrath
@ 2007-07-16 23:10   ` Mike Mason
  2007-07-16 23:52     ` Stone, Joshua I
  2007-07-17  1:49     ` Roland McGrath
  0 siblings, 2 replies; 6+ messages in thread
From: Mike Mason @ 2007-07-16 23:10 UTC (permalink / raw)
  To: Roland McGrath; +Cc: systemtap

Roland McGrath wrote:
>> Does kread() generate an error if you pass it a NULL pointer?  
> 
> Yes.  It's safely caught like all errors, but it only returns successfully
> when given a valid pointer.

So should the following generate an error?  It doesn't.  It just prints "ptr = 0" and exits.

function test_addr:long () %{
        void *nullptr = NULL;
        THIS->__retvalue = (long) kread(&(nullptr));
        CATCH_DEREF_FAULT();
%}


probe begin {
        ptr = test_addr()
        printf("ptr = %d\n", ptr)
        exit()
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: kread() and NULL pointers
  2007-07-16 23:10   ` Mike Mason
@ 2007-07-16 23:52     ` Stone, Joshua I
  2007-07-17 18:05       ` Mike Mason
  2007-07-17  1:49     ` Roland McGrath
  1 sibling, 1 reply; 6+ messages in thread
From: Stone, Joshua I @ 2007-07-16 23:52 UTC (permalink / raw)
  To: Mike Mason; +Cc: Roland McGrath, systemtap

Mike Mason wrote:
> So should the following generate an error?  It doesn't.  It just prints 
> "ptr = 0" and exits.
> 
> function test_addr:long () %{
>        void *nullptr = NULL;
>        THIS->__retvalue = (long) kread(&(nullptr));
-----------------------------------------^

You're reading the address *of* your pointer, not the address in the 
pointer.  Thus kread() is dereferencing an address on the stack, and the 
value there is NULL.  Drop the '&', and change nullptr to a type that's 
meaningful to dereference (e.g., long*), and you'll get your error.

The "&(xxx)" pattern you see everywhere is because usually the "xxx" is 
a struct value, and so you pass kread() a pointer to that value.


Josh

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: kread() and NULL pointers
  2007-07-16 23:10   ` Mike Mason
  2007-07-16 23:52     ` Stone, Joshua I
@ 2007-07-17  1:49     ` Roland McGrath
  1 sibling, 0 replies; 6+ messages in thread
From: Roland McGrath @ 2007-07-17  1:49 UTC (permalink / raw)
  To: Mike Mason; +Cc: systemtap

> Roland McGrath wrote:
> >> Does kread() generate an error if you pass it a NULL pointer?  
> > 
> > Yes.  It's safely caught like all errors, but it only returns successfully
> > when given a valid pointer.
> 
> So should the following generate an error?  It doesn't.  It just prints "ptr = 0" and exits.
> 
> function test_addr:long () %{
>         void *nullptr = NULL;
>         THIS->__retvalue = (long) kread(&(nullptr));
>         CATCH_DEREF_FAULT();
> %}

As noted, this is not kread(NULL).  As to the question, CATCH_DEREF_FAULT
is the code that constitutes what "generate an error" means.  kread is a
macro using deref, which does "goto deref_fault" for errors.  And then:

	#define CATCH_DEREF_FAULT()				\
	  if (0) {						\
	deref_fault:						\
	    CONTEXT->last_error = "pointer dereference fault";	\
	  }

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: kread() and NULL pointers
  2007-07-16 23:52     ` Stone, Joshua I
@ 2007-07-17 18:05       ` Mike Mason
  0 siblings, 0 replies; 6+ messages in thread
From: Mike Mason @ 2007-07-17 18:05 UTC (permalink / raw)
  To: Stone, Joshua I; +Cc: Roland McGrath, systemtap

Stone, Joshua I wrote:
> Mike Mason wrote:
>> So should the following generate an error?  It doesn't.  It just 
>> prints "ptr = 0" and exits.
>>
>> function test_addr:long () %{
>>        void *nullptr = NULL;
>>        THIS->__retvalue = (long) kread(&(nullptr));
> -----------------------------------------^
> 
> You're reading the address *of* your pointer, not the address in the 
> pointer.  Thus kread() is dereferencing an address on the stack, and the 
> value there is NULL.  Drop the '&', and change nullptr to a type that's 
> meaningful to dereference (e.g., long*), and you'll get your error.

You're right.  I fell into the trap of assuming the kread parameter should always have a '&' in front.  Bad assumption on my part.

> 
> The "&(xxx)" pattern you see everywhere is because usually the "xxx" is 
> a struct value, and so you pass kread() a pointer to that value.

Yep, this is what fooled me.

Never mind :-)

Thanks,
Mike

> 
> 
> Josh

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2007-07-17 15:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-16 20:43 kread() and NULL pointers Mike Mason
2007-07-16 22:53 ` Roland McGrath
2007-07-16 23:10   ` Mike Mason
2007-07-16 23:52     ` Stone, Joshua I
2007-07-17 18:05       ` Mike Mason
2007-07-17  1:49     ` Roland McGrath

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).