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