Commit 638bf5f9ae8e6225cf13cb54b9f4952469cba8ab
Committed by
GitHub
Merge pull request #1287 from mslichao/mslichao/capifreebuf
Add C API qpdf_oh_free_buffer to release memory allocated by stream data functions
Showing
5 changed files
with
27 additions
and
3 deletions
ChangeLog
| 1 | +2024-09-20 Chao Li <mslichao@outlook.com> | ||
| 2 | + | ||
| 3 | + * Add C API qpdf_oh_free_buffer to release memory allocated by | ||
| 4 | + stream data functions. | ||
| 5 | + | ||
| 1 | 2024-08-25 M Holger <m.holger@qpdf.org> | 6 | 2024-08-25 M Holger <m.holger@qpdf.org> |
| 2 | 7 | ||
| 3 | * Add new command-line arguments --remove-metadata and --remove-info | 8 | * Add new command-line arguments --remove-metadata and --remove-info |
include/qpdf/qpdf-c.h
| @@ -927,6 +927,14 @@ extern "C" { | @@ -927,6 +927,14 @@ extern "C" { | ||
| 927 | QPDF_ERROR_CODE qpdf_oh_get_page_content_data( | 927 | QPDF_ERROR_CODE qpdf_oh_get_page_content_data( |
| 928 | qpdf_data qpdf, qpdf_oh page_oh, unsigned char** bufp, size_t* len); | 928 | qpdf_data qpdf, qpdf_oh page_oh, unsigned char** bufp, size_t* len); |
| 929 | 929 | ||
| 930 | + /* Call free to release the buffer allocated with malloc. This function can be used to free | ||
| 931 | + * buffers that were dynamically allocated by qpdf functions such as qpdf_oh_get_stream_data | ||
| 932 | + * or qpdf_oh_get_page_content_data. The caller is responsible for calling qpdf_oh_free_buffer | ||
| 933 | + * to manage memory properly and avoid memory leaks. | ||
| 934 | + */ | ||
| 935 | + QPDF_DLL | ||
| 936 | + void qpdf_oh_free_buffer(unsigned char** bufp); | ||
| 937 | + | ||
| 930 | /* The data pointed to by bufp will be copied by the library. It does not need to remain valid | 938 | /* The data pointed to by bufp will be copied by the library. It does not need to remain valid |
| 931 | * after the call returns. | 939 | * after the call returns. |
| 932 | */ | 940 | */ |
libqpdf/qpdf-c.cc
| @@ -1772,6 +1772,16 @@ qpdf_oh_get_page_content_data(qpdf_data qpdf, qpdf_oh page_oh, unsigned char** b | @@ -1772,6 +1772,16 @@ qpdf_oh_get_page_content_data(qpdf_data qpdf, qpdf_oh page_oh, unsigned char** b | ||
| 1772 | } | 1772 | } |
| 1773 | 1773 | ||
| 1774 | void | 1774 | void |
| 1775 | +qpdf_oh_free_buffer(unsigned char** bufp) | ||
| 1776 | +{ | ||
| 1777 | + QTC::TC("qpdf", "qpdf-c called qpdf_oh_free_buffer"); | ||
| 1778 | + if (bufp && *bufp) { | ||
| 1779 | + free(*bufp); | ||
| 1780 | + *bufp = nullptr; | ||
| 1781 | + } | ||
| 1782 | +} | ||
| 1783 | + | ||
| 1784 | +void | ||
| 1775 | qpdf_oh_replace_stream_data( | 1785 | qpdf_oh_replace_stream_data( |
| 1776 | qpdf_data qpdf, | 1786 | qpdf_data qpdf, |
| 1777 | qpdf_oh stream_oh, | 1787 | qpdf_oh stream_oh, |
qpdf/qpdf-ctest.c
| @@ -1151,7 +1151,7 @@ test38(char const* infile, char const* password, char const* outfile, char const | @@ -1151,7 +1151,7 @@ test38(char const* infile, char const* password, char const* outfile, char const | ||
| 1151 | assert(qpdf_oh_get_stream_data(qpdf, stream, qpdf_dl_none, 0, &buf, &len) == 0); | 1151 | assert(qpdf_oh_get_stream_data(qpdf, stream, qpdf_dl_none, 0, &buf, &len) == 0); |
| 1152 | assert(len == 53); | 1152 | assert(len == 53); |
| 1153 | assert(((int)buf[0] == 'x') && ((int)buf[1] == 0234)); | 1153 | assert(((int)buf[0] == 'x') && ((int)buf[1] == 0234)); |
| 1154 | - free(buf); | 1154 | + qpdf_oh_free_buffer(&buf); |
| 1155 | 1155 | ||
| 1156 | /* Test whether filterable */ | 1156 | /* Test whether filterable */ |
| 1157 | QPDF_BOOL filtered = QPDF_FALSE; | 1157 | QPDF_BOOL filtered = QPDF_FALSE; |
| @@ -1169,8 +1169,8 @@ test38(char const* infile, char const* password, char const* outfile, char const | @@ -1169,8 +1169,8 @@ test38(char const* infile, char const* password, char const* outfile, char const | ||
| 1169 | assert(qpdf_oh_get_page_content_data(qpdf, page2, &buf2, &len) == 0); | 1169 | assert(qpdf_oh_get_page_content_data(qpdf, page2, &buf2, &len) == 0); |
| 1170 | assert(len == 47); | 1170 | assert(len == 47); |
| 1171 | assert(memcmp(buf, buf2, len) == 0); | 1171 | assert(memcmp(buf, buf2, len) == 0); |
| 1172 | - free(buf); | ||
| 1173 | - free(buf2); | 1172 | + qpdf_oh_free_buffer(&buf); |
| 1173 | + qpdf_oh_free_buffer(&buf2); | ||
| 1174 | 1174 | ||
| 1175 | /* errors */ | 1175 | /* errors */ |
| 1176 | printf("page content on broken page\n"); | 1176 | printf("page content on broken page\n"); |
qpdf/qpdf.testcov
| @@ -627,6 +627,7 @@ qpdf-c stream data filtered set 1 | @@ -627,6 +627,7 @@ qpdf-c stream data filtered set 1 | ||
| 627 | qpdf-c stream data buf set 1 | 627 | qpdf-c stream data buf set 1 |
| 628 | qpdf-c called qpdf_oh_get_page_content_data 0 | 628 | qpdf-c called qpdf_oh_get_page_content_data 0 |
| 629 | qpdf-c called qpdf_oh_replace_stream_data 0 | 629 | qpdf-c called qpdf_oh_replace_stream_data 0 |
| 630 | +qpdf-c called qpdf_oh_free_buffer 0 | ||
| 630 | qpdf-c silence oh errors 0 | 631 | qpdf-c silence oh errors 0 |
| 631 | qpdf-c called qpdf_oh_get_binary_string_value 0 | 632 | qpdf-c called qpdf_oh_get_binary_string_value 0 |
| 632 | qpdf-c called qpdf_oh_get_binary_utf8_value 0 | 633 | qpdf-c called qpdf_oh_get_binary_utf8_value 0 |