2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 10:58:58 +00:00

Add C API qpdf_free_buffer to release memory allocated by stream data functions

This commit is contained in:
Chao Li(VISION) 2024-09-19 12:18:26 +00:00
parent ff2a78f579
commit 8c1cde4ec3
4 changed files with 22 additions and 3 deletions

View File

@ -927,6 +927,14 @@ extern "C" {
QPDF_ERROR_CODE qpdf_oh_get_page_content_data( QPDF_ERROR_CODE qpdf_oh_get_page_content_data(
qpdf_data qpdf, qpdf_oh page_oh, unsigned char** bufp, size_t* len); qpdf_data qpdf, qpdf_oh page_oh, unsigned char** bufp, size_t* len);
/* Call free to release the buffer allocated with malloc. This function can be used to free
* buffers that were dynamically allocated by qpdf functions such as qpdf_oh_get_stream_data
* or qpdf_oh_get_page_content_data. The caller is responsible for calling qpdf_free_buffer
* to manage memory properly and avoid memory leaks.
*/
QPDF_DLL
void qpdf_free_buffer(unsigned char** bufp);
/* The data pointed to by bufp will be copied by the library. It does not need to remain valid /* The data pointed to by bufp will be copied by the library. It does not need to remain valid
* after the call returns. * after the call returns.
*/ */

View File

@ -1771,6 +1771,16 @@ qpdf_oh_get_page_content_data(qpdf_data qpdf, qpdf_oh page_oh, unsigned char** b
}); });
} }
void
qpdf_free_buffer(unsigned char** bufp)
{
QTC::TC("qpdf", "qpdf-c called qpdf_free_buffer");
if (bufp && *bufp) {
free(*bufp);
*bufp = nullptr;
}
}
void void
qpdf_oh_replace_stream_data( qpdf_oh_replace_stream_data(
qpdf_data qpdf, qpdf_data qpdf,

View File

@ -1151,7 +1151,7 @@ test38(char const* infile, char const* password, char const* outfile, char const
assert(qpdf_oh_get_stream_data(qpdf, stream, qpdf_dl_none, 0, &buf, &len) == 0); assert(qpdf_oh_get_stream_data(qpdf, stream, qpdf_dl_none, 0, &buf, &len) == 0);
assert(len == 53); assert(len == 53);
assert(((int)buf[0] == 'x') && ((int)buf[1] == 0234)); assert(((int)buf[0] == 'x') && ((int)buf[1] == 0234));
free(buf); qpdf_free_buffer(&buf);
/* Test whether filterable */ /* Test whether filterable */
QPDF_BOOL filtered = QPDF_FALSE; QPDF_BOOL filtered = QPDF_FALSE;
@ -1169,8 +1169,8 @@ test38(char const* infile, char const* password, char const* outfile, char const
assert(qpdf_oh_get_page_content_data(qpdf, page2, &buf2, &len) == 0); assert(qpdf_oh_get_page_content_data(qpdf, page2, &buf2, &len) == 0);
assert(len == 47); assert(len == 47);
assert(memcmp(buf, buf2, len) == 0); assert(memcmp(buf, buf2, len) == 0);
free(buf); qpdf_free_buffer(&buf);
free(buf2); qpdf_free_buffer(&buf2);
/* errors */ /* errors */
printf("page content on broken page\n"); printf("page content on broken page\n");

View File

@ -626,6 +626,7 @@ qpdf-c stream data filtered set 1
qpdf-c stream data buf set 1 qpdf-c stream data buf set 1
qpdf-c called qpdf_oh_get_page_content_data 0 qpdf-c called qpdf_oh_get_page_content_data 0
qpdf-c called qpdf_oh_replace_stream_data 0 qpdf-c called qpdf_oh_replace_stream_data 0
qpdf-c called qpdf_free_buffer 0
qpdf-c silence oh errors 0 qpdf-c silence oh errors 0
qpdf-c called qpdf_oh_get_binary_string_value 0 qpdf-c called qpdf_oh_get_binary_string_value 0
qpdf-c called qpdf_oh_get_binary_utf8_value 0 qpdf-c called qpdf_oh_get_binary_utf8_value 0