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