Skip to content

Commit 7fce96a

Browse files
committed
Address left over CWE-687 fault and missing include
Check that ftell returns a positive number before reading JSON text in PDALReadPipelineJson. Add missing test_pdalc_utils.h include in test_pdalc_pointviewiterator.c.in.
1 parent 6db7bb3 commit 7fce96a

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

tests/pdal/test_pdalc_pointviewiterator.c.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <pdal/pdalc_pointviewiterator.h>
3737

3838
#include "greatest.h"
39+
#include "test_pdalc_utils.h"
3940

4041
SUITE(test_pdalc_pointviewiterator);
4142

tests/pdal/test_pdalc_utils.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,25 @@ char *PDALReadPipelineJson(const char *path)
4040
if (file)
4141
{
4242
fseek(file, 0, SEEK_END);
43-
size_t length = (size_t) ftell(file);
44-
fseek(file, 0, SEEK_SET);
45-
json = malloc(length + 1);
43+
long length = ftell(file);
4644

47-
if (json && fread(json, 1, length, file) == length)
45+
// Check that length is positive to avoid CWE-687
46+
// See http://cwe.mitre.org/data/definitions/687.html
47+
// See https://scan4.coverity.com/doc/en/cov_checker_ref.html#static_checker_NEGATIVE_RETURNS
48+
if (length > 0)
4849
{
49-
json[length] = '\0';
50-
}
51-
else
52-
{
53-
free(json);
54-
json = NULL;
50+
fseek(file, 0, SEEK_SET);
51+
json = malloc(length + 1);
52+
53+
if (json && fread(json, 1, (size_t) length, file) == length)
54+
{
55+
json[length] = '\0';
56+
}
57+
else
58+
{
59+
free(json);
60+
json = NULL;
61+
}
5562
}
5663

5764
fclose(file);

0 commit comments

Comments
 (0)