Index: /libmpc/trunk/mpcgain/mpcgain.c
===================================================================
--- /libmpc/trunk/mpcgain/mpcgain.c	(revision 441)
+++ /libmpc/trunk/mpcgain/mpcgain.c	(revision 442)
@@ -44,5 +44,5 @@
 #define MPCGAIN_MAJOR 0
 #define MPCGAIN_MINOR 9
-#define MPCGAIN_BUILD 2
+#define MPCGAIN_BUILD 3
 
 #define _cat(a,b,c) #a"."#b"."#c
@@ -270,7 +270,9 @@
 		r.count = 8;
 
-		size = mpc_bits_get_block(&r, &b);
-
-		while (memcmp(b.key, "RG", 2) != 0 ) {
+		for(;;) {
+			size = mpc_bits_get_block(&r, &b);
+			if (mpc_check_key(b.key) != MPC_STATUS_OK) break;
+
+			if (memcmp(b.key, "RG", 2) == 0) break;
 			header_pos[j] += b.size + size;
 			fseek(file, header_pos[j], SEEK_SET);
@@ -278,7 +280,11 @@
 			r.buff = buffer;
 			r.count = 8;
-			size = mpc_bits_get_block(&r, &b);
-		}
-
+		}
+
+		if (memcmp(b.key, "RG", 2) != 0 || b.size < 9) { //check for the loop above having aborted without finding the packet we want to update
+			fprintf(stderr, "Unsupported file format or corrupted file : %s\n", argv[j + 1]);
+			fclose(file);
+			continue;
+		}
 		header_pos[j] += size;
 
