svn commit: trunk/busybox: archival coreutils libbb miscutils netwo etc...

vda at busybox.net vda at busybox.net
Sun Nov 26 15:45:18 UTC 2006


Author: vda
Date: 2006-11-26 07:45:17 -0800 (Sun, 26 Nov 2006)
New Revision: 16673

Log:
small fixes:
fix xstrdup to not grossly overallocate memory
use xopen instean of xopen3 in several places
etc.


Modified:
   trunk/busybox/archival/tar.c
   trunk/busybox/coreutils/dd.c
   trunk/busybox/libbb/xfuncs.c
   trunk/busybox/miscutils/crontab.c
   trunk/busybox/miscutils/rx.c
   trunk/busybox/networking/ftpgetput.c
   trunk/busybox/networking/wget.c
   trunk/busybox/runit/svlogd.c
   trunk/busybox/util-linux/mdev.c


Changeset:
Modified: trunk/busybox/archival/tar.c
===================================================================
--- trunk/busybox/archival/tar.c	2006-11-26 15:42:03 UTC (rev 16672)
+++ trunk/busybox/archival/tar.c	2006-11-26 15:45:17 UTC (rev 16673)
@@ -851,7 +851,7 @@
 			tar_handle->src_fd = fileno(tar_stream);
 			tar_handle->seek = seek_by_read;
 		} else {
-			tar_handle->src_fd = xopen3(tar_filename, flags, 0666);
+			tar_handle->src_fd = xopen(tar_filename, flags);
 		}
 	}
 

Modified: trunk/busybox/coreutils/dd.c
===================================================================
--- trunk/busybox/coreutils/dd.c	2006-11-26 15:42:03 UTC (rev 16672)
+++ trunk/busybox/coreutils/dd.c	2006-11-26 15:45:17 UTC (rev 16673)
@@ -138,7 +138,7 @@
 		if (!seek && (flags & trunc_flag))
 			oflag |= O_TRUNC;
 
-		ofd = xopen3(outfile, oflag, 0666);
+		ofd = xopen(outfile, oflag);
 
 		if (seek && (flags & trunc_flag)) {
 			if (ftruncate(ofd, seek * obs) < 0) {

Modified: trunk/busybox/libbb/xfuncs.c
===================================================================
--- trunk/busybox/libbb/xfuncs.c	2006-11-26 15:42:03 UTC (rev 16672)
+++ trunk/busybox/libbb/xfuncs.c	2006-11-26 15:45:17 UTC (rev 16673)
@@ -57,7 +57,7 @@
 	if (s == NULL)
 		return NULL;
 
-	t = strdup (s);
+	t = strdup(s);
 
 	if (t == NULL)
 		bb_error_msg_and_die(bb_msg_memory_exhausted);
@@ -69,23 +69,33 @@
 // the (possibly truncated to length n) string into it.
 char * xstrndup(const char *s, int n)
 {
+	int m;
 	char *t;
 
 	if (ENABLE_DEBUG && s == NULL)
 		bb_error_msg_and_die("xstrndup bug");
 
-	/* TODO: think about xstrndup("abc", 10000)!!! */
-	t = xmalloc(++n);
+	/* We can just xmalloc(n+1) and strncpy into it, */
+	/* but think about xstrndup("abc", 10000) wastage! */
+	m = n;
+	t = (char*) s;
+	while (m) {
+		if (!*t) break;
+		m--; t++;
+	}
+	n = n - m;
+	t = xmalloc(n + 1);
+	t[n] = '\0';
 
-	return safe_strncpy(t,s,n);
+	return memcpy(t,s,n);
 }
 
 // Die if we can't open a file and return a FILE * to it.
 // Notice we haven't got xfread(), This is for use with fscanf() and friends.
 FILE *xfopen(const char *path, const char *mode)
 {
-	FILE *fp;
-	if ((fp = fopen(path, mode)) == NULL)
+	FILE *fp = fopen(path, mode);
+	if (fp == NULL)
 		bb_perror_msg_and_die("%s", path);
 	return fp;
 }
@@ -93,8 +103,8 @@
 // Die if we can't open an existing file and return an fd.
 int xopen(const char *pathname, int flags)
 {
-	if (ENABLE_DEBUG && (flags & O_CREAT))
-		bb_error_msg_and_die("xopen() with O_CREAT");
+	//if (ENABLE_DEBUG && (flags & O_CREAT))
+	//	bb_error_msg_and_die("xopen() with O_CREAT");
 
 	return xopen3(pathname, flags, 0666);
 }
@@ -142,7 +152,7 @@
 	return off;
 }
 
-// Die with supplied error message if this FILE * has ferror set.
+// Die with supplied filename if this FILE * has ferror set.
 void die_if_ferror(FILE *fp, const char *fn)
 {
 	if (ferror(fp)) {
@@ -214,7 +224,6 @@
 		bb_error_msg_and_die(bb_msg_memory_exhausted);
 }
 
-
 // Converts unsigned long long value into compact 4-char
 // representation. Examples: "1234", "1.2k", " 27M", "123T"
 // Fifth char is always '\0'
@@ -257,7 +266,6 @@
 	buf[4] = '\0';
 }
 
-
 // Convert unsigned integer to ascii, writing into supplied buffer.  A
 // truncated result is always null terminated (unless buflen is 0), and
 // contains the first few digits of the result ala strncpy.

Modified: trunk/busybox/miscutils/crontab.c
===================================================================
--- trunk/busybox/miscutils/crontab.c	2006-11-26 15:42:03 UTC (rev 16672)
+++ trunk/busybox/miscutils/crontab.c	2006-11-26 15:45:17 UTC (rev 16673)
@@ -156,6 +156,7 @@
 		break;
 	case EDIT:
 		{
+/* FIXME: messy code here! we have file copying helpers for this! */
 			FILE *fi;
 			int fd;
 			int n;
@@ -163,11 +164,12 @@
 
 			snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid());
 			fd = xopen3(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600);
+/* race, use fchown */
 			chown(tmp, getuid(), getgid());
 			fi = fopen(pas->pw_name, "r");
 			if (fi) {
 				while ((n = fread(buf, 1, sizeof(buf), fi)) > 0)
-					write(fd, buf, n);
+					full_write(fd, buf, n);
 			}
 			EditFile(caller, tmp);
 			remove(tmp);
@@ -178,6 +180,7 @@
 		/* fall through */
 	case REPLACE:
 		{
+/* same here */
 			char path[1024];
 			int fd;
 			int n;
@@ -186,7 +189,7 @@
 			fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600);
 			if (fd >= 0) {
 				while ((n = read(repFd, buf, sizeof(buf))) > 0) {
-					write(fd, buf, n);
+					full_write(fd, buf, n);
 				}
 				close(fd);
 				rename(path, pas->pw_name);

Modified: trunk/busybox/miscutils/rx.c
===================================================================
--- trunk/busybox/miscutils/rx.c	2006-11-26 15:42:03 UTC (rev 16672)
+++ trunk/busybox/miscutils/rx.c	2006-11-26 15:45:17 UTC (rev 16673)
@@ -263,7 +263,7 @@
 
 	fn = argv[1];
 	ttyfd = xopen(CURRENT_TTY, O_RDWR);
-	filefd = xopen3(fn, O_RDWR|O_CREAT|O_TRUNC, 0666);
+	filefd = xopen(fn, O_RDWR|O_CREAT|O_TRUNC);
 
 	if (tcgetattr(ttyfd, &tty) < 0)
 			bb_perror_msg_and_die("tcgetattr");

Modified: trunk/busybox/networking/ftpgetput.c
===================================================================
--- trunk/busybox/networking/ftpgetput.c	2006-11-26 15:42:03 UTC (rev 16672)
+++ trunk/busybox/networking/ftpgetput.c	2006-11-26 15:45:17 UTC (rev 16673)
@@ -166,7 +166,7 @@
 		if (do_continue) {
 			fd_local = xopen(local_path, O_APPEND | O_WRONLY);
 		} else {
-			fd_local = xopen3(local_path, O_CREAT | O_TRUNC | O_WRONLY, 0666);
+			fd_local = xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY);
 		}
 	}
 

Modified: trunk/busybox/networking/wget.c
===================================================================
--- trunk/busybox/networking/wget.c	2006-11-26 15:42:03 UTC (rev 16672)
+++ trunk/busybox/networking/wget.c	2006-11-26 15:45:17 UTC (rev 16673)
@@ -452,8 +452,8 @@
 
 	/* Do it before progressmeter (want to have nice error message) */
 	if (output_fd < 0)
-		output_fd = xopen3(fname_out,
-			O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0666);
+		output_fd = xopen(fname_out,
+			O_WRONLY|O_CREAT|O_EXCL|O_TRUNC);
 
 	if (!(opt & WGET_OPT_QUIET))
 		progressmeter(-1);

Modified: trunk/busybox/runit/svlogd.c
===================================================================
--- trunk/busybox/runit/svlogd.c	2006-11-26 15:42:03 UTC (rev 16672)
+++ trunk/busybox/runit/svlogd.c	2006-11-26 15:45:17 UTC (rev 16673)
@@ -148,19 +148,19 @@
 		if (fd_move(0, fd) == -1)
 			bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
 		ld->fnsave[26] = 't';
-		fd = xopen3(ld->fnsave, O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT, 0644);
+		fd = xopen(ld->fnsave, O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT);
 		if (fd_move(1, fd) == -1)
 			bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
 		fd = open_read("state");
 		if (fd == -1) {
 			if (errno != ENOENT)
 				bb_perror_msg_and_die(FATAL"cannot %s processor %s", "open state for", ld->name);
-			close(xopen3("state", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT, 0644));
+			close(xopen("state", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT));
 			fd = xopen("state", O_RDONLY|O_NDELAY);
 		}
 		if (fd_move(4, fd) == -1)
 			bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
-		fd = xopen3("newstate", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT, 0644);
+		fd = xopen("newstate", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT);
 		if (fd_move(5, fd) == -1)
 			bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
 

Modified: trunk/busybox/util-linux/mdev.c
===================================================================
--- trunk/busybox/util-linux/mdev.c	2006-11-26 15:42:03 UTC (rev 16672)
+++ trunk/busybox/util-linux/mdev.c	2006-11-26 15:45:17 UTC (rev 16673)
@@ -73,7 +73,7 @@
 
 			line++;
 			/* find end of this line */
-			for(end=pos; end-conf<len && *end!='\n'; end++)
+			for (end=pos; end-conf<len && *end!='\n'; end++)
 				;
 
 			/* Three fields: regex, uid:gid, mode */
@@ -111,7 +111,7 @@
 					char *s, *s2;
 
 					/* Find : */
-					for(s=pos; s<end2 && *s!=':'; s++)
+					for (s=pos; s<end2 && *s!=':'; s++)
 						;
 					if (s == end2) break;
 




More information about the busybox-cvs mailing list