changeset 22107:bfe1854bbf87

More compiler warning fixes from o_sukhodolsky (with additions by me). Fixes #4643, references #1344.
author Mark Doliner <mark@kingant.net>
date Mon, 14 Jan 2008 07:13:49 +0000
parents 06c58cffd4c4
children e21cdaffbcdb
files libpurple/dnsquery.c libpurple/dnssrv.c
diffstat 2 files changed, 43 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/dnsquery.c	Mon Jan 14 06:44:14 2008 +0000
+++ b/libpurple/dnsquery.c	Mon Jan 14 07:13:49 2008 +0000
@@ -159,6 +159,24 @@
 }
 #endif
 
+static void
+write_to_parent(int fd, const void *buf, size_t count)
+{
+	ssize_t written;
+
+	written = write(fd, buf, count);
+	if (written != count) {
+		if (written < 0)
+			fprintf(stderr, "dns[%d]: Error writing data to "
+					"parent: %s\n", getpid(), strerror(errno));
+		else
+			fprintf(stderr, "dns[%d]: Error: Tried to write %"
+					G_GSIZE_FORMAT " bytes to parent but instead "
+					"wrote %" G_GSIZE_FORMAT " bytes\n",
+					getpid(), count, written);
+	}
+}
+
 G_GNUC_NORETURN static void
 purple_dnsquery_resolver_run(int child_out, int child_in, gboolean show_debug)
 {
@@ -201,7 +219,8 @@
 		}
 		rc = read(child_in, &dns_params, sizeof(dns_params_t));
 		if (rc < 0) {
-			perror("read()");
+			fprintf(stderr, "dns[%d]: Error: Could not read dns_params: "
+					"%s\n", getpid(), strerror(errno));
 			break;
 		}
 		if (rc == 0) {
@@ -210,11 +229,13 @@
 			_exit(0);
 		}
 		if (dns_params.hostname[0] == '\0') {
-			printf("dns[%d]: hostname = \"\" (port = %d)!!!\n", getpid(), dns_params.port);
+			fprintf(stderr, "dns[%d]: Error: Parent requested resolution "
+					"of an empty hostname (port = %d)!!!\n", getpid(),
+					dns_params.port);
 			_exit(1);
 		}
 		/* Tell our parent that we read the data successfully */
-		write(child_out, &ch, sizeof(ch));
+		write_to_parent(child_out, &ch, sizeof(ch));
 
 		/* We have the hostname and port, now resolve the IP */
 
@@ -230,7 +251,7 @@
 		 */
 		hints.ai_socktype = SOCK_STREAM;
 		rc = getaddrinfo(dns_params.hostname, servname, &hints, &res);
-		write(child_out, &rc, sizeof(rc));
+		write_to_parent(child_out, &rc, sizeof(rc));
 		if (rc != 0) {
 			close(child_out);
 			if (show_debug)
@@ -242,17 +263,17 @@
 		tmp = res;
 		while (res) {
 			size_t ai_addrlen = res->ai_addrlen;
-			write(child_out, &ai_addrlen, sizeof(ai_addrlen));
-			write(child_out, res->ai_addr, res->ai_addrlen);
+			write_to_parent(child_out, &ai_addrlen, sizeof(ai_addrlen));
+			write_to_parent(child_out, res->ai_addr, res->ai_addrlen);
 			res = res->ai_next;
 		}
 		freeaddrinfo(tmp);
-		write(child_out, &zero, sizeof(zero));
+		write_to_parent(child_out, &zero, sizeof(zero));
 #else
 		if (!inet_aton(dns_params.hostname, &sin.sin_addr)) {
 			struct hostent *hp;
 			if (!(hp = gethostbyname(dns_params.hostname))) {
-				write(child_out, &h_errno, sizeof(int));
+				write_to_parent(child_out, &h_errno, sizeof(int));
 				close(child_out);
 				if (show_debug)
 					printf("DNS Error: %d\n", h_errno);
@@ -265,10 +286,10 @@
 			sin.sin_family = AF_INET;
 
 		sin.sin_port = htons(dns_params.port);
-		write(child_out, &zero, sizeof(zero));
-		write(child_out, &addrlen, sizeof(addrlen));
-		write(child_out, &sin, addrlen);
-		write(child_out, &zero, sizeof(zero));
+		write_to_parent(child_out, &zero, sizeof(zero));
+		write_to_parent(child_out, &addrlen, sizeof(addrlen));
+		write_to_parent(child_out, &sin, addrlen);
+		write_to_parent(child_out, &zero, sizeof(zero));
 #endif
 		dns_params.hostname[0] = '\0';
 	}
--- a/libpurple/dnssrv.c	Mon Jan 14 06:44:14 2008 +0000
+++ b/libpurple/dnssrv.c	Mon Jan 14 07:13:49 2008 +0000
@@ -200,12 +200,20 @@
 	PurpleSrvCallback cb = query_data->cb;
 	int status;
 
-	if (read(source, &size, sizeof(int)) > 0)
+	if (read(source, &size, sizeof(int)) == sizeof(int))
 	{
+		ssize_t red;
 		purple_debug_info("dnssrv","found %d SRV entries\n", size);
 		tmp = res = g_new0(PurpleSrvResponse, size);
 		for (i = 0; i < size; i++) {
-			read(source, tmp++, sizeof(PurpleSrvResponse));
+			red = read(source, tmp++, sizeof(PurpleSrvResponse));
+			if (red != sizeof(PurpleSrvResponse)) {
+				purple_debug_error("dnssrv","unable to read srv "
+						"response: %s\n", g_strerror(errno));
+				size = 0;
+				g_free(res);
+				res = NULL;
+			}
 		}
 	}
 	else