From 09bdcdb1f3f2d7810ea956842bc7551d9ca26f92 Mon Sep 17 00:00:00 2001
From: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Date: Sat, 4 May 2019 22:57:29 +0200
Subject: rdsquashfs: more cleanup

Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
---
 unpack/rdsquashfs.c | 40 ++++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 16 deletions(-)

diff --git a/unpack/rdsquashfs.c b/unpack/rdsquashfs.c
index deba5be..3c80853 100644
--- a/unpack/rdsquashfs.c
+++ b/unpack/rdsquashfs.c
@@ -110,6 +110,26 @@ static char *get_path(char *old, const char *arg)
 	return path;
 }
 
+static int load_fragment_table(unsqfs_info_t *info, sqfs_super_t *super)
+{
+	if (super->fragment_entry_count == 0)
+		return 0;
+
+	if (super->flags & SQFS_FLAG_NO_FRAGMENTS)
+		return 0;
+
+	if (super->fragment_table_start >= super->bytes_used) {
+		fputs("Fragment table start is past end of file\n", stderr);
+		return -1;
+	}
+
+	info->frag = frag_reader_create(super, info->sqfsfd, info->cmp);
+	if (info->frag == NULL)
+		return -1;
+
+	return 0;
+}
+
 int main(int argc, char **argv)
 {
 	int i, status = EXIT_FAILURE, op = OP_NONE;
@@ -251,14 +271,8 @@ int main(int argc, char **argv)
 			goto out_fs;
 		}
 
-		if (super.fragment_entry_count > 0 &&
-		    super.fragment_table_start < super.bytes_used &&
-		    !(super.flags & SQFS_FLAG_NO_FRAGMENTS)) {
-			info.frag = frag_reader_create(&super, info.sqfsfd,
-						       info.cmp);
-			if (info.frag == NULL)
-				goto out_fs;
-		}
+		if (load_fragment_table(&info, &super))
+			goto out_fs;
 
 		if (extract_file(n->data.file, &info, STDOUT_FILENO))
 			goto out_fs;
@@ -274,14 +288,8 @@ int main(int argc, char **argv)
 			}
 		}
 
-		if (super.fragment_entry_count > 0 &&
-		    super.fragment_table_start < super.bytes_used &&
-		    !(super.flags & SQFS_FLAG_NO_FRAGMENTS)) {
-			info.frag = frag_reader_create(&super, info.sqfsfd,
-						       info.cmp);
-			if (info.frag == NULL)
-				goto out_fs;
-		}
+		if (load_fragment_table(&info, &super))
+			goto out_fs;
 
 		if (restore_fstree(unpack_root, n, &info))
 			goto out_fs;
-- 
cgit v1.2.3