[PATCH] Fixed bug when copying many files

Juan Perez-Sanchez <[email protected]>
Newsgroups org.kernel.vger.linux-8086
Message-ID <CAD6VGuYDDYckt_u=2jr2Ma5eeC=6Ea5eUD8kCx58PO85fmF8Yg@mail.gmail.com>
Hi,

    Attached is a patch to fix a very old bug, the #6 bug listed in
the BUGS file in the elks source. It happened to be a memory leak and
null pointer dereferencing in the cp program. I tested the patch using
Qemu, copying the entire bin directory to a mounted hard disk image.
Now the copy operation succeeds. After the simulation, under linux,
fsck'ed the destination disk image and there were no errors. Also
diff'ed the copied files with the originals and they all matched.

Greetings,

Juan
elksT.patch (application/octet-stream, 956 B)
diff -Nurb elkscmd.orig/file_utils/cp.c elkscmd/file_utils/cp.c
--- elkscmd.orig/file_utils/cp.c	2012-08-18 13:28:59.000000000 -0500
+++ elkscmd/file_utils/cp.c	2012-11-21 10:08:33.000000000 -0600
@@ -18,6 +18,10 @@
 #include <utime.h>
 #include <errno.h>
 
+#define BUF_SIZE 1024
+
+char *buf;
+
 void
 main(argc, argv)
 	char	**argv;
@@ -36,6 +40,7 @@
 		exit(1);
 	}
 
+	buf = malloc(BUF_SIZE);
 	while (argc-- > 2) {
 		srcname = argv[1];
 		destname = lastarg;
@@ -44,11 +49,10 @@
 
 		(void) copyfile(*++argv, destname, FALSE);
 	}
+	free(buf);
 	exit(0);
 }
 
-#define BUF_SIZE 1024 
-
 typedef	struct	chunk	CHUNK;
 #define	CHUNKINITSIZE	4
 struct	chunk	{
@@ -93,7 +97,6 @@
 	int		rcc;
 	int		wcc;
 	char		*bp;
-	char		*buf;
 	struct	stat	statbuf1;
 	struct	stat	statbuf2;
 	struct	utimbuf	times;
@@ -128,7 +131,6 @@
 		return FALSE;
 	}
 
-	buf = malloc(BUF_SIZE);
 	while ((rcc = read(rfd, buf, BUF_SIZE)) > 0) {
 		bp = buf;
 		while (rcc > 0) {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.