On Tue, 2010-05-25 at 09:36 -0400, Chad Mynhier wrote: > On Tue, May 25, 2010 at 5:13 AM, Mark Phalan <mark.pha...@sun.com> wrote: > > On Mon, 2010-05-24 at 15:16 -0400, Chad Mynhier wrote: > >> > >> Even though "string" is a separate type in DTrace, a string is still > >> just stored as a null-terminated sequence of characters. stringof() > >> isn't doing anything to null-terminate what you give it, it's just > >> assuming that you're giving it something that's null-terminated. In > >> this case, it appears that there's enough garbage after the copied-in > >> string to ... > > > > I thought that stringof() was being clever. I see this example: > > > > "To print only as much of the string as the caller intended, use the > > copyin() subroutine, which takes a size as its second argument: > > > > > > syscall::write:entry > > { > > printf("%s", stringof(copyin(arg1, arg2))); > > }" > > > > from: http://docs.sun.com/app/docs/doc/817-6223/chp-user?a=view > > > > which seems to imply that stringof() should NULL-terminate. > > > > Is the above example incorrect? > > > > Nope, the above example is mostly working by sheer luck. In this > case, the destination of the copyin() just happens to be zeroed, so > the string it copies in will necessarily be null-terminated.
Ok. I guess its a doc bug then. > > Note that stringof() doesn't actually _do_ anything. stringof() isn't > a subroutine that walks what you give it to verify that it's > null-terminated. (And it _can't_ do that, as it doesn't have a length > argument and thus has no way of determining the proper length of that > string.) stringof() isn't much more than a type cast. Ok. > > OTOH, copyinstr() _does_ take a second argument that specifies a max > length, so the workaround you're looking for is to use that: > > data_string = strjoin("@", > copyinstr((uintptr_t)(*((uint32_t *) > copyin((uintptr_t)&P->data, sizeof (uint32_t)))), *((uint32_t *) > copyin((uintptr_t)&P->length, sizeof (uint32_t))))); > > copyinstr() will null-terminate the resulting string. You can see > this here: > http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/common/dtrace/dtrace.c#3174. > copyinstr() will work when the source is NULL terminated. It mostly won't be in the real-world case I'll need this for. I think my workaround with substr() (see other mail) should work though. Thanks for the help, -M _______________________________________________ dtrace-discuss mailing list dtrace-discuss@opensolaris.org