Browse code

Part one of fixing a crash that happens when grabbing certain PDF properties

Shawn Webb authored on 2014/06/03 22:46:13
Showing 1 changed files
... ...
@@ -71,14 +71,15 @@ static	char	const	rcsid[] = "$Id: pdf.c,v 1.61 2007/02/12 20:46:09 njh Exp $";
71 71
  *Save the file being worked on in tmp */
72 72
 #endif
73 73
 
74
+struct pdf_struct;
75
+
74 76
 static	int	asciihexdecode(const char *buf, off_t len, char *output);
75 77
 static	int	ascii85decode(const char *buf, off_t len, unsigned char *output);
76 78
 static	const	char	*pdf_nextlinestart(const char *ptr, size_t len);
77 79
 static	const	char	*pdf_nextobject(const char *ptr, size_t len);
78
-static char *pdf_parse_string(const char *objstart, size_t objsize, const char *str);
80
+static char *pdf_parse_string(struct pdf_struct *pdf, const char *objstart, size_t objsize, const char *str);
79 81
 
80 82
 /* PDF statistics callbacks and related */
81
-struct pdf_struct;
82 83
 struct pdf_action;
83 84
 
84 85
 static void pdf_export_json(struct pdf_struct *);
... ...
@@ -2807,7 +2808,7 @@ pdf_nextobject(const char *ptr, size_t len)
2807 2807
     return NULL;
2808 2808
 }
2809 2809
 
2810
-static char *pdf_parse_string(const char *objstart, size_t objsize, const char *str)
2810
+static char *pdf_parse_string(struct pdf_struct *pdf, const char *objstart, size_t objsize, const char *str)
2811 2811
 {
2812 2812
     const char *q = objstart;
2813 2813
     char *p1, *p2;
... ...
@@ -2849,9 +2850,24 @@ static char *pdf_parse_string(const char *objstart, size_t objsize, const char *
2849 2849
         p1++;
2850 2850
     }
2851 2851
 
2852
-    if ((p1 - q) == objsize || *p1 != '(')
2852
+    if ((p1 - q) == objsize)
2853 2853
         return NULL;
2854 2854
 
2855
+    if (isdigit(p1[0])) {
2856
+        unsigned long objnum;
2857
+        char *end;
2858
+
2859
+        objnum = strtoul(p1, &end, 10);
2860
+        if ((end - p1) == 0)
2861
+            return NULL;
2862
+
2863
+        if (objnum > pdf->nobjs)
2864
+            return NULL;
2865
+
2866
+        res = NULL;
2867
+        return res;
2868
+    }
2869
+
2855 2870
     p2 = ++p1;
2856 2871
     while (1) {
2857 2872
         int shouldbreak=1;
... ...
@@ -3095,7 +3111,7 @@ static void Author_cb(struct pdf_struct *pdf, struct pdf_obj *obj, struct pdf_ac
3095 3095
         return;
3096 3096
 
3097 3097
     if (!(pdf->stats.author))
3098
-        pdf->stats.author = pdf_parse_string(obj->start + pdf->map, obj_size(pdf, obj, 1), "/Author");
3098
+        pdf->stats.author = pdf_parse_string(pdf, obj->start + pdf->map, obj_size(pdf, obj, 1), "/Author");
3099 3099
 #endif
3100 3100
 }
3101 3101
 
... ...
@@ -3106,7 +3122,7 @@ static void Creator_cb(struct pdf_struct *pdf, struct pdf_obj *obj, struct pdf_a
3106 3106
         return;
3107 3107
 
3108 3108
     if (!(pdf->stats.creator))
3109
-        pdf->stats.creator = pdf_parse_string(obj->start + pdf->map, obj_size(pdf, obj, 1), "/Creator");
3109
+        pdf->stats.creator = pdf_parse_string(pdf, obj->start + pdf->map, obj_size(pdf, obj, 1), "/Creator");
3110 3110
 #endif
3111 3111
 }
3112 3112
 
... ...
@@ -3117,7 +3133,7 @@ static void ModificationDate_cb(struct pdf_struct *pdf, struct pdf_obj *obj, str
3117 3117
         return;
3118 3118
 
3119 3119
     if (!(pdf->stats.modificationdate))
3120
-        pdf->stats.modificationdate = pdf_parse_string(obj->start + pdf->map, obj_size(pdf, obj, 1), "/ModDate");
3120
+        pdf->stats.modificationdate = pdf_parse_string(pdf, obj->start + pdf->map, obj_size(pdf, obj, 1), "/ModDate");
3121 3121
 #endif
3122 3122
 }
3123 3123
 
... ...
@@ -3128,7 +3144,7 @@ static void CreationDate_cb(struct pdf_struct *pdf, struct pdf_obj *obj, struct
3128 3128
         return;
3129 3129
 
3130 3130
     if (!(pdf->stats.creationdate))
3131
-        pdf->stats.creationdate = pdf_parse_string(obj->start + pdf->map, obj_size(pdf, obj, 1), "/CreationDate");
3131
+        pdf->stats.creationdate = pdf_parse_string(pdf, obj->start + pdf->map, obj_size(pdf, obj, 1), "/CreationDate");
3132 3132
 #endif
3133 3133
 }
3134 3134
 
... ...
@@ -3139,7 +3155,7 @@ static void Producer_cb(struct pdf_struct *pdf, struct pdf_obj *obj, struct pdf_
3139 3139
         return;
3140 3140
 
3141 3141
     if (!(pdf->stats.producer))
3142
-        pdf->stats.producer = pdf_parse_string(obj->start + pdf->map, obj_size(pdf, obj, 1), "/Producer");
3142
+        pdf->stats.producer = pdf_parse_string(pdf, obj->start + pdf->map, obj_size(pdf, obj, 1), "/Producer");
3143 3143
 #endif
3144 3144
 }
3145 3145