Skip to content

Commit

Permalink
secilc: add check for malloc in secilc
Browse files Browse the repository at this point in the history
Check the return value of malloc() to avoid null pointer reference.

Signed-off-by: Huaxin Lu <[email protected]>
Acked-by: James Carter <[email protected]>
  • Loading branch information
Huaxin Lu authored and jwcart2 committed Aug 4, 2023
1 parent 8730e07 commit 04613f6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions secilc/secil2conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ int main(int argc, char *argv[])
file_size = filedata.st_size;

buffer = malloc(file_size);
if (!buffer) {
fprintf(stderr, "Out of memory\n");
rc = SEPOL_ERR;
goto exit;
}

rc = fread(buffer, file_size, 1, file);
if (rc != 1) {
fprintf(stderr, "Failure reading file: %s\n", argv[i]);
Expand Down
6 changes: 6 additions & 0 deletions secilc/secil2tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ int main(int argc, char *argv[])
file_size = filedata.st_size;

buffer = malloc(file_size);
if (!buffer) {
fprintf(stderr, "Out of memory\n");
rc = SEPOL_ERR;
goto exit;
}

rc = fread(buffer, file_size, 1, file);
if (rc != 1) {
fprintf(stderr, "Failure reading file: %s\n", argv[i]);
Expand Down
6 changes: 6 additions & 0 deletions secilc/secilc.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ int main(int argc, char *argv[])
}

buffer = malloc(file_size);
if (!buffer) {
fprintf(stderr, "Out of memory\n");
rc = SEPOL_ERR;
goto exit;
}

rc = fread(buffer, file_size, 1, file);
if (rc != 1) {
fprintf(stderr, "Failure reading file: %s\n", argv[i]);
Expand Down

0 comments on commit 04613f6

Please sign in to comment.