[PATCH] Bug fix for file comparison

Florian Zumbiehl
2019-03-07T07:41:51Z

Hi,

below you find a patch fixing a bug in the file comparison code: The
current code skips the unchanged_file() check when that check succeeded for
an earlier candidate, which could lead to accepting a file with a different
checksum as unchanged, for example.

Please be aware that the patch is untested.

Florian

diff --git a/generator.c b/generator.c
index a112da6..2df1b28 100644
--- a/generator.c
+++ b/generator.c
@@ -876,26 +876,22 @@ static int try_dests_reg(struct file_struct *file, char
*fname, int ndx,
 		pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
 		if (link_stat(cmpbuf, &sxp->st, 0) < 0 ||
!S_ISREG(sxp->st.st_mode))
 			continue;
-		switch (match_level) {
-		case 0:
+		if (match_level < 1) {
 			best_match = j;
 			match_level = 1;
-			/* FALL THROUGH */
-		case 1:
-			if (!unchanged_file(cmpbuf, file, &sxp->st))
-				continue;
+		}
+		if (!unchanged_file(cmpbuf, file, &sxp->st))
+			continue;
+		if (match_level < 2) {
 			best_match = j;
 			match_level = 2;
-			/* FALL THROUGH */
-		case 2:
-			if (!unchanged_attrs(cmpbuf, file, sxp)) {
-				free_stat_x(sxp);
-				continue;
-			}
-			best_match = j;
-			match_level = 3;
-			break;
 		}
+		if (!unchanged_attrs(cmpbuf, file, sxp)) {
+			free_stat_x(sxp);
+			continue;
+		}
+		best_match = j;
+		match_level = 3;
 		break;
 	} while (basis_dir[++j] != NULL);