[Debburn-changes] r676 - cdrkit/trunk/icedax

Peter Samuelson peters-guest at alioth.debian.org
Sat Jan 6 04:19:16 CET 2007


Author: peters-guest
Date: 2007-01-06 04:19:16 +0100 (Sat, 06 Jan 2007)
New Revision: 676

Modified:
   cdrkit/trunk/icedax/toc.c
Log:
icedax/toc.c: fix CDDB query, based on a patch from Heiko Schlenker.

His patch demonstrates that the writen() function is dumb and should be
replaced by writez() which writes a null-terminated buffer.  So, do
that as well.  It looks nicer, and would have prevented this bug.


Modified: cdrkit/trunk/icedax/toc.c
===================================================================
--- cdrkit/trunk/icedax/toc.c	2007-01-05 19:09:03 UTC (rev 675)
+++ cdrkit/trunk/icedax/toc.c	2007-01-06 03:19:16 UTC (rev 676)
@@ -804,9 +804,6 @@
 #if	defined	USE_REMOTE
 #include <pwd.h>
 
-static int readn(register int fd, register char *ptr, register int nbytes);
-static int writen(register int fd, register char *ptr, register int nbytes);
-
 static int readn(register int fd, register char *ptr, register int nbytes)
 {
 	int	nread;
@@ -826,13 +823,14 @@
 	return nread;
 }
 
-static int writen(register int fd, register char *ptr, register int nbytes)
+static ssize_t writez(int fd, const char *ptr)
 {
-	int	nleft, nwritten;
+	size_t nleft, nbytes;
 
-	nleft = nbytes;
+	nleft = nbytes = strlen(ptr);
+
 	while (nleft > 0) {
-		nwritten = write(fd, ptr, nleft);
+		ssize_t nwritten = write(fd, ptr, nleft);
 		if (nwritten <= 0) {
 			return nwritten;	/* return error */
 		}
@@ -1209,7 +1207,7 @@
 	if (0 > gethostname(hostname, sizeof(hostname)))
 		strcpy(hostname, "unknown_host"); 
 	hostname[sizeof(hostname)-1] = '\0';
-	writen(sock_fd, "cddb hello ", 11);
+	writez(sock_fd, "cddb hello ");
 	if (pw != NULL) {
 		BOOL	space_err = FALSE;
 		BOOL	ascii_err = FALSE;
@@ -1236,11 +1234,11 @@
 			}
 			q++;
 		}
-		writen(sock_fd, pw->pw_name, strlen(pw->pw_name));
+		writez(sock_fd, pw->pw_name);
+		writez(sock_fd, " ");
 	} else {
-		writen(sock_fd, "unknown", 7);
+		writez(sock_fd, "unknown ");
 	}
-	writen(sock_fd, " ", 1);
 
 	/* change spaces to underscores */
 	{
@@ -1271,10 +1269,8 @@
 		}
 	}
 
-	writen(sock_fd, hostname, strlen(hostname));
-	writen(sock_fd, " icedax ", 10);
-	writen(sock_fd, VERSION, strlen(VERSION));
-	writen(sock_fd, "\n", 1);
+	writez(sock_fd, hostname);
+	writez(sock_fd, " icedax " VERSION "\n");
 
 	readbytes = readn(sock_fd, inbuff, sizeof(inbuff));
 	if (readbytes < 0) {
@@ -1293,7 +1289,7 @@
 	}
 
 	/* enable new protocol variant. Weird command here, no cddb prefix ?!?! */
-	writen(sock_fd, "proto\n", 6);
+	writez(sock_fd, "proto\n");
 	readbytes = readn(sock_fd, inbuff, sizeof(inbuff));
 	if (readbytes < 0) {
 		fprintf(stderr, "Could not read from socket\n");
@@ -1323,7 +1319,7 @@
 				if (pr_level > 5)
 					pr_level = 5;
 				sprintf(inbuff, "proto %1u\n", pr_level);
-				writen(sock_fd, inbuff, 8);
+				writez(sock_fd, inbuff);
 				readbytes = readn(sock_fd, inbuff, sizeof(inbuff));
 				if (readbytes < 0) {
 					fprintf(stderr, "Could not read from socket\n");
@@ -1379,7 +1375,7 @@
 #endif
 /*	strcpy(outbuff, "cddb query 9709210c 12 150 12010 33557 50765 65380 81467 93235 109115 124135 137732 152575 166742 2339\n"); */
 /*	strcpy(outbuff, "cddb query 03015501 1 296 344\n"); */
-	writen(sock_fd, outbuff, strlen(outbuff));
+	writez(sock_fd, outbuff);
 
 	readbytes = readn(sock_fd, inbuff, sizeof(inbuff) - 1);
 	if (readbytes < 0) {
@@ -1440,7 +1436,7 @@
 
 	/* read */
 	sprintf(inbuff, "cddb read %s %08x\n", category, disc_id);
-	writen(sock_fd, inbuff, strlen(inbuff));
+	writez(sock_fd, inbuff);
 
 	/* read status and first buffer size. */
 	readbytes = readn(sock_fd, inbuff, sizeof(inbuff));
@@ -1465,7 +1461,7 @@
 
 signoff:
 	/* sign-off */
-	writen(sock_fd, "quit\n", 5);
+	writez(sock_fd, "quit\n");
 	readbytes = readn(sock_fd, inbuff, sizeof(inbuff));
 	if (readbytes < 0) {
 		fprintf(stderr, "Could not read from socket\n");




More information about the Debburn-changes mailing list