diff mbox series

secilc.c: Don't fail if input file is empty

Message ID 20210414141027.3494107-1-yochiang@google.com (mailing list archive)
State Accepted
Headers show
Series secilc.c: Don't fail if input file is empty | expand

Commit Message

Yi-Yo Chiang April 14, 2021, 2:10 p.m. UTC
fread(3) returns zero if |size| is zero. This confuses secilc, and
causes it to fail with a "Failure reading file" error, even though there
is no error.

Add a shortcut that closes and skips an input file if file size is zero.

Signed-off-by: Yi-Yo Chiang <yochiang@google.com>
---
 secilc/secilc.c | 6 ++++++
 1 file changed, 6 insertions(+)

Comments

James Carter April 14, 2021, 7:18 p.m. UTC | #1
On Wed, Apr 14, 2021 at 12:32 PM Yi-Yo Chiang <yochiang@google.com> wrote:
>
> fread(3) returns zero if |size| is zero. This confuses secilc, and
> causes it to fail with a "Failure reading file" error, even though there
> is no error.
>
> Add a shortcut that closes and skips an input file if file size is zero.
>
> Signed-off-by: Yi-Yo Chiang <yochiang@google.com>

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

> ---
>  secilc/secilc.c | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/secilc/secilc.c b/secilc/secilc.c
> index 186c5a73..9c78e425 100644
> --- a/secilc/secilc.c
> +++ b/secilc/secilc.c
> @@ -268,6 +268,12 @@ int main(int argc, char *argv[])
>                 }
>                 file_size = filedata.st_size;
>
> +               if (!file_size) {
> +                       fclose(file);
> +                       file = NULL;
> +                       continue;
> +               }
> +
>                 buffer = malloc(file_size);
>                 rc = fread(buffer, file_size, 1, file);
>                 if (rc != 1) {
> --
> 2.31.1.295.g9ea45b61b8-goog
>
James Carter April 19, 2021, 2:33 p.m. UTC | #2
On Wed, Apr 14, 2021 at 3:18 PM James Carter <jwcart2@gmail.com> wrote:
>
> On Wed, Apr 14, 2021 at 12:32 PM Yi-Yo Chiang <yochiang@google.com> wrote:
> >
> > fread(3) returns zero if |size| is zero. This confuses secilc, and
> > causes it to fail with a "Failure reading file" error, even though there
> > is no error.
> >
> > Add a shortcut that closes and skips an input file if file size is zero.
> >
> > Signed-off-by: Yi-Yo Chiang <yochiang@google.com>
>
> Acked-by: James Carter <jwcart2@gmail.com>
>

Applied.
Thanks,
Jim

> > ---
> >  secilc/secilc.c | 6 ++++++
> >  1 file changed, 6 insertions(+)
> >
> > diff --git a/secilc/secilc.c b/secilc/secilc.c
> > index 186c5a73..9c78e425 100644
> > --- a/secilc/secilc.c
> > +++ b/secilc/secilc.c
> > @@ -268,6 +268,12 @@ int main(int argc, char *argv[])
> >                 }
> >                 file_size = filedata.st_size;
> >
> > +               if (!file_size) {
> > +                       fclose(file);
> > +                       file = NULL;
> > +                       continue;
> > +               }
> > +
> >                 buffer = malloc(file_size);
> >                 rc = fread(buffer, file_size, 1, file);
> >                 if (rc != 1) {
> > --
> > 2.31.1.295.g9ea45b61b8-goog
> >
diff mbox series

Patch

diff --git a/secilc/secilc.c b/secilc/secilc.c
index 186c5a73..9c78e425 100644
--- a/secilc/secilc.c
+++ b/secilc/secilc.c
@@ -268,6 +268,12 @@  int main(int argc, char *argv[])
 		}
 		file_size = filedata.st_size;
 
+		if (!file_size) {
+			fclose(file);
+			file = NULL;
+			continue;
+		}
+
 		buffer = malloc(file_size);
 		rc = fread(buffer, file_size, 1, file);
 		if (rc != 1) {