Skip to content

Commit 58976a5

Browse files
committed
Use check with FAIL instead of assertions to avoid CWE-404 faults in tests for pdalc_pipeline
Addresses resource leaks identified by Coverity Scan. See: * http://cwe.mitre.org/data/definitions/404.html * https://scan4.coverity.com/doc/en/cov_checker_ref.html#static_checker_RESOURCE_LEAK
1 parent 7feee88 commit 58976a5

File tree

1 file changed

+176
-25
lines changed

1 file changed

+176
-25
lines changed

tests/pdal/test_pdalc_pipeline.c.in

Lines changed: 176 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/******************************************************************************
32
* Copyright (c) 2019, Simverge Software LLC. All rights reserved.
43
*
@@ -61,13 +60,35 @@ static void teardown_test_pdalc_pipeline(void *arg)
6160
TEST testPDALCreateAndDisposePipeline(void)
6261
{
6362
PDALPipelinePtr pipeline = PDALCreatePipeline(NULL);
64-
ASSERT_EQ(NULL, pipeline);
63+
64+
// Use check with FAIL instead of assertions to avoid CWE-404
65+
// See http://cwe.mitre.org/data/definitions/404.html
66+
// See https://scan4.coverity.com/doc/en/cov_checker_ref.html#static_checker_RESOURCE_LEAK
67+
68+
if (pipeline)
69+
{
70+
PDALDisposePipeline(pipeline);
71+
pipeline = NULL;
72+
FAILm("PDALCreatePipeline returned a non-null pipeline when given a NULL argument");
73+
}
6574

6675
pipeline = PDALCreatePipeline("");
67-
ASSERT_EQ(NULL, pipeline);
76+
77+
if (pipeline)
78+
{
79+
PDALDisposePipeline(pipeline);
80+
pipeline = NULL;
81+
FAILm("PDALCreatePipeline returned a non-null pipeline when given an empty string argument");
82+
}
6883

6984
pipeline = PDALCreatePipeline("This is not a valid pipeline, it's not even JSON");
70-
ASSERT_EQ(NULL, pipeline);
85+
86+
if (pipeline)
87+
{
88+
PDALDisposePipeline(pipeline);
89+
pipeline = NULL;
90+
FAILm("PDALCreatePipeline returned a non-null pipeline when given a string argument with an invalid pipeline");
91+
}
7192

7293
pipeline = PDALCreatePipeline(gPipelineJson);
7394
ASSERT(pipeline);
@@ -81,15 +102,33 @@ TEST testPDALCreateAndDisposePipeline(void)
81102
TEST testPDALGetPipelineAsString(void)
82103
{
83104
PDALPipelinePtr pipeline = PDALCreatePipeline(gPipelineJson);
84-
ASSERT_FALSE(pipeline == NULL);
105+
ASSERT(pipeline);
85106

86107
int64_t count = PDALExecutePipeline(pipeline);
87-
ASSERT(count > 0);
108+
109+
if (count <= 0)
110+
{
111+
PDALDisposePipeline(pipeline);
112+
pipeline = NULL;
113+
FAILm("PDALExecutePipeline returned a non-positive point count for a valid pipeline");
114+
}
88115

89116
char json[1024];
90117
size_t size = PDALGetPipelineAsString(pipeline, json, 1024);
91-
ASSERT(size > 0 && size <= 1024);
92-
ASSERT_FALSE(json[0] == '\0');
118+
119+
if (size == 0 || size > 1024)
120+
{
121+
PDALDisposePipeline(pipeline);
122+
pipeline = NULL;
123+
FAILm("PDALGetPipelineAsString returned a string size equal to zero or greater than the provided buffer size");
124+
}
125+
126+
if (json[0] == '\0')
127+
{
128+
PDALDisposePipeline(pipeline);
129+
pipeline = NULL;
130+
FAILm("PDALGetPipelineAsString generated a JSON string whose first character is null");
131+
}
93132

94133
// Make sure that the JSON object's name is "pipeline"
95134
char jsonName[16];
@@ -98,7 +137,13 @@ TEST testPDALGetPipelineAsString(void)
98137
#else
99138
sscanf(json, "%*s\n\t%10s", jsonName);
100139
#endif
101-
ASSERT_STR_EQ("\"pipeline\"", jsonName);
140+
141+
if (strncmp("\"pipeline\"", jsonName, 10) != 0)
142+
{
143+
PDALDisposePipeline(pipeline);
144+
pipeline = NULL;
145+
FAILm("PDALGetPipelineMetadata generated a JSON string whose object name is not \"pipeline\"");
146+
}
102147

103148
PDALDisposePipeline(pipeline);
104149
PASS();
@@ -110,12 +155,34 @@ TEST testPDALGetPipelineMetadata(void)
110155
ASSERT(pipeline);
111156

112157
int64_t count = PDALExecutePipeline(pipeline);
113-
ASSERT(count > 0);
158+
159+
// Use check with FAIL instead of assertions to avoid CWE-404
160+
// See http://cwe.mitre.org/data/definitions/404.html
161+
// See https://scan4.coverity.com/doc/en/cov_checker_ref.html#static_checker_RESOURCE_LEAK
162+
163+
if (count <= 0)
164+
{
165+
PDALDisposePipeline(pipeline);
166+
pipeline = NULL;
167+
FAILm("PDALExecutePipeline returned a non-positive point count for a valid pipeline");
168+
}
114169

115170
char json[1024];
116171
size_t size = PDALGetPipelineMetadata(pipeline, json, 1024);
117-
ASSERT(size > 0 && size <= 1024);
118-
ASSERT_FALSE(json[0] == '\0');
172+
173+
if (size == 0 || size > 1024)
174+
{
175+
PDALDisposePipeline(pipeline);
176+
pipeline = NULL;
177+
FAILm("PDALGetPipelineMetadata returned a string size equal to zero or greater than the provided buffer size");
178+
}
179+
180+
if (json[0] == '\0')
181+
{
182+
PDALDisposePipeline(pipeline);
183+
pipeline = NULL;
184+
FAILm("PDALGetPipelineMetadata generated a JSON string whose first character is null");
185+
}
119186

120187
// Make sure that the JSON object's name is "metadata"
121188
char jsonName[16];
@@ -124,7 +191,13 @@ TEST testPDALGetPipelineMetadata(void)
124191
#else
125192
sscanf(json, "%*s\n\t%10s", jsonName);
126193
#endif
127-
ASSERT_STR_EQ("\"metadata\"", jsonName);
194+
195+
if (strncmp("\"metadata\"", jsonName, 10) != 0)
196+
{
197+
PDALDisposePipeline(pipeline);
198+
pipeline = NULL;
199+
FAILm("PDALGetPipelineMetadata generated a JSON string whose object name is not \"schema\"");
200+
}
128201

129202
PDALDisposePipeline(pipeline);
130203
PASS();
@@ -136,12 +209,34 @@ TEST testPDALGetPipelineSchema(void)
136209
ASSERT(pipeline);
137210

138211
int64_t count = PDALExecutePipeline(pipeline);
139-
ASSERT(count > 0);
212+
213+
// Use check with FAIL instead of assertions to avoid CWE-404
214+
// See http://cwe.mitre.org/data/definitions/404.html
215+
// See https://scan4.coverity.com/doc/en/cov_checker_ref.html#static_checker_RESOURCE_LEAK
216+
217+
if (count <= 0)
218+
{
219+
PDALDisposePipeline(pipeline);
220+
pipeline = NULL;
221+
FAILm("PDALExecutePipeline returned a non-positive point count for a valid pipeline");
222+
}
140223

141224
char json[2048];
142225
size_t size = PDALGetPipelineSchema(pipeline, json, 2048);
143-
ASSERT(size > 0 && size <= 2048);
144-
ASSERT_FALSE(json[0] == '\0');
226+
227+
if (size == 0 || size > 2048)
228+
{
229+
PDALDisposePipeline(pipeline);
230+
pipeline = NULL;
231+
FAILm("PDALGetPipelineSchema returned a string size equal to zero or greater than the provided buffer size");
232+
}
233+
234+
if (json[0] == '\0')
235+
{
236+
PDALDisposePipeline(pipeline);
237+
pipeline = NULL;
238+
FAILm("PDALGetPipelineSchema generated a JSON string whose first character is null");
239+
}
145240

146241
// Make sure that the JSON object's name is "schema"
147242
char jsonName[16];
@@ -150,7 +245,13 @@ TEST testPDALGetPipelineSchema(void)
150245
#else
151246
sscanf(json, "%*s\n\t%10s", jsonName);
152247
#endif
153-
ASSERT_STR_EQ("\"schema\"", jsonName);
248+
249+
if (strncmp("\"schema\"", jsonName, 8) != 0)
250+
{
251+
PDALDisposePipeline(pipeline);
252+
pipeline = NULL;
253+
FAILm("PDALGetPipelineSchema generated a JSON string whose object name is not \"schema\"");
254+
}
154255

155256
PDALDisposePipeline(pipeline);
156257
PASS();
@@ -162,22 +263,49 @@ TEST testPDALGetSetPipelineLog(void)
162263
ASSERT(pipeline);
163264

164265
int64_t count = PDALExecutePipeline(pipeline);
165-
ASSERT(count > 0);
266+
267+
// Use check with FAIL instead of assertions to avoid CWE-404
268+
// See http://cwe.mitre.org/data/definitions/404.html
269+
// See https://scan4.coverity.com/doc/en/cov_checker_ref.html#static_checker_RESOURCE_LEAK
270+
271+
if (count <= 0)
272+
{
273+
PDALDisposePipeline(pipeline);
274+
pipeline = NULL;
275+
FAILm("PDALExecutePipeline returned a non-positive point count for a valid pipeline");
276+
}
166277

167278
// Test valid cases: 0 to 8
168279
char log[1024];
169280

170281
for (int i = 0; i < 9; ++i)
171282
{
172283
PDALSetPipelineLogLevel(pipeline, i);
173-
int j = PDALGetPipelineLogLevel(pipeline);
174-
ASSERT_EQ(i, j);
284+
285+
if (i != PDALGetPipelineLogLevel(pipeline))
286+
{
287+
PDALDisposePipeline(pipeline);
288+
pipeline = NULL;
289+
FAILm("PDALGetPipelineLogLevel returned a number different than the immediately preceding PDALSetPipelineLogLevel call's argument");
290+
}
175291
}
176292

177293
// TODO Determine why all levels yield empty logs
178294
size_t size = PDALGetPipelineLog(pipeline, log, 1024);
179-
//ASSERT(size > 0 && size <= 1024);
180-
//ASSERT_FALSE(log[0] == '\0');
295+
296+
// if (size == 0 || size > 1024)
297+
// {
298+
// PDALDisposePipeline(pipeline);
299+
// pipeline = NULL;
300+
// FAILm("PDALGetPipelineLog returned a string size equal to zero or greater than the provided buffer size");
301+
// }
302+
303+
// if (log[0] == '\0')
304+
// {
305+
// PDALDisposePipeline(pipeline);
306+
// pipeline = NULL;
307+
// FAILm("PDALGetPipelineLog generated a JSON string whose first character is null");
308+
// }
181309

182310
PDALDisposePipeline(pipeline);
183311

@@ -190,10 +318,26 @@ TEST testPDALExecutePipeline(void)
190318
ASSERT(pipeline);
191319

192320
int64_t count = PDALExecutePipeline(pipeline);
193-
ASSERT(count > 0);
321+
322+
// Use check with FAIL instead of assertions to avoid CWE-404
323+
// See http://cwe.mitre.org/data/definitions/404.html
324+
// See https://scan4.coverity.com/doc/en/cov_checker_ref.html#static_checker_RESOURCE_LEAK
325+
326+
if (count <= 0)
327+
{
328+
PDALDisposePipeline(pipeline);
329+
pipeline = NULL;
330+
FAILm("PDALExecutePipeline returned a non-positive point count for a valid pipeline");
331+
}
194332

195333
count = PDALExecutePipeline(NULL);
196-
ASSERT_EQ(0, count);
334+
335+
if (count != 0)
336+
{
337+
PDALDisposePipeline(pipeline);
338+
pipeline = NULL;
339+
FAILm("PDALExecutePipeline returned a positive count for a null pipeline");
340+
}
197341

198342
PDALDisposePipeline(pipeline);
199343
PASS();
@@ -202,11 +346,18 @@ TEST testPDALExecutePipeline(void)
202346
TEST testPDALValidatePipeline(void)
203347
{
204348
bool valid = PDALValidatePipeline(NULL);
205-
ASSERT_FALSEm("Null pipeline evaluated as valid when it should be invalid", valid);
349+
ASSERT_FALSEm("PDALValidatePipeline returned true for a NULL pipeline argument", valid);
206350

207351
PDALPipelinePtr pipeline = PDALCreatePipeline(gPipelineJson);
208352
ASSERT(pipeline);
209353
valid = PDALValidatePipeline(pipeline);
354+
355+
if (!valid)
356+
{
357+
PDALDisposePipeline(pipeline);
358+
pipeline = NULL;
359+
FAILm("PDALValidatePipeline returned false for a valid pipeline");
360+
}
210361
ASSERTm("Valid pipeline evaluated as invalid", valid);
211362

212363
PASS();

0 commit comments

Comments
 (0)