[PATCH] net.c (printsock): Output AF_UNIX socket address using printstr

Dmitry V. Levin ldv at altlinux.org
Sat Sep 22 16:25:40 UTC 2007


Hi,

Sorry for long delay,

On Sat, Aug 25, 2007 at 01:21:00PM +0100, Neil Campbell wrote:
> I was tracing some applications and found that the output of strace
> included some non-printable characters, leading various tools such as
> 'file' and 'less' to refuse to recognise the file as text.  It seemed
> that this was due to some strange paths being included in unix domain
> sockaddr structs.
> 
> I've attached a patch which solves this issue for me, I'd be grateful
> for any feedback on it.  Essentially I've refactored printstr() and
> added a printstrn() function, and I'm now calling this rather than
> tprintf() to display the paths.  This takes care of escaping
> non-printable characters for me.
> 
> Please let me know what you think.

While your suggestion to avoid unprintable characters in output is right,
your patch looks too intrusive: printstr() seems to be enough, and
strnlen() is a GNU extension.

I propose another patch to avoid unprintable characters in output.

2007-09-22  Dmitry V. Levin <ldv at altlinux.org>

	* net.c (printsock): Output AF_UNIX socket address using
	printstr() to avoid unprintable characters in output.
	Suggested by Neil Campbell.

--- strace/net.c
+++ strace/net.c
@@ -937,10 +937,7 @@
 
 
 void
-printsock(tcp, addr, addrlen)
-struct tcb *tcp;
-long addr;
-int addrlen;
+printsock(struct tcb *tcp, long addr, int addrlen)
 {
 	union {
 		char pad[128];
@@ -970,13 +967,16 @@ int addrlen;
 		tprintf("%#lx", addr);
 		return;
 	}
-	if ((addrlen<2) || (addrlen>sizeof(addrbuf)))
-		addrlen=sizeof(addrbuf);
 
-	if (umoven(tcp, addr, addrlen, (char*)&addrbuf) < 0) {
+	if (addrlen < 2 || addrlen > sizeof(addrbuf))
+		addrlen = sizeof(addrbuf);
+
+	memset(&addrbuf, 0, sizeof(addrbuf));
+	if (umoven(tcp, addr, addrlen, addrbuf.pad) < 0) {
 		tprintf("{...}");
 		return;
 	}
+	addrbuf.pad[sizeof(addrbuf.pad) - 1] = '\0';
 
 	tprintf("{sa_family=");
 	printxval(addrfams, addrbuf.sa.sa_family, "AF_???");
@@ -984,12 +984,14 @@ int addrlen;
 
 	switch (addrbuf.sa.sa_family) {
 	case AF_UNIX:
-		if (addrlen==2) {
-			tprintf("<nil>");
+		if (addrlen == 2) {
+			tprintf("NULL");
 		} else if (addrbuf.sau.sun_path[0]) {
-			tprintf("path=\"%-.*s\"", addrlen-2, addrbuf.sau.sun_path);
+			tprintf("path=");
+			printstr(tcp, addr + 2, strlen(addrbuf.sau.sun_path));
 		} else {
-			tprintf("path=@%-.*s", addrlen-3, addrbuf.sau.sun_path+1);
+			tprintf("path=@");
+			printstr(tcp, addr + 3, strlen(addrbuf.sau.sun_path + 1));
 		}
 		break;
 	case AF_INET:


-- 
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20070922/e8d48b88/attachment.bin>


More information about the Strace-devel mailing list