76f8c5
From d2f5d424ba8752f9a9e9dad410546ec1b46caa0a Mon Sep 17 00:00:00 2001
76f8c5
From: Adam Reichold <adam.reichold@t-online.de>
76f8c5
Date: Tue, 6 Nov 2018 09:08:06 +0100
76f8c5
Subject: [PATCH] pdfdetach: Check for valid file name of embedded file before
76f8c5
 using it to determine save path.
76f8c5
76f8c5
Closes #660
76f8c5
---
76f8c5
 utils/pdfdetach.cc | 24 ++++++++++++++++++------
76f8c5
 1 file changed, 18 insertions(+), 6 deletions(-)
76f8c5
76f8c5
diff --git a/utils/pdfdetach.cc b/utils/pdfdetach.cc
76f8c5
index a8720c64..71fa8608 100644
76f8c5
--- a/utils/pdfdetach.cc
76f8c5
+++ b/utils/pdfdetach.cc
76f8c5
@@ -191,14 +191,18 @@ int main(int argc, char *argv[]) {
76f8c5
       fileSpec = static_cast<FileSpec *>(embeddedFiles->get(i));
76f8c5
       printf("%d: ", i+1);
76f8c5
       s1 = fileSpec->getFileName();
76f8c5
-      if ((s1->getChar(0) & 0xff) == 0xfe && (s1->getChar(1) & 0xff) == 0xff) {
76f8c5
+      if (!s1) {
76f8c5
+	exitCode = 3;
76f8c5
+	goto err2;
76f8c5
+      }
76f8c5
+      if (s1->hasUnicodeMarker()) {
76f8c5
         isUnicode = gTrue;
76f8c5
         j = 2;
76f8c5
       } else {
76f8c5
         isUnicode = gFalse;
76f8c5
         j = 0;
76f8c5
       }
76f8c5
-      while (j < fileSpec->getFileName()->getLength()) {
76f8c5
+      while (j < s1->getLength()) {
76f8c5
         if (isUnicode) {
76f8c5
           u = ((s1->getChar(j) & 0xff) << 8) | (s1->getChar(j+1) & 0xff);
76f8c5
           j += 2;
76f8c5
@@ -228,14 +232,18 @@ int main(int argc, char *argv[]) {
76f8c5
 	p = path;
76f8c5
       }
76f8c5
       s1 = fileSpec->getFileName();
76f8c5
-      if ((s1->getChar(0) & 0xff) == 0xfe && (s1->getChar(1) & 0xff) == 0xff) {
76f8c5
+      if (!s1) {
76f8c5
+	exitCode = 3;
76f8c5
+	goto err2;
76f8c5
+      }
76f8c5
+      if (s1->hasUnicodeMarker()) {
76f8c5
         isUnicode = gTrue;
76f8c5
         j = 2;
76f8c5
       } else {
76f8c5
         isUnicode = gFalse;
76f8c5
         j = 0;
76f8c5
       }
76f8c5
-      while (j < fileSpec->getFileName()->getLength()) {
76f8c5
+      while (j < s1->getLength()) {
76f8c5
         if (isUnicode) {
76f8c5
           u = ((s1->getChar(j) & 0xff) << 8) | (s1->getChar(j+1) & 0xff);
76f8c5
           j += 2;
76f8c5
@@ -276,14 +284,18 @@ int main(int argc, char *argv[]) {
76f8c5
     } else {
76f8c5
       p = path;
76f8c5
       s1 = fileSpec->getFileName();
76f8c5
-      if ((s1->getChar(0) & 0xff) == 0xfe && (s1->getChar(1) & 0xff) == 0xff) {
76f8c5
+      if (!s1) {
76f8c5
+	exitCode = 3;
76f8c5
+	goto err2;
76f8c5
+      }
76f8c5
+      if (s1->hasUnicodeMarker()) {
76f8c5
         isUnicode = gTrue;
76f8c5
         j = 2;
76f8c5
       } else {
76f8c5
         isUnicode = gFalse;
76f8c5
         j = 0;
76f8c5
       }
76f8c5
-      while (j < fileSpec->getFileName()->getLength()) {
76f8c5
+      while (j < s1->getLength()) {
76f8c5
         if (isUnicode) {
76f8c5
           u = ((s1->getChar(j) & 0xff) << 8) | (s1->getChar(j+1) & 0xff);
76f8c5
           j += 2;
76f8c5
-- 
76f8c5
2.19.1
76f8c5