Skip to content

Commit 05bdce1

Browse files
authored
Merge pull request #2 from jketema/amammad-cpp-bombs
C++: Fix zstd and clean up test
2 parents 4fa4624 + 3aa68b3 commit 05bdce1

File tree

3 files changed

+96
-57
lines changed

3 files changed

+96
-57
lines changed

cpp/ql/src/experimental/Security/CWE/CWE-409/ZSTD.qll

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,26 @@ class FreadOrDieFunctionStep extends DecompressionFlowStep {
6363

6464
override predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
6565
exists(FunctionCall fc | fc.getTarget().hasGlobalName("fread_orDie") |
66-
node1.asIndirectExpr() = fc.getArgument(2) and
66+
node1.asExpr() = fc.getArgument(2) and
6767
node2.asIndirectExpr() = fc.getArgument(0)
6868
)
6969
}
7070
}
71+
72+
/**
73+
* The `src` member of a `ZSTD_inBuffer` variable is used in a flow steps.
74+
*/
75+
class SrcMember extends DecompressionFlowStep {
76+
SrcMember() { this = "SrcMember" }
77+
78+
override predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
79+
exists(VariableAccess inBufferAccess, Field srcField, ClassAggregateLiteral c |
80+
inBufferAccess.getType().hasName("ZSTD_inBuffer") and
81+
srcField.hasName("src")
82+
|
83+
node2.asExpr() = inBufferAccess and
84+
inBufferAccess.getTarget().getInitializer().getExpr() = c and
85+
node1.asIndirectExpr() = c.getFieldExpr(srcField, _)
86+
)
87+
}
88+
}

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-409/DecompressionBombs/DecompressionBombs.expected

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,30 @@ edges
1616
| main.cpp:7:33:7:36 | **argv | main.cpp:9:27:9:30 | **argv | provenance | |
1717
| main.cpp:7:33:7:36 | **argv | main.cpp:10:24:10:27 | **argv | provenance | |
1818
| main.cpp:7:33:7:36 | **argv | main.cpp:11:21:11:24 | **argv | provenance | |
19+
| main.cpp:7:33:7:36 | **argv | main.cpp:12:21:12:24 | **argv | provenance | |
1920
| main.cpp:8:23:8:26 | **argv | brotliTest.cpp:15:41:15:44 | **argv | provenance | |
2021
| main.cpp:8:23:8:26 | **argv | main.cpp:8:23:8:26 | brotli_test output argument | provenance | |
2122
| main.cpp:8:23:8:26 | brotli_test output argument | main.cpp:9:27:9:30 | **argv | provenance | |
2223
| main.cpp:8:23:8:26 | brotli_test output argument | main.cpp:10:24:10:27 | **argv | provenance | |
2324
| main.cpp:8:23:8:26 | brotli_test output argument | main.cpp:11:21:11:24 | **argv | provenance | |
25+
| main.cpp:8:23:8:26 | brotli_test output argument | main.cpp:12:21:12:24 | **argv | provenance | |
2426
| main.cpp:9:27:9:30 | **argv | libarchiveTests.cpp:30:45:30:48 | **argv | provenance | |
2527
| main.cpp:9:27:9:30 | **argv | main.cpp:9:27:9:30 | libarchive_test output argument | provenance | |
2628
| main.cpp:9:27:9:30 | libarchive_test output argument | main.cpp:10:24:10:27 | **argv | provenance | |
2729
| main.cpp:9:27:9:30 | libarchive_test output argument | main.cpp:11:21:11:24 | **argv | provenance | |
30+
| main.cpp:9:27:9:30 | libarchive_test output argument | main.cpp:12:21:12:24 | **argv | provenance | |
2831
| main.cpp:10:24:10:27 | **argv | main.cpp:10:24:10:27 | minizip_test output argument | provenance | |
2932
| main.cpp:10:24:10:27 | **argv | minizipTest.cpp:12:42:12:45 | **argv | provenance | |
3033
| main.cpp:10:24:10:27 | minizip_test output argument | main.cpp:11:21:11:24 | **argv | provenance | |
34+
| main.cpp:10:24:10:27 | minizip_test output argument | main.cpp:12:21:12:24 | **argv | provenance | |
35+
| main.cpp:11:21:11:24 | **argv | main.cpp:11:21:11:24 | zlib_test output argument | provenance | |
36+
| main.cpp:11:21:11:24 | **argv | main.cpp:11:21:11:24 | zlib_test output argument | provenance | |
3137
| main.cpp:11:21:11:24 | **argv | zlibTest.cpp:80:33:80:36 | **argv | provenance | |
38+
| main.cpp:11:21:11:24 | zlib_test output argument | main.cpp:12:21:12:24 | **argv | provenance | |
39+
| main.cpp:11:21:11:24 | zlib_test output argument | main.cpp:12:21:12:24 | *argv | provenance | |
40+
| main.cpp:12:21:12:24 | **argv | zstdTest.cpp:26:39:26:42 | **argv | provenance | |
41+
| main.cpp:12:21:12:24 | *argv | zstdTest.cpp:26:39:26:42 | **argv | provenance | |
42+
| main.cpp:12:21:12:24 | *argv | zstdTest.cpp:26:39:26:42 | *argv | provenance | |
3243
| minizipTest.cpp:12:42:12:45 | **argv | minizipTest.cpp:12:42:12:45 | **argv | provenance | |
3344
| minizipTest.cpp:12:42:12:45 | **argv | minizipTest.cpp:17:52:17:67 | *access to array | provenance | |
3445
| minizipTest.cpp:12:42:12:45 | **argv | minizipTest.cpp:24:41:24:47 | *access to array | provenance | |
@@ -57,31 +68,62 @@ edges
5768
| zlibTest.cpp:58:22:58:27 | call to gzopen | zlibTest.cpp:62:25:62:31 | inFileZ | provenance | |
5869
| zlibTest.cpp:58:29:58:36 | *fileName | zlibTest.cpp:57:25:57:32 | *fileName | provenance | |
5970
| zlibTest.cpp:58:29:58:36 | *fileName | zlibTest.cpp:58:22:58:27 | call to gzopen | provenance | Config |
71+
| zlibTest.cpp:71:26:71:30 | *input | zlibTest.cpp:71:26:71:30 | *input | provenance | |
6072
| zlibTest.cpp:71:26:71:30 | *input | zlibTest.cpp:77:45:77:59 | *input | provenance | |
73+
| zlibTest.cpp:80:33:80:36 | **argv | zlibTest.cpp:80:33:80:36 | **argv | provenance | |
6174
| zlibTest.cpp:80:33:80:36 | **argv | zlibTest.cpp:81:19:81:25 | *access to array | provenance | |
6275
| zlibTest.cpp:80:33:80:36 | **argv | zlibTest.cpp:82:18:82:24 | *access to array | provenance | |
6376
| zlibTest.cpp:80:33:80:36 | **argv | zlibTest.cpp:83:19:83:25 | *access to array | provenance | |
6477
| zlibTest.cpp:80:33:80:36 | **argv | zlibTest.cpp:84:18:84:24 | *access to array | provenance | |
6578
| zlibTest.cpp:80:33:80:36 | **argv | zlibTest.cpp:85:19:85:25 | *access to array | provenance | |
6679
| zlibTest.cpp:81:19:81:25 | *access to array | zlibTest.cpp:47:26:47:33 | *fileName | provenance | |
6780
| zlibTest.cpp:81:19:81:25 | *access to array | zlibTest.cpp:81:19:81:25 | UnsafeGzfread output argument | provenance | |
81+
| zlibTest.cpp:81:19:81:25 | UnsafeGzfread output argument | zlibTest.cpp:80:33:80:36 | **argv | provenance | |
82+
| zlibTest.cpp:81:19:81:25 | UnsafeGzfread output argument | zlibTest.cpp:80:33:80:36 | **argv [Return] | provenance | |
6883
| zlibTest.cpp:81:19:81:25 | UnsafeGzfread output argument | zlibTest.cpp:82:18:82:24 | *access to array | provenance | |
6984
| zlibTest.cpp:81:19:81:25 | UnsafeGzfread output argument | zlibTest.cpp:83:19:83:25 | *access to array | provenance | |
7085
| zlibTest.cpp:81:19:81:25 | UnsafeGzfread output argument | zlibTest.cpp:84:18:84:24 | *access to array | provenance | |
7186
| zlibTest.cpp:81:19:81:25 | UnsafeGzfread output argument | zlibTest.cpp:85:19:85:25 | *access to array | provenance | |
7287
| zlibTest.cpp:82:18:82:24 | *access to array | zlibTest.cpp:57:25:57:32 | *fileName | provenance | |
7388
| zlibTest.cpp:82:18:82:24 | *access to array | zlibTest.cpp:82:18:82:24 | UnsafeGzgets output argument | provenance | |
89+
| zlibTest.cpp:82:18:82:24 | UnsafeGzgets output argument | zlibTest.cpp:80:33:80:36 | **argv | provenance | |
90+
| zlibTest.cpp:82:18:82:24 | UnsafeGzgets output argument | zlibTest.cpp:80:33:80:36 | **argv [Return] | provenance | |
7491
| zlibTest.cpp:82:18:82:24 | UnsafeGzgets output argument | zlibTest.cpp:83:19:83:25 | *access to array | provenance | |
7592
| zlibTest.cpp:82:18:82:24 | UnsafeGzgets output argument | zlibTest.cpp:84:18:84:24 | *access to array | provenance | |
7693
| zlibTest.cpp:82:18:82:24 | UnsafeGzgets output argument | zlibTest.cpp:85:19:85:25 | *access to array | provenance | |
7794
| zlibTest.cpp:83:19:83:25 | *access to array | zlibTest.cpp:16:26:16:30 | *input | provenance | |
7895
| zlibTest.cpp:83:19:83:25 | *access to array | zlibTest.cpp:83:19:83:25 | UnsafeInflate output argument | provenance | |
96+
| zlibTest.cpp:83:19:83:25 | UnsafeInflate output argument | zlibTest.cpp:80:33:80:36 | **argv | provenance | |
97+
| zlibTest.cpp:83:19:83:25 | UnsafeInflate output argument | zlibTest.cpp:80:33:80:36 | **argv [Return] | provenance | |
7998
| zlibTest.cpp:83:19:83:25 | UnsafeInflate output argument | zlibTest.cpp:84:18:84:24 | *access to array | provenance | |
8099
| zlibTest.cpp:83:19:83:25 | UnsafeInflate output argument | zlibTest.cpp:85:19:85:25 | *access to array | provenance | |
81100
| zlibTest.cpp:84:18:84:24 | *access to array | zlibTest.cpp:37:25:37:32 | *fileName | provenance | |
82101
| zlibTest.cpp:84:18:84:24 | *access to array | zlibTest.cpp:84:18:84:24 | UnsafeGzread output argument | provenance | |
102+
| zlibTest.cpp:84:18:84:24 | UnsafeGzread output argument | zlibTest.cpp:80:33:80:36 | **argv | provenance | |
103+
| zlibTest.cpp:84:18:84:24 | UnsafeGzread output argument | zlibTest.cpp:80:33:80:36 | **argv [Return] | provenance | |
83104
| zlibTest.cpp:84:18:84:24 | UnsafeGzread output argument | zlibTest.cpp:85:19:85:25 | *access to array | provenance | |
84105
| zlibTest.cpp:85:19:85:25 | *access to array | zlibTest.cpp:71:26:71:30 | *input | provenance | |
106+
| zlibTest.cpp:85:19:85:25 | *access to array | zlibTest.cpp:85:19:85:25 | InflateString output argument | provenance | |
107+
| zlibTest.cpp:85:19:85:25 | InflateString output argument | zlibTest.cpp:80:33:80:36 | **argv | provenance | |
108+
| zlibTest.cpp:85:19:85:25 | InflateString output argument | zlibTest.cpp:80:33:80:36 | **argv [Return] | provenance | |
109+
| zstdTest.cpp:26:39:26:42 | **argv | zstdTest.cpp:27:35:27:41 | *access to array | provenance | |
110+
| zstdTest.cpp:26:39:26:42 | *argv | zstdTest.cpp:27:35:27:41 | *access to array | provenance | |
111+
| zstdTest.cpp:27:23:27:33 | call to fopen_orDie | zstdTest.cpp:27:23:27:33 | call to fopen_orDie | provenance | |
112+
| zstdTest.cpp:27:23:27:33 | call to fopen_orDie | zstdTest.cpp:35:52:35:54 | fin | provenance | |
113+
| zstdTest.cpp:27:35:27:41 | *access to array | zstdTest.cpp:27:23:27:33 | call to fopen_orDie | provenance | Config |
114+
| zstdTest.cpp:35:32:35:37 | **buffIn | zstdTest.cpp:36:32:36:37 | **buffIn | provenance | |
115+
| zstdTest.cpp:35:32:35:37 | *buffIn | zstdTest.cpp:36:32:36:37 | *buffIn | provenance | |
116+
| zstdTest.cpp:35:52:35:54 | fin | zstdTest.cpp:35:32:35:37 | **buffIn | provenance | Config |
117+
| zstdTest.cpp:35:52:35:54 | fin | zstdTest.cpp:35:32:35:37 | *buffIn | provenance | Config |
118+
| zstdTest.cpp:35:52:35:54 | fin | zstdTest.cpp:35:52:35:54 | fin | provenance | |
119+
| zstdTest.cpp:36:32:36:37 | **buffIn | zstdTest.cpp:35:32:35:37 | **buffIn | provenance | |
120+
| zstdTest.cpp:36:32:36:37 | **buffIn | zstdTest.cpp:39:69:39:74 | & ... | provenance | Config |
121+
| zstdTest.cpp:36:32:36:37 | **buffIn | zstdTest.cpp:39:69:39:74 | & ... | provenance | Config |
122+
| zstdTest.cpp:36:32:36:37 | *buffIn | zstdTest.cpp:35:32:35:37 | *buffIn | provenance | |
123+
| zstdTest.cpp:36:32:36:37 | *buffIn | zstdTest.cpp:39:69:39:74 | & ... | provenance | Config |
124+
| zstdTest.cpp:36:32:36:37 | *buffIn | zstdTest.cpp:39:69:39:74 | & ... | provenance | Config |
125+
| zstdTest.cpp:39:69:39:74 | & ... | zstdTest.cpp:39:69:39:74 | & ... | provenance | |
126+
| zstdTest.cpp:39:69:39:74 | & ... | zstdTest.cpp:39:69:39:74 | & ... | provenance | |
85127
nodes
86128
| brotliTest.cpp:15:41:15:44 | **argv | semmle.label | **argv |
87129
| brotliTest.cpp:15:41:15:44 | **argv | semmle.label | **argv |
@@ -105,6 +147,10 @@ nodes
105147
| main.cpp:10:24:10:27 | **argv | semmle.label | **argv |
106148
| main.cpp:10:24:10:27 | minizip_test output argument | semmle.label | minizip_test output argument |
107149
| main.cpp:11:21:11:24 | **argv | semmle.label | **argv |
150+
| main.cpp:11:21:11:24 | zlib_test output argument | semmle.label | zlib_test output argument |
151+
| main.cpp:11:21:11:24 | zlib_test output argument | semmle.label | zlib_test output argument |
152+
| main.cpp:12:21:12:24 | **argv | semmle.label | **argv |
153+
| main.cpp:12:21:12:24 | *argv | semmle.label | *argv |
108154
| minizipTest.cpp:12:42:12:45 | **argv | semmle.label | **argv |
109155
| minizipTest.cpp:12:42:12:45 | **argv | semmle.label | **argv |
110156
| minizipTest.cpp:17:52:17:67 | *access to array | semmle.label | *access to array |
@@ -138,8 +184,11 @@ nodes
138184
| zlibTest.cpp:58:29:58:36 | *fileName | semmle.label | *fileName |
139185
| zlibTest.cpp:62:25:62:31 | inFileZ | semmle.label | inFileZ |
140186
| zlibTest.cpp:71:26:71:30 | *input | semmle.label | *input |
187+
| zlibTest.cpp:71:26:71:30 | *input | semmle.label | *input |
141188
| zlibTest.cpp:77:45:77:59 | *input | semmle.label | *input |
142189
| zlibTest.cpp:80:33:80:36 | **argv | semmle.label | **argv |
190+
| zlibTest.cpp:80:33:80:36 | **argv | semmle.label | **argv |
191+
| zlibTest.cpp:80:33:80:36 | **argv [Return] | semmle.label | **argv [Return] |
143192
| zlibTest.cpp:81:19:81:25 | *access to array | semmle.label | *access to array |
144193
| zlibTest.cpp:81:19:81:25 | UnsafeGzfread output argument | semmle.label | UnsafeGzfread output argument |
145194
| zlibTest.cpp:82:18:82:24 | *access to array | semmle.label | *access to array |
@@ -149,15 +198,32 @@ nodes
149198
| zlibTest.cpp:84:18:84:24 | *access to array | semmle.label | *access to array |
150199
| zlibTest.cpp:84:18:84:24 | UnsafeGzread output argument | semmle.label | UnsafeGzread output argument |
151200
| zlibTest.cpp:85:19:85:25 | *access to array | semmle.label | *access to array |
201+
| zlibTest.cpp:85:19:85:25 | InflateString output argument | semmle.label | InflateString output argument |
202+
| zstdTest.cpp:26:39:26:42 | **argv | semmle.label | **argv |
203+
| zstdTest.cpp:26:39:26:42 | *argv | semmle.label | *argv |
204+
| zstdTest.cpp:27:23:27:33 | call to fopen_orDie | semmle.label | call to fopen_orDie |
205+
| zstdTest.cpp:27:23:27:33 | call to fopen_orDie | semmle.label | call to fopen_orDie |
206+
| zstdTest.cpp:27:35:27:41 | *access to array | semmle.label | *access to array |
207+
| zstdTest.cpp:35:32:35:37 | **buffIn | semmle.label | **buffIn |
208+
| zstdTest.cpp:35:32:35:37 | *buffIn | semmle.label | *buffIn |
209+
| zstdTest.cpp:35:52:35:54 | fin | semmle.label | fin |
210+
| zstdTest.cpp:36:32:36:37 | **buffIn | semmle.label | **buffIn |
211+
| zstdTest.cpp:36:32:36:37 | *buffIn | semmle.label | *buffIn |
212+
| zstdTest.cpp:39:69:39:74 | & ... | semmle.label | & ... |
213+
| zstdTest.cpp:39:69:39:74 | & ... | semmle.label | & ... |
152214
subpaths
153215
| libarchiveTests.cpp:38:27:38:27 | *a | libarchiveTests.cpp:16:31:16:32 | *ar | libarchiveTests.cpp:16:31:16:32 | *ar | libarchiveTests.cpp:38:27:38:27 | read_data output argument |
154216
| main.cpp:8:23:8:26 | **argv | brotliTest.cpp:15:41:15:44 | **argv | brotliTest.cpp:15:41:15:44 | **argv | main.cpp:8:23:8:26 | brotli_test output argument |
155217
| main.cpp:9:27:9:30 | **argv | libarchiveTests.cpp:30:45:30:48 | **argv | libarchiveTests.cpp:30:45:30:48 | **argv | main.cpp:9:27:9:30 | libarchive_test output argument |
156218
| main.cpp:10:24:10:27 | **argv | minizipTest.cpp:12:42:12:45 | **argv | minizipTest.cpp:12:42:12:45 | **argv | main.cpp:10:24:10:27 | minizip_test output argument |
219+
| main.cpp:11:21:11:24 | **argv | zlibTest.cpp:80:33:80:36 | **argv | zlibTest.cpp:80:33:80:36 | **argv | main.cpp:11:21:11:24 | zlib_test output argument |
220+
| main.cpp:11:21:11:24 | **argv | zlibTest.cpp:80:33:80:36 | **argv | zlibTest.cpp:80:33:80:36 | **argv [Return] | main.cpp:11:21:11:24 | zlib_test output argument |
221+
| main.cpp:11:21:11:24 | **argv | zlibTest.cpp:80:33:80:36 | **argv | zlibTest.cpp:80:33:80:36 | **argv [Return] | main.cpp:11:21:11:24 | zlib_test output argument |
157222
| zlibTest.cpp:81:19:81:25 | *access to array | zlibTest.cpp:47:26:47:33 | *fileName | zlibTest.cpp:47:26:47:33 | *fileName | zlibTest.cpp:81:19:81:25 | UnsafeGzfread output argument |
158223
| zlibTest.cpp:82:18:82:24 | *access to array | zlibTest.cpp:57:25:57:32 | *fileName | zlibTest.cpp:57:25:57:32 | *fileName | zlibTest.cpp:82:18:82:24 | UnsafeGzgets output argument |
159224
| zlibTest.cpp:83:19:83:25 | *access to array | zlibTest.cpp:16:26:16:30 | *input | zlibTest.cpp:16:26:16:30 | *input | zlibTest.cpp:83:19:83:25 | UnsafeInflate output argument |
160225
| zlibTest.cpp:84:18:84:24 | *access to array | zlibTest.cpp:37:25:37:32 | *fileName | zlibTest.cpp:37:25:37:32 | *fileName | zlibTest.cpp:84:18:84:24 | UnsafeGzread output argument |
226+
| zlibTest.cpp:85:19:85:25 | *access to array | zlibTest.cpp:71:26:71:30 | *input | zlibTest.cpp:71:26:71:30 | *input | zlibTest.cpp:85:19:85:25 | InflateString output argument |
161227
#select
162228
| brotliTest.cpp:18:35:18:53 | *access to array | main.cpp:7:33:7:36 | **argv | brotliTest.cpp:18:35:18:53 | *access to array | The decompression output of $@ is not limited | brotliTest.cpp:18:5:18:27 | call to BrotliDecoderDecompress | BrotliDecoderDecompress |
163229
| brotliTest.cpp:24:51:24:58 | **& ... | main.cpp:7:33:7:36 | **argv | brotliTest.cpp:24:51:24:58 | **& ... | The decompression output of $@ is not limited | brotliTest.cpp:24:5:24:33 | call to BrotliDecoderDecompressStream | BrotliDecoderDecompressStream |
@@ -171,3 +237,4 @@ subpaths
171237
| zlibTest.cpp:51:38:51:44 | inFileZ | main.cpp:7:33:7:36 | **argv | zlibTest.cpp:51:38:51:44 | inFileZ | The decompression output of $@ is not limited | zlibTest.cpp:51:14:51:20 | call to gzfread | gzfread |
172238
| zlibTest.cpp:62:25:62:31 | inFileZ | main.cpp:7:33:7:36 | **argv | zlibTest.cpp:62:25:62:31 | inFileZ | The decompression output of $@ is not limited | zlibTest.cpp:62:18:62:23 | call to gzgets | gzgets |
173239
| zlibTest.cpp:77:45:77:59 | *input | main.cpp:7:33:7:36 | **argv | zlibTest.cpp:77:45:77:59 | *input | The decompression output of $@ is not limited | zlibTest.cpp:77:5:77:14 | call to uncompress | uncompress |
240+
| zstdTest.cpp:39:69:39:74 | & ... | main.cpp:7:33:7:36 | **argv | zstdTest.cpp:39:69:39:74 | & ... | The decompression output of $@ is not limited | zstdTest.cpp:39:32:39:52 | call to ZSTD_decompressStream | ZSTD_decompressStream |
Lines changed: 10 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
typedef struct _IO_FILE FILE;
2-
3-
FILE *fopen_orDie(const char *filename, const char *instruction);
4-
51
typedef long unsigned int size_t;
2+
struct FILE;
63

7-
const size_t ZSTD_DStreamInSize();
8-
4+
FILE *fopen_orDie(const char *filename, const char *instruction);
5+
size_t fread_orDie(void *const pVoid, const size_t read, FILE *const pFile);
96
void *const malloc_orDie(const size_t size);
107

11-
const size_t ZSTD_DStreamOutSize();
12-
138
struct ZSTD_DCtx;
14-
159
typedef struct ZSTD_inBuffer_s {
1610
const void *src;
1711
size_t size;
@@ -23,67 +17,27 @@ typedef struct ZSTD_outBuffer_s {
2317
size_t pos;
2418
} ZSTD_outBuffer;
2519

20+
const size_t ZSTD_DStreamInSize();
21+
const size_t ZSTD_DStreamOutSize();
2622
ZSTD_DCtx *const ZSTD_createDCtx();
27-
28-
void CHECK(bool b, const char *string);
29-
30-
size_t fread_orDie(void *const pVoid, const size_t read, FILE *const pFile);
31-
32-
void CHECK_ZSTD(const size_t ret);
33-
34-
void fwrite_orDie(void *const pVoid, size_t pos, FILE *const pFile);
35-
36-
void exit(int i);
37-
38-
void fclose_orDie(FILE *const pFile);
39-
40-
void free(void *const pVoid);
41-
4223
const size_t ZSTD_decompressStream(ZSTD_DCtx *const pCtx, ZSTD_outBuffer *pS, ZSTD_inBuffer *pS1);
24+
void CHECK_ZSTD(const size_t ret);
4325

44-
void ZSTD_freeDCtx(ZSTD_DCtx *const pCtx);
45-
46-
static void decompressFile_orDie(const char *fname) {
47-
FILE *const fin = fopen_orDie(fname, "rb");
26+
void zstd_test(int argc, const char **argv) {
27+
FILE *const fin = fopen_orDie(argv[1], "rb");
4828
size_t const buffInSize = ZSTD_DStreamInSize();
4929
void *const buffIn = malloc_orDie(buffInSize);
50-
FILE *stdout;
51-
FILE *const fout = stdout;
52-
size_t const buffOutSize = ZSTD_DStreamOutSize(); /* Guarantee to successfully flush at least one complete compressed block in all circumstances. */
30+
size_t const buffOutSize = ZSTD_DStreamOutSize();
5331
void *const buffOut = malloc_orDie(buffOutSize);
5432

5533
ZSTD_DCtx *const dctx = ZSTD_createDCtx();
56-
CHECK(dctx != nullptr, "ZSTD_createDCtx() failed!");
57-
size_t const toRead = buffInSize;
5834
size_t read;
59-
size_t lastRet = 0;
60-
int isEmpty = 1;
61-
while ((read = fread_orDie(buffIn, toRead, fin))) {
62-
isEmpty = 0;
35+
while ((read = fread_orDie(buffIn, buffInSize, fin))) {
6336
ZSTD_inBuffer input = {buffIn, read, 0};
6437
while (input.pos < input.size) {
6538
ZSTD_outBuffer output = {buffOut, buffOutSize, 0};
6639
size_t const ret = ZSTD_decompressStream(dctx, &output, &input);
6740
CHECK_ZSTD(ret);
68-
fwrite_orDie(buffOut, output.pos, fout);
69-
lastRet = ret;
7041
}
7142
}
72-
if (isEmpty) {
73-
exit(1);
74-
}
75-
if (lastRet != 0) {
76-
exit(1);
77-
}
78-
ZSTD_freeDCtx(dctx);
79-
fclose_orDie(fin);
80-
fclose_orDie(fout);
81-
free(buffIn);
82-
free(buffOut);
83-
}
84-
85-
86-
void zstd_test(int argc, const char **argv) {
87-
const char *const inFilename = argv[1];
88-
decompressFile_orDie(inFilename);
8943
}

0 commit comments

Comments
 (0)