b77848
From e77659a8c87272e5061738a31430d2111482c426 Mon Sep 17 00:00:00 2001
b77848
From: Remi Collet <remi@php.net>
b77848
Date: Tue, 10 Jun 2014 14:02:36 +0200
b77848
Subject: [PATCH] Fixed Bug #67410 fileinfo: mconvert incorrect handling of
b77848
 truncated pascal string size
b77848
b77848
Upstream
b77848
https://github.com/file/file/commit/27a14bc7ba285a0a5ebfdb55e54001aa11932b08
b77848
---
b77848
 ext/fileinfo/libmagic/softmagic.c | 14 +++++++++++---
b77848
 1 file changed, 11 insertions(+), 3 deletions(-)
b77848
b77848
diff --git a/ext/fileinfo/libmagic/softmagic.c b/ext/fileinfo/libmagic/softmagic.c
b77848
index 21fea6b..01e4977 100644
b77848
--- a/ext/fileinfo/libmagic/softmagic.c
b77848
+++ b/ext/fileinfo/libmagic/softmagic.c
b77848
@@ -881,10 +881,18 @@ mconvert(struct magic_set *ms, struct magic *m, int flip)
b77848
 		return 1;
b77848
 	}
b77848
 	case FILE_PSTRING: {
b77848
-		char *ptr1 = p->s, *ptr2 = ptr1 + file_pstring_length_size(m);
b77848
+		size_t sz = file_pstring_length_size(m);
b77848
+		char *ptr1 = p->s, *ptr2 = ptr1 + sz;
b77848
 		size_t len = file_pstring_get_length(m, ptr1);
b77848
-		if (len >= sizeof(p->s))
b77848
-			len = sizeof(p->s) - 1;
b77848
+		if (len >= sizeof(p->s)) {
b77848
+			/*
b77848
+			 * The size of the pascal string length (sz)
b77848
+			 * is 1, 2, or 4. We need at least 1 byte for NUL
b77848
+			 * termination, but we've already truncated the
b77848
+			 * string by p->s, so we need to deduct sz.
b77848
+			 */ 
b77848
+			len = sizeof(p->s) - sz;
b77848
+		}
b77848
 		while (len--)
b77848
 			*ptr1++ = *ptr2++;
b77848
 		*ptr1 = '\0';
b77848
-- 
b77848
1.9.2
b77848