Skip to content

Commit

Permalink
fixed : crash if pdf stream length is zero after decompress
Browse files Browse the repository at this point in the history
in flate_decode_filter() sometimes decompressed stream length is zero,
in such case, realloc() frees memory.
this causes memory corruption when we free the memory again.
  • Loading branch information
ksharindam committed Nov 28, 2023
1 parent e640f40 commit 86b55ce
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/pdf_doc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -794,10 +794,14 @@ PdfDocument:: newFontObject(const char *font_name)

static void pdf_stream_prepend(PdfObject *stream, const char *str, int len)
{
if (len==0 or str==NULL)
return;
char *new_stream = (char*) malloc2(len + stream->stream->len);
memcpy(new_stream, str, len);
if (stream->stream->len!=0) {
memcpy(new_stream+len, stream->stream->stream, stream->stream->len);
}
if (stream->stream->stream){
free(stream->stream->stream);
}
stream->stream->stream = new_stream;
Expand All @@ -807,6 +811,8 @@ static void pdf_stream_prepend(PdfObject *stream, const char *str, int len)
// get a stream object and append a char stream to it
static void pdf_stream_append(PdfObject *stream, const char *str, int len)
{
if (len==0 or str==NULL)
return;
int old_len = stream->stream->len;
stream->stream->len += len;
stream->stream->stream = (char*) realloc(stream->stream->stream, stream->stream->len);
Expand Down
4 changes: 4 additions & 0 deletions src/pdf_filters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ int flate_decode_filter(char **stream, size_t *len, DictObj &dict)
if (buff){
new_stream_content = buff;
}
// new_stream_len my be zero after decompress, in that case realloc() frees memory
if (!new_stream_len){
new_stream_content = NULL;
}
*stream = new_stream_content;
*len = new_stream_len;
return 0;
Expand Down
4 changes: 3 additions & 1 deletion src/pdf_objects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ PdfObject:: PdfObject() {
void
PdfObject:: setType(ObjectType obj_type)
{
type = obj_type;//TODO : if prev type is not PDF_OBJ_UNKNOWN , clear()
if (obj_type!=PDF_OBJ_UNKNOWN)
this->clear();
type = obj_type;
switch (type)
{
case PDF_OBJ_DICT:
Expand Down

0 comments on commit 86b55ce

Please sign in to comment.