45e69e
From 61f79b8447c3ac8ab5a26e79e0c28053ffdccf75 Mon Sep 17 00:00:00 2001
45e69e
From: Albert Astals Cid <aacid@kde.org>
45e69e
Date: Wed, 23 Oct 2013 22:54:56 +0000
45e69e
Subject: Allow only one %d in the filename
45e69e
45e69e
Fixes crashes if you had %s and similar in the filename
45e69e
45e69e
Inspired from patch by Pedro Ribeiro <pedrib@gmail.com>
45e69e
45e69e
Bug #69434
45e69e
---
45e69e
diff --git a/utils/pdfseparate.cc b/utils/pdfseparate.cc
45e69e
index 1d4901b..6424d20 100644
45e69e
--- a/utils/pdfseparate.cc
45e69e
+++ b/utils/pdfseparate.cc
45e69e
@@ -20,6 +20,7 @@
45e69e
 #include "PDFDoc.h"
45e69e
 #include "ErrorCodes.h"
45e69e
 #include "GlobalParams.h"
45e69e
+#include <ctype.h>
45e69e
 
45e69e
 static int firstPage = 0;
45e69e
 static int lastPage = 0;
45e69e
@@ -63,9 +64,37 @@ bool extractPages (const char *srcFileName, const char *destFileName) {
45e69e
   if (firstPage == 0)
45e69e
     firstPage = 1;
45e69e
   if (firstPage != lastPage && strstr(destFileName, "%d") == NULL) {
45e69e
-    error(errSyntaxError, -1, "'{0:s}' must contain '%%d' if more than one page should be extracted", destFileName);
45e69e
+    error(errSyntaxError, -1, "'{0:s}' must contain '%d' if more than one page should be extracted", destFileName);
45e69e
     return false;
45e69e
   }
45e69e
+  
45e69e
+  // destFileName can have multiple %% and one %d
45e69e
+  // We use auxDestFileName to replace all the valid % appearances
45e69e
+  // by 'A' (random char that is not %), if at the end of replacing
45e69e
+  // any of the valid appearances there is still any % around, the
45e69e
+  // pattern is wrong
45e69e
+  char *auxDestFileName = strdup(destFileName);
45e69e
+  // %% can appear as many times as you want
45e69e
+  char *p = strstr(auxDestFileName, "%%");
45e69e
+  while (p != NULL) {
45e69e
+    *p = 'A';
45e69e
+    *(p + 1) = 'A';
45e69e
+    p = strstr(p, "%%"); 
45e69e
+  }
45e69e
+  // %d can appear only one time
45e69e
+  p = strstr(auxDestFileName, "%d");
45e69e
+  if (p != NULL) {
45e69e
+    *p = 'A';
45e69e
+  }
45e69e
+  // at this point any other % is wrong
45e69e
+  p = strstr(auxDestFileName, "%");
45e69e
+  if (p != NULL) {
45e69e
+    error(errSyntaxError, -1, "'{0:s}' can only contain one '%d' pattern", destFileName);
45e69e
+    free(auxDestFileName);
45e69e
+    return false;
45e69e
+  }
45e69e
+  free(auxDestFileName);
45e69e
+  
45e69e
   for (int pageNo = firstPage; pageNo <= lastPage; pageNo++) {
45e69e
     snprintf (pathName, sizeof (pathName) - 1, destFileName, pageNo);
45e69e
     GooString *gpageName = new GooString (pathName);
45e69e
--
45e69e
cgit v0.9.0.2-2-gbebe