Browse code

PDF: Fix error Attempt to allocate 0 bytes

The PDF parser currently prints verbose error messages when attempting
to shrink a buffer down to actual data length after decoding if it turns
out that the decoded stream was empty (0 bytes). With exception to the
verbose error messages, there's no real behavior issue.

This commit fixes the issue by checking if any bytes were decoded before
attempting to shrink the buffer.

Micah Snyder (micasnyd) authored on 2020/04/07 07:03:20
Showing 1 changed files
... ...
@@ -638,8 +638,11 @@ static cl_error_t filter_rldecode(struct pdf_struct *pdf, struct pdf_obj *obj, s
638 638
     }
639 639
 
640 640
     if (rc == CL_SUCCESS) {
641
-        /* Shrink output buffer to final the decoded data length to minimize RAM usage */
642
-        if (!(temp = cli_realloc(decoded, declen))) {
641
+        if (declen == 0) {
642
+            cli_dbgmsg("cli_pdf: empty stream after inflation completed.\n");
643
+            rc = CL_BREAK;
644
+        } else if (!(temp = cli_realloc(decoded, declen))) {
645
+            /* Shrink output buffer to final the decoded data length to minimize RAM usage */
643 646
             cli_errmsg("cli_pdf: cannot reallocate memory for decoded output\n");
644 647
             rc = CL_EMEM;
645 648
         } else {
... ...
@@ -647,7 +650,7 @@ static cl_error_t filter_rldecode(struct pdf_struct *pdf, struct pdf_obj *obj, s
647 647
         }
648 648
     }
649 649
 
650
-    if (rc == CL_SUCCESS) {
650
+    if (rc == CL_SUCCESS || rc == CL_BREAK) {
651 651
         free(token->content);
652 652
 
653 653
         cli_dbgmsg("cli_pdf: decoded %lu bytes from %lu total bytes\n",
... ...
@@ -817,8 +820,11 @@ static cl_error_t filter_flatedecode(struct pdf_struct *pdf, struct pdf_obj *obj
817 817
     (void)inflateEnd(&stream);
818 818
 
819 819
     if (rc == CL_SUCCESS) {
820
-        /* Shrink output buffer to final the decoded data length to minimize RAM usage */
821
-        if (!(temp = cli_realloc(decoded, declen))) {
820
+        if (declen == 0) {
821
+            cli_dbgmsg("cli_pdf: empty stream after inflation completed.\n");
822
+            rc = CL_BREAK;
823
+        } else if (!(temp = cli_realloc(decoded, declen))) {
824
+            /* Shrink output buffer to final the decoded data length to minimize RAM usage */
822 825
             cli_errmsg("cli_pdf: cannot reallocate memory for decoded output\n");
823 826
             rc = CL_EMEM;
824 827
         } else {
... ...
@@ -826,7 +832,7 @@ static cl_error_t filter_flatedecode(struct pdf_struct *pdf, struct pdf_obj *obj
826 826
         }
827 827
     }
828 828
 
829
-    if (rc == CL_SUCCESS) {
829
+    if (rc == CL_SUCCESS || rc == CL_BREAK) {
830 830
         free(token->content);
831 831
 
832 832
         token->content = decoded;
... ...
@@ -1099,8 +1105,11 @@ static cl_error_t filter_lzwdecode(struct pdf_struct *pdf, struct pdf_obj *obj,
1099 1099
     (void)lzwInflateEnd(&stream);
1100 1100
 
1101 1101
     if (rc == CL_SUCCESS) {
1102
-        /* Shrink output buffer to final the decoded data length to minimize RAM usage */
1103
-        if (!(temp = cli_realloc(decoded, declen))) {
1102
+        if (declen == 0) {
1103
+            cli_dbgmsg("cli_pdf: empty stream after inflation completed.\n");
1104
+            rc = CL_BREAK;
1105
+        } else if (!(temp = cli_realloc(decoded, declen))) {
1106
+            /* Shrink output buffer to final the decoded data length to minimize RAM usage */
1104 1107
             cli_errmsg("cli_pdf: cannot reallocate memory for decoded output\n");
1105 1108
             rc = CL_EMEM;
1106 1109
         } else {
... ...
@@ -1108,7 +1117,7 @@ static cl_error_t filter_lzwdecode(struct pdf_struct *pdf, struct pdf_obj *obj,
1108 1108
         }
1109 1109
     }
1110 1110
 
1111
-    if (rc == CL_SUCCESS) {
1111
+    if (rc == CL_SUCCESS || rc == CL_BREAK) {
1112 1112
         free(token->content);
1113 1113
 
1114 1114
         token->content = decoded;