Skip to content

Commit c3d2f07

Browse files
authored
1.x: Fix NULL-pointer dereference when parsing %%PDFTOPDF comments (#644)
* Fix null pointer dereference in %%PDFTOPDF* parsers * %%PDFTOPDFCollate comment parsing: Increment p where needed Without this, if strchr succeeds, p will point to a ':' character. *p will therefore never be a whitespace in the subsequent loop, or compare successfully against the "true" string.
1 parent aea8d0d commit c3d2f07

File tree

4 files changed

+54
-33
lines changed

4 files changed

+54
-33
lines changed

filter/gstoraster.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,21 @@ parse_pdf_header_options(FILE *fp, gs_page_header *h)
104104
char *p;
105105

106106
p = strchr(buf+19,':');
107-
h->NumCopies = atoi(p+1);
107+
if (p) {
108+
h->NumCopies = atoi(p+1);
109+
}
108110
} else if (strncmp(buf,"%%PDFTOPDFCollate",17) == 0) {
109111
char *p;
110112

111113
p = strchr(buf+17,':');
112-
while (*p == ' ' || *p == '\t') p++;
113-
if (strncasecmp(p,"true",4) == 0) {
114-
h->Collate = CUPS_TRUE;
115-
} else {
116-
h->Collate = CUPS_FALSE;
114+
if (p) {
115+
p++;
116+
while (*p == ' ' || *p == '\t') p++;
117+
if (strncasecmp(p,"true",4) == 0) {
118+
h->Collate = CUPS_TRUE;
119+
} else {
120+
h->Collate = CUPS_FALSE;
121+
}
117122
}
118123
}
119124
}

filter/mupdftoraster.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,21 @@ parse_pdf_header_options(FILE *fp, mupdf_page_header *h)
102102
char *p;
103103

104104
p = strchr(buf+19,':');
105-
h->NumCopies = atoi(p+1);
105+
if (p) {
106+
h->NumCopies = atoi(p+1);
107+
}
106108
} else if (strncmp(buf,"%%PDFTOPDFCollate",17) == 0) {
107109
char *p;
108110

109111
p = strchr(buf+17,':');
110-
while (*p == ' ' || *p == '\t') p++;
111-
if (strncasecmp(p,"true",4) == 0) {
112-
h->Collate = CUPS_TRUE;
113-
} else {
114-
h->Collate = CUPS_FALSE;
112+
if (p) {
113+
p++;
114+
while (*p == ' ' || *p == '\t') p++;
115+
if (strncasecmp(p,"true",4) == 0) {
116+
h->Collate = CUPS_TRUE;
117+
} else {
118+
h->Collate = CUPS_FALSE;
119+
}
115120
}
116121
}
117122
}

filter/pdftops.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,25 +138,31 @@ static void parsePDFTOPDFComment(char *filename)
138138
if (strncmp(buf,"%%PDFTOPDFNumCopies",19) == 0) {
139139
char *p;
140140

141-
p = strchr(buf+19,':') + 1;
142-
while (*p == ' ' || *p == '\t') p++;
143-
strncpy(deviceCopies, p, sizeof(deviceCopies));
144-
deviceCopies[sizeof(deviceCopies) - 1] = '\0';
145-
p = deviceCopies + strlen(deviceCopies) - 1;
146-
while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') p--;
147-
*(p + 1) = '\0';
148-
pdftopdfapplied = 1;
141+
p = strchr(buf+19,':');
142+
if (p) {
143+
p++;
144+
while (*p == ' ' || *p == '\t') p++;
145+
strncpy(deviceCopies, p, sizeof(deviceCopies));
146+
deviceCopies[sizeof(deviceCopies) - 1] = '\0';
147+
p = deviceCopies + strlen(deviceCopies) - 1;
148+
while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') p--;
149+
*(p + 1) = '\0';
150+
pdftopdfapplied = 1;
151+
}
149152
} else if (strncmp(buf,"%%PDFTOPDFCollate",17) == 0) {
150153
char *p;
151154

152-
p = strchr(buf+17,':') + 1;
153-
while (*p == ' ' || *p == '\t') p++;
154-
if (strncasecmp(p,"true",4) == 0) {
155-
deviceCollate = 1;
156-
} else {
157-
deviceCollate = 0;
155+
p = strchr(buf+17,':');
156+
if (p) {
157+
p++;
158+
while (*p == ' ' || *p == '\t') p++;
159+
if (strncasecmp(p,"true",4) == 0) {
160+
deviceCollate = 1;
161+
} else {
162+
deviceCollate = 0;
163+
}
164+
pdftopdfapplied = 1;
158165
}
159-
pdftopdfapplied = 1;
160166
} else if (strcmp(buf,"% This file was generated by pdftopdf") == 0) {
161167
pdftopdfapplied = 1;
162168
}

filter/pdftoraster.cxx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -489,16 +489,21 @@ static void parsePDFTOPDFComment(FILE *fp)
489489
char *p;
490490

491491
p = strchr(buf+19,':');
492-
deviceCopies = atoi(p+1);
492+
if (p) {
493+
deviceCopies = atoi(p+1);
494+
}
493495
} else if (strncmp(buf,"%%PDFTOPDFCollate",17) == 0) {
494496
char *p;
495497

496498
p = strchr(buf+17,':');
497-
while (*p == ' ' || *p == '\t') p++;
498-
if (strncasecmp(p,"true",4) == 0) {
499-
deviceCollate = true;
500-
} else {
501-
deviceCollate = false;
499+
if (p) {
500+
p++;
501+
while (*p == ' ' || *p == '\t') p++;
502+
if (strncasecmp(p,"true",4) == 0) {
503+
deviceCollate = true;
504+
} else {
505+
deviceCollate = false;
506+
}
502507
}
503508
}
504509
}

0 commit comments

Comments
 (0)