Skip to content

Commit 447ff31

Browse files
Remove zlib from being dynamically loaded
A pointer containing the dynamically loaded zlib library was not being properly cleaned up. This is picked up by our Coverity scans. One solution to this would be to store the handle to the library alongside the function pointers to functoin within that library. Then, we can free the library whenever we call `inflateEnd` or `deflateEnd`. However, this solution ended up not being necessary. Another solution (the one this commit implements) removes zlib from being dynamically loaded in the first place. This is possible since we already link zlib with the runtime during compilation, and so loading zlib dynamically is uncecessary. This fixes the following Coverity issue: ``` lib/pkg_editor/src/zlib.c:119:1: Type: Resource leak (RESOURCE_LEAK) lib/pkg_editor/src/zlib.c:98:3: 1. path: Condition "zlib_interface.deflateInit_", taking false branch. lib/pkg_editor/src/zlib.c:101:3: 2. alloc_fn: Storage is returned from allocation function "zlib_load_library". lib/pkg_editor/src/zlib.c:90:3: 2.1. path: Condition "dlopen_handle", taking false branch. lib/pkg_editor/src/zlib.c:93:3: 2.2. alloc_fn: Storage is returned from allocation function "dlopen". lib/pkg_editor/src/zlib.c:93:3: 2.3. return_alloc_fn: Directly returning storage allocated by "dlopen". lib/pkg_editor/src/zlib.c:101:3: 3. var_assign: Assigning: "library" = storage returned from "zlib_load_library()". lib/pkg_editor/src/zlib.c:102:3: 4. path: Condition "library == NULL", taking false branch. lib/pkg_editor/src/zlib.c:110:3: 5. noescape: Resource "library" is not freed or pointed-to in "load_one_symbol". lib/pkg_editor/src/zlib.c:39:36: 5.1. noescape: "load_one_symbol(void *, char const *)" does not free or save its parameter "library". lib/pkg_editor/src/zlib.c:111:3: 6. noescape: Resource "library" is not freed or pointed-to in "load_one_symbol". lib/pkg_editor/src/zlib.c:39:36: 6.1. noescape: "load_one_symbol(void *, char const *)" does not free or save its parameter "library". lib/pkg_editor/src/zlib.c:112:3: 7. noescape: Resource "library" is not freed or pointed-to in "load_one_symbol". lib/pkg_editor/src/zlib.c:39:36: 7.1. noescape: "load_one_symbol(void *, char const *)" does not free or save its parameter "library". lib/pkg_editor/src/zlib.c:113:3: 8. noescape: Resource "library" is not freed or pointed-to in "load_one_symbol". lib/pkg_editor/src/zlib.c:39:36: 8.1. noescape: "load_one_symbol(void *, char const *)" does not free or save its parameter "library". lib/pkg_editor/src/zlib.c:114:3: 9. noescape: Resource "library" is not freed or pointed-to in "load_one_symbol". lib/pkg_editor/src/zlib.c:39:36: 9.1. noescape: "load_one_symbol(void *, char const *)" does not free or save its parameter "library". lib/pkg_editor/src/zlib.c:115:3: 10. noescape: Resource "library" is not freed or pointed-to in "load_one_symbol". lib/pkg_editor/src/zlib.c:39:36: 10.1. noescape: "load_one_symbol(void *, char const *)" does not free or save its parameter "library". lib/pkg_editor/src/zlib.c:119:1: 11. leaked_storage: Variable "library" going out of scope leaks the storage it points to. ```
1 parent b982e66 commit 447ff31

File tree

4 files changed

+20
-224
lines changed

4 files changed

+20
-224
lines changed

lib/pkg_editor/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ if(ZLIB_FOUND)
2020
target_compile_definitions(pkg_editor PRIVATE WINDOWS_ZLIB_PATH="${WINDOWS_ZLIB_DLL_PATH}")
2121
endif()
2222
target_link_libraries(pkg_editor PRIVATE ZLIB::ZLIB)
23-
target_sources(pkg_editor PRIVATE src/zlib.c)
2423
endif()
2524

2625
install(TARGETS pkg_editor

lib/pkg_editor/src/pkg_editor.c

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@
3939
#endif
4040

4141
#include "pkg_editor/pkg_editor.h"
42-
#if USE_ZLIB
43-
#include "zlib_interface.h"
44-
#endif
42+
#include "zlib.h"
4543

4644
typedef struct acl_pkg_file {
4745
const char *fname;
@@ -1160,8 +1158,6 @@ void acl_pkg_set_show_mode(acl_pkg_file *pkg, int show_mode) {
11601158
}
11611159
}
11621160

1163-
#if USE_ZLIB
1164-
11651161
typedef struct ZInfo {
11661162
z_stream strm;
11671163
unsigned char buffer[32 * 1024];
@@ -1194,7 +1190,7 @@ static int append_data(const void *data, size_t size, ZInfo *z_info, FILE *of,
11941190
int ret;
11951191
z_info->strm.avail_out = sizeof(z_info->buffer);
11961192
z_info->strm.next_out = z_info->buffer;
1197-
ret = zlib_deflate(&z_info->strm, Z_FINISH);
1193+
ret = deflate(&z_info->strm, Z_FINISH);
11981194
assert(ret != Z_STREAM_ERROR);
11991195
output_size = sizeof(z_info->buffer) - z_info->strm.avail_out;
12001196
if (output_size > 0) {
@@ -1206,7 +1202,7 @@ static int append_data(const void *data, size_t size, ZInfo *z_info, FILE *of,
12061202
} else {
12071203
// Only dump the output buffer when it is full.
12081204
do {
1209-
int ret = zlib_deflate(&z_info->strm, Z_NO_FLUSH);
1205+
int ret = deflate(&z_info->strm, Z_NO_FLUSH);
12101206
assert(ret != Z_STREAM_ERROR);
12111207
if (z_info->strm.avail_out == 0) {
12121208
if (fwrite(z_info->buffer, sizeof(z_info->buffer), 1, of) != 1) {
@@ -1476,7 +1472,8 @@ int acl_pkg_pack(const char *out_file, const char **input_files_dirs) {
14761472
z_info.strm.opaque = Z_NULL;
14771473
z_info.strm.avail_out = sizeof(z_info.buffer);
14781474
z_info.strm.next_out = z_info.buffer;
1479-
ret = zlib_deflateInit(&z_info.strm, Z_BEST_COMPRESSION);
1475+
deflateInit_(&z_info.strm, Z_BEST_COMPRESSION, ZLIB_VERSION,
1476+
(int)sizeof(z_stream));
14801477
if (ret != Z_OK) {
14811478
fprintf(stderr, "acl_pkg_pack: Unable to initialize zlib for writing %s\n",
14821479
out_file);
@@ -1491,7 +1488,7 @@ int acl_pkg_pack(const char *out_file, const char **input_files_dirs) {
14911488
if (result == PACK_END) {
14921489
// We had a failure; stop here.
14931490
fclose(of);
1494-
zlib_deflateEnd(&z_info.strm);
1491+
deflateEnd(&z_info.strm);
14951492
return 0;
14961493
}
14971494
input_files_dirs++;
@@ -1505,10 +1502,10 @@ int acl_pkg_pack(const char *out_file, const char **input_files_dirs) {
15051502
if (fclose(of) != 0) {
15061503
fprintf(stderr, "acl_pkg_pack: Write of %s failed: %s\n", out_file,
15071504
strerror(errno));
1508-
zlib_deflateEnd(&z_info.strm);
1505+
deflateEnd(&z_info.strm);
15091506
return 0;
15101507
}
1511-
zlib_deflateEnd(&z_info.strm);
1508+
deflateEnd(&z_info.strm);
15121509
return 1 /* success */;
15131510
}
15141511

@@ -1534,7 +1531,7 @@ static int read_data(void *data, size_t size, ZInfo *z_info, FILE *in_fd) {
15341531
z_info->strm.next_in = z_info->buffer;
15351532
}
15361533
// Grab the next chunk of data from the input buffer.
1537-
ret = zlib_inflate(&z_info->strm, Z_NO_FLUSH);
1534+
ret = inflate(&z_info->strm, Z_NO_FLUSH);
15381535
assert(ret != Z_STREAM_ERROR);
15391536
if (ret == Z_STREAM_END) {
15401537
// Last bit of data.
@@ -1590,7 +1587,7 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
15901587
z_info.strm.avail_in = 0;
15911588
z_info.strm.next_in = NULL;
15921589
}
1593-
ret = zlib_inflateInit(&z_info.strm);
1590+
ret = inflateInit_(&z_info.strm, ZLIB_VERSION, (int)sizeof(z_stream));
15941591
if (ret != Z_OK) {
15951592
fprintf(stderr, "%s: Unable to initialize zlib for reading from buffer\n",
15961593
routine_name);
@@ -1611,13 +1608,13 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
16111608
acl_pkg_pack_info info;
16121609
if (!read_data(&info, sizeof(info), &z_info, input)) {
16131610
fprintf(stderr, "%s: Error reading from buffer\n", routine_name);
1614-
zlib_inflateEnd(&z_info.strm);
1611+
inflateEnd(&z_info.strm);
16151612
return 0;
16161613
}
16171614
if (info.magic != PACK_MAGIC) {
16181615
fprintf(stderr, "%s: Incorrect magic number read from buffer\n",
16191616
routine_name);
1620-
zlib_inflateEnd(&z_info.strm);
1617+
inflateEnd(&z_info.strm);
16211618
return 0;
16221619
}
16231620

@@ -1630,7 +1627,7 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
16301627
if (!read_data(name, info.name_length, &z_info, input)) {
16311628
fprintf(stderr, "%s: Error reading file name from buffer\n",
16321629
routine_name);
1633-
zlib_inflateEnd(&z_info.strm);
1630+
inflateEnd(&z_info.strm);
16341631
return 0;
16351632
}
16361633

@@ -1656,7 +1653,7 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
16561653
if (out_file == NULL) {
16571654
fprintf(stderr, "%s: Unable to open %s for writing: %s\n", routine_name,
16581655
full_name, strerror(errno));
1659-
zlib_inflateEnd(&z_info.strm);
1656+
inflateEnd(&z_info.strm);
16601657
return 0;
16611658
}
16621659
if (info.file_length > 0) {
@@ -1666,14 +1663,14 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
16661663
fprintf(stderr, "%s: Error reading file data for %s from buffer\n",
16671664
routine_name, full_name);
16681665
fclose(out_file);
1669-
zlib_inflateEnd(&z_info.strm);
1666+
inflateEnd(&z_info.strm);
16701667
return 0;
16711668
}
16721669
if (fwrite(buf, info.file_length, 1, out_file) != 1) {
16731670
fprintf(stderr, "%s: Failed to write to %s: %s\n", routine_name,
16741671
full_name, strerror(errno));
16751672
fclose(out_file);
1676-
zlib_inflateEnd(&z_info.strm);
1673+
inflateEnd(&z_info.strm);
16771674
return 0;
16781675
}
16791676
} else {
@@ -1683,23 +1680,23 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
16831680
routine_name, full_name, strerror(errno));
16841681
fclose(out_file);
16851682
free(buf2);
1686-
zlib_inflateEnd(&z_info.strm);
1683+
inflateEnd(&z_info.strm);
16871684
return PACK_END;
16881685
}
16891686
if (!read_data(buf2, info.file_length, &z_info, input)) {
16901687
fprintf(stderr, "%s: Error reading file data for %s from buffer\n",
16911688
routine_name, full_name);
16921689
fclose(out_file);
16931690
free(buf2);
1694-
zlib_inflateEnd(&z_info.strm);
1691+
inflateEnd(&z_info.strm);
16951692
return 0;
16961693
}
16971694
if (fwrite(buf2, info.file_length, 1, out_file) != 1) {
16981695
fprintf(stderr, "%s: Failed to write to %s: %s\n", routine_name,
16991696
full_name, strerror(errno));
17001697
fclose(out_file);
17011698
free(buf2);
1702-
zlib_inflateEnd(&z_info.strm);
1699+
inflateEnd(&z_info.strm);
17031700
return 0;
17041701
}
17051702
free(buf2);
@@ -1709,7 +1706,7 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
17091706
}
17101707
}
17111708

1712-
zlib_inflateEnd(&z_info.strm);
1709+
inflateEnd(&z_info.strm);
17131710
return 1;
17141711
}
17151712

@@ -1743,23 +1740,3 @@ int acl_pkg_unpack(const char *in_file, const char *out_dir) {
17431740
}
17441741
return ret;
17451742
}
1746-
1747-
#else // USE_ZLIB
1748-
1749-
int acl_pkg_pack(const char *out_file, const char **input_files_dirs) {
1750-
// Not implemented if no ZLIB
1751-
return 0;
1752-
}
1753-
1754-
int acl_pkg_unpack(const char *in_file, const char *out_dir) {
1755-
// Not implemented if no ZLIB
1756-
return 0;
1757-
}
1758-
1759-
int acl_pkg_unpack_buffer(const char *buffer, size_t buffer_size,
1760-
const char *out_dir) {
1761-
// Not implemented if no ZLIB
1762-
return 0;
1763-
}
1764-
1765-
#endif // USE_ZLIB

lib/pkg_editor/src/zlib.c

Lines changed: 0 additions & 150 deletions
This file was deleted.

lib/pkg_editor/src/zlib_interface.h

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)