Skip to content

Commit 2cdb814

Browse files
Prevent 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 2cdb814

File tree

4 files changed

+21
-221
lines changed

4 files changed

+21
-221
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: 21 additions & 40 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;
@@ -1194,7 +1192,7 @@ static int append_data(const void *data, size_t size, ZInfo *z_info, FILE *of,
11941192
int ret;
11951193
z_info->strm.avail_out = sizeof(z_info->buffer);
11961194
z_info->strm.next_out = z_info->buffer;
1197-
ret = zlib_deflate(&z_info->strm, Z_FINISH);
1195+
ret = deflate(&z_info->strm, Z_FINISH);
11981196
assert(ret != Z_STREAM_ERROR);
11991197
output_size = sizeof(z_info->buffer) - z_info->strm.avail_out;
12001198
if (output_size > 0) {
@@ -1206,7 +1204,7 @@ static int append_data(const void *data, size_t size, ZInfo *z_info, FILE *of,
12061204
} else {
12071205
// Only dump the output buffer when it is full.
12081206
do {
1209-
int ret = zlib_deflate(&z_info->strm, Z_NO_FLUSH);
1207+
int ret = deflate(&z_info->strm, Z_NO_FLUSH);
12101208
assert(ret != Z_STREAM_ERROR);
12111209
if (z_info->strm.avail_out == 0) {
12121210
if (fwrite(z_info->buffer, sizeof(z_info->buffer), 1, of) != 1) {
@@ -1476,7 +1474,8 @@ int acl_pkg_pack(const char *out_file, const char **input_files_dirs) {
14761474
z_info.strm.opaque = Z_NULL;
14771475
z_info.strm.avail_out = sizeof(z_info.buffer);
14781476
z_info.strm.next_out = z_info.buffer;
1479-
ret = zlib_deflateInit(&z_info.strm, Z_BEST_COMPRESSION);
1477+
ret = deflateInit_(&z_info.strm, Z_BEST_COMPRESSION, ZLIB_VERSION,
1478+
(int)sizeof(z_stream));
14801479
if (ret != Z_OK) {
14811480
fprintf(stderr, "acl_pkg_pack: Unable to initialize zlib for writing %s\n",
14821481
out_file);
@@ -1491,7 +1490,7 @@ int acl_pkg_pack(const char *out_file, const char **input_files_dirs) {
14911490
if (result == PACK_END) {
14921491
// We had a failure; stop here.
14931492
fclose(of);
1494-
zlib_deflateEnd(&z_info.strm);
1493+
deflateEnd(&z_info.strm);
14951494
return 0;
14961495
}
14971496
input_files_dirs++;
@@ -1505,10 +1504,10 @@ int acl_pkg_pack(const char *out_file, const char **input_files_dirs) {
15051504
if (fclose(of) != 0) {
15061505
fprintf(stderr, "acl_pkg_pack: Write of %s failed: %s\n", out_file,
15071506
strerror(errno));
1508-
zlib_deflateEnd(&z_info.strm);
1507+
deflateEnd(&z_info.strm);
15091508
return 0;
15101509
}
1511-
zlib_deflateEnd(&z_info.strm);
1510+
deflateEnd(&z_info.strm);
15121511
return 1 /* success */;
15131512
}
15141513

@@ -1534,7 +1533,7 @@ static int read_data(void *data, size_t size, ZInfo *z_info, FILE *in_fd) {
15341533
z_info->strm.next_in = z_info->buffer;
15351534
}
15361535
// Grab the next chunk of data from the input buffer.
1537-
ret = zlib_inflate(&z_info->strm, Z_NO_FLUSH);
1536+
ret = inflate(&z_info->strm, Z_NO_FLUSH);
15381537
assert(ret != Z_STREAM_ERROR);
15391538
if (ret == Z_STREAM_END) {
15401539
// Last bit of data.
@@ -1590,7 +1589,7 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
15901589
z_info.strm.avail_in = 0;
15911590
z_info.strm.next_in = NULL;
15921591
}
1593-
ret = zlib_inflateInit(&z_info.strm);
1592+
ret = inflateInit_(&z_info.strm, ZLIB_VERSION, (int)sizeof(z_stream));
15941593
if (ret != Z_OK) {
15951594
fprintf(stderr, "%s: Unable to initialize zlib for reading from buffer\n",
15961595
routine_name);
@@ -1611,13 +1610,13 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
16111610
acl_pkg_pack_info info;
16121611
if (!read_data(&info, sizeof(info), &z_info, input)) {
16131612
fprintf(stderr, "%s: Error reading from buffer\n", routine_name);
1614-
zlib_inflateEnd(&z_info.strm);
1613+
inflateEnd(&z_info.strm);
16151614
return 0;
16161615
}
16171616
if (info.magic != PACK_MAGIC) {
16181617
fprintf(stderr, "%s: Incorrect magic number read from buffer\n",
16191618
routine_name);
1620-
zlib_inflateEnd(&z_info.strm);
1619+
inflateEnd(&z_info.strm);
16211620
return 0;
16221621
}
16231622

@@ -1630,7 +1629,7 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
16301629
if (!read_data(name, info.name_length, &z_info, input)) {
16311630
fprintf(stderr, "%s: Error reading file name from buffer\n",
16321631
routine_name);
1633-
zlib_inflateEnd(&z_info.strm);
1632+
inflateEnd(&z_info.strm);
16341633
return 0;
16351634
}
16361635

@@ -1656,7 +1655,7 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
16561655
if (out_file == NULL) {
16571656
fprintf(stderr, "%s: Unable to open %s for writing: %s\n", routine_name,
16581657
full_name, strerror(errno));
1659-
zlib_inflateEnd(&z_info.strm);
1658+
inflateEnd(&z_info.strm);
16601659
return 0;
16611660
}
16621661
if (info.file_length > 0) {
@@ -1666,14 +1665,14 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
16661665
fprintf(stderr, "%s: Error reading file data for %s from buffer\n",
16671666
routine_name, full_name);
16681667
fclose(out_file);
1669-
zlib_inflateEnd(&z_info.strm);
1668+
inflateEnd(&z_info.strm);
16701669
return 0;
16711670
}
16721671
if (fwrite(buf, info.file_length, 1, out_file) != 1) {
16731672
fprintf(stderr, "%s: Failed to write to %s: %s\n", routine_name,
16741673
full_name, strerror(errno));
16751674
fclose(out_file);
1676-
zlib_inflateEnd(&z_info.strm);
1675+
inflateEnd(&z_info.strm);
16771676
return 0;
16781677
}
16791678
} else {
@@ -1683,23 +1682,23 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
16831682
routine_name, full_name, strerror(errno));
16841683
fclose(out_file);
16851684
free(buf2);
1686-
zlib_inflateEnd(&z_info.strm);
1685+
inflateEnd(&z_info.strm);
16871686
return PACK_END;
16881687
}
16891688
if (!read_data(buf2, info.file_length, &z_info, input)) {
16901689
fprintf(stderr, "%s: Error reading file data for %s from buffer\n",
16911690
routine_name, full_name);
16921691
fclose(out_file);
16931692
free(buf2);
1694-
zlib_inflateEnd(&z_info.strm);
1693+
inflateEnd(&z_info.strm);
16951694
return 0;
16961695
}
16971696
if (fwrite(buf2, info.file_length, 1, out_file) != 1) {
16981697
fprintf(stderr, "%s: Failed to write to %s: %s\n", routine_name,
16991698
full_name, strerror(errno));
17001699
fclose(out_file);
17011700
free(buf2);
1702-
zlib_inflateEnd(&z_info.strm);
1701+
inflateEnd(&z_info.strm);
17031702
return 0;
17041703
}
17051704
free(buf2);
@@ -1709,7 +1708,7 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
17091708
}
17101709
}
17111710

1712-
zlib_inflateEnd(&z_info.strm);
1711+
inflateEnd(&z_info.strm);
17131712
return 1;
17141713
}
17151714

@@ -1744,22 +1743,4 @@ int acl_pkg_unpack(const char *in_file, const char *out_dir) {
17441743
return ret;
17451744
}
17461745

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
1746+
#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)