diff mbox series

secilc: add check for malloc in secilc

Message ID 20230713063504.161485-1-luhuaxin1@huawei.com (mailing list archive)
State Accepted
Commit 04613f6875f6
Delegated to: Petr Lautrbach
Headers show
Series secilc: add check for malloc in secilc | expand

Commit Message

Huaxin Lu July 13, 2023, 6:35 a.m. UTC
Check the return value of malloc() to avoid null pointer reference.

Signed-off-by: Huaxin Lu <luhuaxin1@huawei.com>
---
 secilc/secil2conf.c | 6 ++++++
 secilc/secil2tree.c | 6 ++++++
 secilc/secilc.c     | 6 ++++++
 3 files changed, 18 insertions(+)

Comments

James Carter July 19, 2023, 6:47 p.m. UTC | #1
On Thu, Jul 13, 2023 at 5:12 AM Huaxin Lu <luhuaxin1@huawei.com> wrote:
>
> Check the return value of malloc() to avoid null pointer reference.
>
> Signed-off-by: Huaxin Lu <luhuaxin1@huawei.com>

Acked-by: James Carter <jwcart2@gmail.com>

> ---
>  secilc/secil2conf.c | 6 ++++++
>  secilc/secil2tree.c | 6 ++++++
>  secilc/secilc.c     | 6 ++++++
>  3 files changed, 18 insertions(+)
>
> diff --git a/secilc/secil2conf.c b/secilc/secil2conf.c
> index c49522e..bf050f3 100644
> --- a/secilc/secil2conf.c
> +++ b/secilc/secil2conf.c
> @@ -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]);
> diff --git a/secilc/secil2tree.c b/secilc/secil2tree.c
> index e5cdf6b..d04566d 100644
> --- a/secilc/secil2tree.c
> +++ b/secilc/secil2tree.c
> @@ -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]);
> diff --git a/secilc/secilc.c b/secilc/secilc.c
> index 80d3583..f3102ca 100644
> --- a/secilc/secilc.c
> +++ b/secilc/secilc.c
> @@ -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]);
> --
> 2.33.0
>
James Carter Aug. 4, 2023, 6:38 p.m. UTC | #2
On Wed, Jul 19, 2023 at 2:47 PM James Carter <jwcart2@gmail.com> wrote:
>
> On Thu, Jul 13, 2023 at 5:12 AM Huaxin Lu <luhuaxin1@huawei.com> wrote:
> >
> > Check the return value of malloc() to avoid null pointer reference.
> >
> > Signed-off-by: Huaxin Lu <luhuaxin1@huawei.com>
>
> Acked-by: James Carter <jwcart2@gmail.com>
>

Merged.
Thanks,
Jim

> > ---
> >  secilc/secil2conf.c | 6 ++++++
> >  secilc/secil2tree.c | 6 ++++++
> >  secilc/secilc.c     | 6 ++++++
> >  3 files changed, 18 insertions(+)
> >
> > diff --git a/secilc/secil2conf.c b/secilc/secil2conf.c
> > index c49522e..bf050f3 100644
> > --- a/secilc/secil2conf.c
> > +++ b/secilc/secil2conf.c
> > @@ -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]);
> > diff --git a/secilc/secil2tree.c b/secilc/secil2tree.c
> > index e5cdf6b..d04566d 100644
> > --- a/secilc/secil2tree.c
> > +++ b/secilc/secil2tree.c
> > @@ -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]);
> > diff --git a/secilc/secilc.c b/secilc/secilc.c
> > index 80d3583..f3102ca 100644
> > --- a/secilc/secilc.c
> > +++ b/secilc/secilc.c
> > @@ -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]);
> > --
> > 2.33.0
> >
diff mbox series

Patch

diff --git a/secilc/secil2conf.c b/secilc/secil2conf.c
index c49522e..bf050f3 100644
--- a/secilc/secil2conf.c
+++ b/secilc/secil2conf.c
@@ -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]);
diff --git a/secilc/secil2tree.c b/secilc/secil2tree.c
index e5cdf6b..d04566d 100644
--- a/secilc/secil2tree.c
+++ b/secilc/secil2tree.c
@@ -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]);
diff --git a/secilc/secilc.c b/secilc/secilc.c
index 80d3583..f3102ca 100644
--- a/secilc/secilc.c
+++ b/secilc/secilc.c
@@ -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]);