Description: various memory-related patches
 clang-reported return value and space gotchas, dynamic treescan buffer alloc
Author: наб <nabijaczleweli@nabijaczleweli.xyz>
Bug-Debian: http://bugs.debian.org/995996
Bug-Debian: http://bugs.debian.org/1002836

--- a/restore/dirs.c
+++ b/restore/dirs.c
@@ -183,16 +183,15 @@ extractdirs(int genmode)
 	int fd;
 	char xattr[XATTR_MAXSIZE];
 	int xattr_found = 0;
+	_Bool is_tmp = command != 'r' && command != 'R';
 	dump_ino_t ino;
 
 	Vprintf(stdout, "Extract directories from tape\n");
-	(void) snprintf(dirfile, sizeof(dirfile), "%s/rstdir%ld", tmpdir,
-		(long)dumpdate);
-	if (command != 'r' && command != 'R') {
-		(void) strncat(dirfile, "-XXXXXX",
-			sizeof(dirfile) - strlen(dirfile));
+	(void) snprintf(dirfile, sizeof(dirfile), "%s/rstdir%ld%s", tmpdir,
+		(long)dumpdate, is_tmp ? "-XXXXXX" : "");
+	if (is_tmp)
 		fd = mkstemp(dirfile);
-	} else
+	else
 		fd = open(dirfile, O_RDWR|O_CREAT|O_EXCL, 0666);
 	if (fd == -1 || (df = fdopen(fd, "w")) == NULL) {
 		if (fd != -1)
@@ -200,12 +199,10 @@ extractdirs(int genmode)
 		err(1, "cannot create directory temporary %s", dirfile);
 	}
 	if (genmode != 0) {
-		(void) snprintf(modefile, sizeof(modefile), "%s/rstmode%ld", tmpdir, (long)dumpdate);
-		if (command != 'r' && command != 'R') {
-			(void) strncat(modefile, "-XXXXXX",
-				sizeof(modefile) - strlen(modefile));
+		(void) snprintf(modefile, sizeof(modefile), "%s/rstmode%ld%s", tmpdir, (long)dumpdate, is_tmp ? "-XXXXXX" : "");
+		if (is_tmp)
 			fd = mkstemp(modefile);
-		} else
+		else
 			fd = open(modefile, O_RDWR|O_CREAT|O_EXCL, 0666);
 		if (fd == -1 || (mf = fdopen(fd, "w")) == NULL) {
 			if (fd != -1)
@@ -287,9 +284,8 @@ treescan(char *pname, dump_ino_t ino, lo
 {
 	struct inotab *itp;
 	struct direct *dp;
-	int namelen;
 	off_t bpt;
-	char locname[MAXPATHLEN + 1];
+	char *locname;
 
 	itp = inotablookup(ino);
 	if (itp == NULL) {
@@ -308,9 +304,6 @@ treescan(char *pname, dump_ino_t ino, lo
 	 * begin search through the directory
 	 * skipping over "." and ".."
 	 */
-	namelen = snprintf(locname, sizeof(locname), "%s/", pname);
-	if (namelen >= (int)sizeof(locname))
-		namelen = sizeof(locname) - 1;
 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
 	dp = rst_readdir(dirp); /* "." */
 	if (dp != NULL && strcmp(dp->d_name, ".") == 0)
@@ -328,15 +321,13 @@ treescan(char *pname, dump_ino_t ino, lo
 	 * a zero inode signals end of directory
 	 */
 	while (dp != NULL) {
-		locname[namelen] = '\0';
-		if (namelen + dp->d_namlen >= (int)sizeof(locname)) {
-			fprintf(stderr, "%s%s: name exceeds %ld char\n",
-				locname, dp->d_name, (long)sizeof(locname) - 1);
-		} else {
-			(void) strncat(locname, dp->d_name, (int)dp->d_namlen);
-			treescan(locname, dp->d_ino, todo);
-			rst_seekdir(dirp, bpt, itp->t_seekpt);
+		if (asprintf(&locname, "%s/%.*s", pname, (int)dp->d_namlen, dp->d_name) == -1) {
+			panic("Error: %s\n", strerror(errno));
+			abort();
 		}
+		treescan(locname, dp->d_ino, todo);
+		free(locname);
+		rst_seekdir(dirp, bpt, itp->t_seekpt);
 		dp = rst_readdir(dirp);
 		bpt = rst_telldir(dirp);
 	}
--- a/common/indexer_test.c
+++ b/common/indexer_test.c
@@ -105,7 +105,7 @@ mkchecksum(union u_spcl *tmpspcl)
 /*
  *
  */
-int
+void
 dump_inode(Indexer *indexer, struct stat *buf)
 {
 	struct dinode dinode;
@@ -176,12 +176,13 @@ dump_walk(Indexer *indexer, const char *
 		}
 	}
 	closedir(dirp);
+  return 0;
 }
 
 /*
  *
  */
-int
+void
 test_indexer(Indexer *indexer, const char *filename, const char *path)
 {
 	struct direct dp;
--- a/restore/xattr.c
+++ b/restore/xattr.c
@@ -314,7 +314,7 @@ fail:
 static int
 xattr_cb_list(char *name, char *value, int valuelen, int isSELinux, void *private)
 {
-	isSELinux;
+	(void) isSELinux;
 	value[valuelen] = '\0';
 	printf("EA: %s:%s\n", name, value);
 
@@ -330,7 +330,7 @@ xattr_cb_set(char *name, char *value, in
 	if (Nflag)
 		return GOOD;
 
-	isSELinux;
+	(void) isSELinux;
 #ifdef TRANSSELINUX			/*GAN6May06 SELinux MLS */
 	if (isSELinux)
 		err = lsetfilecon(path, value);
@@ -353,11 +353,11 @@ xattr_cb_compare(char *name, char *value
 	char valuef[XATTR_MAXSIZE];
 	int valuesz;
 
-	isSELinux;
+	(void) isSELinux;
 #ifdef TRANSSELINUX			/*GAN6May06 SELinux MLS */
 	if (isSELinux)
 	{
-		security_context_t con = NULL;
+		char *con = NULL;
 
 		if (lgetfilecon(path, &con) < 0) {
 			warn("%s: EA compare lgetfilecon failed\n", path);
@@ -513,7 +513,7 @@ xattr_walk(char *buffer, int (*xattr_cb)
 			convertcon = 0;	/*GAN24May06 only for selinux */
 
 		if (convertcon) {
-			security_context_t con = NULL;
+			char *con = NULL;
 			int err;
 
 			if (!transselinuxarg)
