diff mbox series

[kmod,09/13] libkmod: swap alloca usage for a few assert_cc

Message ID 20240212-decompression-fixes-v1-9-06f92ad07985@gmail.com (mailing list archive)
State New, archived
Headers show
Series Load compressed modules with compression-less kmod | expand

Commit Message

Emil Velikov via B4 Relay Feb. 12, 2024, 5:23 p.m. UTC
From: Emil Velikov <emil.l.velikov@gmail.com>

Since all the compression magic is always available now, we don't need
to loop at runtime nor use alloca - latter of which comes with a handful
of caveats.

Simply throw in a few assert_cc(), which will trigger at build-time.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
---
 libkmod/libkmod-file.c | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)

Comments

Lucas De Marchi April 29, 2024, 11:19 p.m. UTC | #1
On Mon, Feb 12, 2024 at 05:23:10PM GMT, Emil Velikov via B4 Relay wrote:
>From: Emil Velikov <emil.l.velikov@gmail.com>
>
>Since all the compression magic is always available now, we don't need
>to loop at runtime nor use alloca - latter of which comes with a handful
>of caveats.
>
>Simply throw in a few assert_cc(), which will trigger at build-time.

given the small size, we actually never needed it and could have added
the asserts.

But this gets rid of more, with the loop going away, and I'm happy with
it.


Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>

thanks
Lucas De Marchi
Lucas De Marchi April 30, 2024, 5:39 p.m. UTC | #2
On Mon, Feb 12, 2024 at 05:23:10PM GMT, Emil Velikov via B4 Relay wrote:
>From: Emil Velikov <emil.l.velikov@gmail.com>
>
>Since all the compression magic is always available now, we don't need
>to loop at runtime nor use alloca - latter of which comes with a handful
>of caveats.
>
>Simply throw in a few assert_cc(), which will trigger at build-time.
>
>Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
>---
> libkmod/libkmod-file.c | 22 ++++++++--------------
> 1 file changed, 8 insertions(+), 14 deletions(-)
>
>diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c
>index b69f1ef..5b88d6c 100644
>--- a/libkmod/libkmod-file.c
>+++ b/libkmod/libkmod-file.c
>@@ -410,7 +410,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
> {
> 	struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
> 	const struct comp_type *itr;
>-	size_t magic_size_max = 0;
> 	int err = 0;
>
> 	if (file == NULL)
>@@ -422,22 +421,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
> 		goto error;
> 	}
>
>-	for (itr = comp_types; itr->load != NULL; itr++) {
>-		if (magic_size_max < itr->magic_size)
>-			magic_size_max = itr->magic_size;
>-	}
>-
>-	if (magic_size_max > 0) {
>-		char *buf = alloca(magic_size_max + 1);
>+	{
>+		char buf[7];
> 		ssize_t sz;
>
>-		if (buf == NULL) {
>-			err = -errno;
>-			goto error;
>-		}
>-		sz = read_str_safe(file->fd, buf, magic_size_max + 1);
>+		assert_cc(sizeof(magic_zstd) < sizeof(buf));
>+		assert_cc(sizeof(magic_xz) < sizeof(buf));
>+		assert_cc(sizeof(magic_zlib) < sizeof(buf));

../libkmod/libkmod-file.c: In function 'kmod_file_open':
../shared/macro.h:25:9: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
    25 |         _Static_assert((expr), #expr)
       |         ^~~~~~~~~~~~~~
../libkmod/libkmod-file.c:424:9: note: in expansion of macro 'assert_cc'
   424 |         assert_cc(sizeof(magic_zstd) < sizeof(buf));
       |         ^~~~~~~~~


So I'd go with this on top of this patch...  I can squash it as needed
when applying:


|diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c
|index f162a10..8baf12d 100644
|--- a/libkmod/libkmod-file.c
|+++ b/libkmod/libkmod-file.c
|@@ -408,10 +408,15 @@ struct kmod_elf *kmod_file_get_elf(struct kmod_file *file)
| struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
| 						const char *filename)
| {
|-	struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
|+	struct kmod_file *file;
| 	char buf[7];
| 	ssize_t sz;
| 
|+	assert_cc(sizeof(magic_zstd) < sizeof(buf));
|+	assert_cc(sizeof(magic_xz) < sizeof(buf));
|+	assert_cc(sizeof(magic_zlib) < sizeof(buf));
|+
|+	file = calloc(1, sizeof(struct kmod_file));
| 	if (file == NULL)
| 		return NULL;
| 
|@@ -421,10 +426,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
| 		return NULL;
| 	}
| 
|-	assert_cc(sizeof(magic_zstd) < sizeof(buf));
|-	assert_cc(sizeof(magic_xz) < sizeof(buf));
|-	assert_cc(sizeof(magic_zlib) < sizeof(buf));
|-
| 	sz = read_str_safe(file->fd, buf, sizeof(buf));
| 	lseek(file->fd, 0, SEEK_SET);
| 	if (sz != (sizeof(buf) - 1)) {

Lucas De Marchi
Emil Velikov April 30, 2024, 5:54 p.m. UTC | #3
On Tue, 30 Apr 2024 at 18:39, Lucas De Marchi <lucas.demarchi@intel.com> wrote:
>
> On Mon, Feb 12, 2024 at 05:23:10PM GMT, Emil Velikov via B4 Relay wrote:
> >From: Emil Velikov <emil.l.velikov@gmail.com>
> >
> >Since all the compression magic is always available now, we don't need
> >to loop at runtime nor use alloca - latter of which comes with a handful
> >of caveats.
> >
> >Simply throw in a few assert_cc(), which will trigger at build-time.
> >
> >Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
> >---
> > libkmod/libkmod-file.c | 22 ++++++++--------------
> > 1 file changed, 8 insertions(+), 14 deletions(-)
> >
> >diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c
> >index b69f1ef..5b88d6c 100644
> >--- a/libkmod/libkmod-file.c
> >+++ b/libkmod/libkmod-file.c
> >@@ -410,7 +410,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
> > {
> >       struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
> >       const struct comp_type *itr;
> >-      size_t magic_size_max = 0;
> >       int err = 0;
> >
> >       if (file == NULL)
> >@@ -422,22 +421,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
> >               goto error;
> >       }
> >
> >-      for (itr = comp_types; itr->load != NULL; itr++) {
> >-              if (magic_size_max < itr->magic_size)
> >-                      magic_size_max = itr->magic_size;
> >-      }
> >-
> >-      if (magic_size_max > 0) {
> >-              char *buf = alloca(magic_size_max + 1);
> >+      {
> >+              char buf[7];
> >               ssize_t sz;
> >
> >-              if (buf == NULL) {
> >-                      err = -errno;
> >-                      goto error;
> >-              }
> >-              sz = read_str_safe(file->fd, buf, magic_size_max + 1);
> >+              assert_cc(sizeof(magic_zstd) < sizeof(buf));
> >+              assert_cc(sizeof(magic_xz) < sizeof(buf));
> >+              assert_cc(sizeof(magic_zlib) < sizeof(buf));
>
> ../libkmod/libkmod-file.c: In function 'kmod_file_open':
> ../shared/macro.h:25:9: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]

Is there a particular use-case for explicitly forcing C90?

The configure.ac contains `AC_PROG_CC_C99`, which seems reasonable
IMHO. Plus the autogen.sh goes a step further with `-std=gnu11`

Thanks
Emil
Lucas De Marchi April 30, 2024, 6:17 p.m. UTC | #4
On Tue, Apr 30, 2024 at 06:54:00PM GMT, Emil Velikov wrote:
>On Tue, 30 Apr 2024 at 18:39, Lucas De Marchi <lucas.demarchi@intel.com> wrote:
>>
>> On Mon, Feb 12, 2024 at 05:23:10PM GMT, Emil Velikov via B4 Relay wrote:
>> >From: Emil Velikov <emil.l.velikov@gmail.com>
>> >
>> >Since all the compression magic is always available now, we don't need
>> >to loop at runtime nor use alloca - latter of which comes with a handful
>> >of caveats.
>> >
>> >Simply throw in a few assert_cc(), which will trigger at build-time.
>> >
>> >Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
>> >---
>> > libkmod/libkmod-file.c | 22 ++++++++--------------
>> > 1 file changed, 8 insertions(+), 14 deletions(-)
>> >
>> >diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c
>> >index b69f1ef..5b88d6c 100644
>> >--- a/libkmod/libkmod-file.c
>> >+++ b/libkmod/libkmod-file.c
>> >@@ -410,7 +410,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
>> > {
>> >       struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
>> >       const struct comp_type *itr;
>> >-      size_t magic_size_max = 0;
>> >       int err = 0;
>> >
>> >       if (file == NULL)
>> >@@ -422,22 +421,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
>> >               goto error;
>> >       }
>> >
>> >-      for (itr = comp_types; itr->load != NULL; itr++) {
>> >-              if (magic_size_max < itr->magic_size)
>> >-                      magic_size_max = itr->magic_size;
>> >-      }
>> >-
>> >-      if (magic_size_max > 0) {
>> >-              char *buf = alloca(magic_size_max + 1);
>> >+      {
>> >+              char buf[7];
>> >               ssize_t sz;
>> >
>> >-              if (buf == NULL) {
>> >-                      err = -errno;
>> >-                      goto error;
>> >-              }
>> >-              sz = read_str_safe(file->fd, buf, magic_size_max + 1);
>> >+              assert_cc(sizeof(magic_zstd) < sizeof(buf));
>> >+              assert_cc(sizeof(magic_xz) < sizeof(buf));
>> >+              assert_cc(sizeof(magic_zlib) < sizeof(buf));
>>
>> ../libkmod/libkmod-file.c: In function 'kmod_file_open':
>> ../shared/macro.h:25:9: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
>
>Is there a particular use-case for explicitly forcing C90?

not forcing C90, but forcing -Wdeclaration-after-statement as per
flag passed in configure.ac. I think the message given by gcc is
misleading here.

Lucas De Marchi

>
>The configure.ac contains `AC_PROG_CC_C99`, which seems reasonable
>IMHO. Plus the autogen.sh goes a step further with `-std=gnu11`
>
>Thanks
>Emil
Emil Velikov April 30, 2024, 6:27 p.m. UTC | #5
On Tue, 30 Apr 2024 at 19:18, Lucas De Marchi <lucas.demarchi@intel.com> wrote:
>
> On Tue, Apr 30, 2024 at 06:54:00PM GMT, Emil Velikov wrote:
> >On Tue, 30 Apr 2024 at 18:39, Lucas De Marchi <lucas.demarchi@intel.com> wrote:
> >>
> >> On Mon, Feb 12, 2024 at 05:23:10PM GMT, Emil Velikov via B4 Relay wrote:
> >> >From: Emil Velikov <emil.l.velikov@gmail.com>
> >> >
> >> >Since all the compression magic is always available now, we don't need
> >> >to loop at runtime nor use alloca - latter of which comes with a handful
> >> >of caveats.
> >> >
> >> >Simply throw in a few assert_cc(), which will trigger at build-time.
> >> >
> >> >Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
> >> >---
> >> > libkmod/libkmod-file.c | 22 ++++++++--------------
> >> > 1 file changed, 8 insertions(+), 14 deletions(-)
> >> >
> >> >diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c
> >> >index b69f1ef..5b88d6c 100644
> >> >--- a/libkmod/libkmod-file.c
> >> >+++ b/libkmod/libkmod-file.c
> >> >@@ -410,7 +410,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
> >> > {
> >> >       struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
> >> >       const struct comp_type *itr;
> >> >-      size_t magic_size_max = 0;
> >> >       int err = 0;
> >> >
> >> >       if (file == NULL)
> >> >@@ -422,22 +421,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
> >> >               goto error;
> >> >       }
> >> >
> >> >-      for (itr = comp_types; itr->load != NULL; itr++) {
> >> >-              if (magic_size_max < itr->magic_size)
> >> >-                      magic_size_max = itr->magic_size;
> >> >-      }
> >> >-
> >> >-      if (magic_size_max > 0) {
> >> >-              char *buf = alloca(magic_size_max + 1);
> >> >+      {
> >> >+              char buf[7];
> >> >               ssize_t sz;
> >> >
> >> >-              if (buf == NULL) {
> >> >-                      err = -errno;
> >> >-                      goto error;
> >> >-              }
> >> >-              sz = read_str_safe(file->fd, buf, magic_size_max + 1);
> >> >+              assert_cc(sizeof(magic_zstd) < sizeof(buf));
> >> >+              assert_cc(sizeof(magic_xz) < sizeof(buf));
> >> >+              assert_cc(sizeof(magic_zlib) < sizeof(buf));
> >>
> >> ../libkmod/libkmod-file.c: In function 'kmod_file_open':
> >> ../shared/macro.h:25:9: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
> >
> >Is there a particular use-case for explicitly forcing C90?
>
> not forcing C90, but forcing -Wdeclaration-after-statement as per
> flag passed in configure.ac. I think the message given by gcc is
> misleading here.
>

Indeed thanks. Seems like I should add `export CFLAGS+=-Werror" to my dev box.

-Emil
Lucas De Marchi April 30, 2024, 6:43 p.m. UTC | #6
On Tue, Apr 30, 2024 at 07:27:00PM GMT, Emil Velikov wrote:
>On Tue, 30 Apr 2024 at 19:18, Lucas De Marchi <lucas.demarchi@intel.com> wrote:
>>
>> On Tue, Apr 30, 2024 at 06:54:00PM GMT, Emil Velikov wrote:
>> >On Tue, 30 Apr 2024 at 18:39, Lucas De Marchi <lucas.demarchi@intel.com> wrote:
>> >>
>> >> On Mon, Feb 12, 2024 at 05:23:10PM GMT, Emil Velikov via B4 Relay wrote:
>> >> >From: Emil Velikov <emil.l.velikov@gmail.com>
>> >> >
>> >> >Since all the compression magic is always available now, we don't need
>> >> >to loop at runtime nor use alloca - latter of which comes with a handful
>> >> >of caveats.
>> >> >
>> >> >Simply throw in a few assert_cc(), which will trigger at build-time.
>> >> >
>> >> >Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
>> >> >---
>> >> > libkmod/libkmod-file.c | 22 ++++++++--------------
>> >> > 1 file changed, 8 insertions(+), 14 deletions(-)
>> >> >
>> >> >diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c
>> >> >index b69f1ef..5b88d6c 100644
>> >> >--- a/libkmod/libkmod-file.c
>> >> >+++ b/libkmod/libkmod-file.c
>> >> >@@ -410,7 +410,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
>> >> > {
>> >> >       struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
>> >> >       const struct comp_type *itr;
>> >> >-      size_t magic_size_max = 0;
>> >> >       int err = 0;
>> >> >
>> >> >       if (file == NULL)
>> >> >@@ -422,22 +421,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
>> >> >               goto error;
>> >> >       }
>> >> >
>> >> >-      for (itr = comp_types; itr->load != NULL; itr++) {
>> >> >-              if (magic_size_max < itr->magic_size)
>> >> >-                      magic_size_max = itr->magic_size;
>> >> >-      }
>> >> >-
>> >> >-      if (magic_size_max > 0) {
>> >> >-              char *buf = alloca(magic_size_max + 1);
>> >> >+      {
>> >> >+              char buf[7];
>> >> >               ssize_t sz;
>> >> >
>> >> >-              if (buf == NULL) {
>> >> >-                      err = -errno;
>> >> >-                      goto error;
>> >> >-              }
>> >> >-              sz = read_str_safe(file->fd, buf, magic_size_max + 1);
>> >> >+              assert_cc(sizeof(magic_zstd) < sizeof(buf));
>> >> >+              assert_cc(sizeof(magic_xz) < sizeof(buf));
>> >> >+              assert_cc(sizeof(magic_zlib) < sizeof(buf));
>> >>
>> >> ../libkmod/libkmod-file.c: In function 'kmod_file_open':
>> >> ../shared/macro.h:25:9: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
>> >
>> >Is there a particular use-case for explicitly forcing C90?
>>
>> not forcing C90, but forcing -Wdeclaration-after-statement as per
>> flag passed in configure.ac. I think the message given by gcc is
>> misleading here.
>>
>
>Indeed thanks. Seems like I should add `export CFLAGS+=-Werror" to my dev box.

so, are you ok that I just squash this?

Lucas De Marchi

>
>-Emil
Emil Velikov April 30, 2024, 6:47 p.m. UTC | #7
On Tue, 30 Apr 2024 at 19:43, Lucas De Marchi <lucas.demarchi@intel.com> wrote:
>
> On Tue, Apr 30, 2024 at 07:27:00PM GMT, Emil Velikov wrote:
> >On Tue, 30 Apr 2024 at 19:18, Lucas De Marchi <lucas.demarchi@intel.com> wrote:
> >>
> >> On Tue, Apr 30, 2024 at 06:54:00PM GMT, Emil Velikov wrote:
> >> >On Tue, 30 Apr 2024 at 18:39, Lucas De Marchi <lucas.demarchi@intel.com> wrote:
> >> >>
> >> >> On Mon, Feb 12, 2024 at 05:23:10PM GMT, Emil Velikov via B4 Relay wrote:
> >> >> >From: Emil Velikov <emil.l.velikov@gmail.com>
> >> >> >
> >> >> >Since all the compression magic is always available now, we don't need
> >> >> >to loop at runtime nor use alloca - latter of which comes with a handful
> >> >> >of caveats.
> >> >> >
> >> >> >Simply throw in a few assert_cc(), which will trigger at build-time.
> >> >> >
> >> >> >Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
> >> >> >---
> >> >> > libkmod/libkmod-file.c | 22 ++++++++--------------
> >> >> > 1 file changed, 8 insertions(+), 14 deletions(-)
> >> >> >
> >> >> >diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c
> >> >> >index b69f1ef..5b88d6c 100644
> >> >> >--- a/libkmod/libkmod-file.c
> >> >> >+++ b/libkmod/libkmod-file.c
> >> >> >@@ -410,7 +410,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
> >> >> > {
> >> >> >       struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
> >> >> >       const struct comp_type *itr;
> >> >> >-      size_t magic_size_max = 0;
> >> >> >       int err = 0;
> >> >> >
> >> >> >       if (file == NULL)
> >> >> >@@ -422,22 +421,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
> >> >> >               goto error;
> >> >> >       }
> >> >> >
> >> >> >-      for (itr = comp_types; itr->load != NULL; itr++) {
> >> >> >-              if (magic_size_max < itr->magic_size)
> >> >> >-                      magic_size_max = itr->magic_size;
> >> >> >-      }
> >> >> >-
> >> >> >-      if (magic_size_max > 0) {
> >> >> >-              char *buf = alloca(magic_size_max + 1);
> >> >> >+      {
> >> >> >+              char buf[7];
> >> >> >               ssize_t sz;
> >> >> >
> >> >> >-              if (buf == NULL) {
> >> >> >-                      err = -errno;
> >> >> >-                      goto error;
> >> >> >-              }
> >> >> >-              sz = read_str_safe(file->fd, buf, magic_size_max + 1);
> >> >> >+              assert_cc(sizeof(magic_zstd) < sizeof(buf));
> >> >> >+              assert_cc(sizeof(magic_xz) < sizeof(buf));
> >> >> >+              assert_cc(sizeof(magic_zlib) < sizeof(buf));
> >> >>
> >> >> ../libkmod/libkmod-file.c: In function 'kmod_file_open':
> >> >> ../shared/macro.h:25:9: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
> >> >
> >> >Is there a particular use-case for explicitly forcing C90?
> >>
> >> not forcing C90, but forcing -Wdeclaration-after-statement as per
> >> flag passed in configure.ac. I think the message given by gcc is
> >> misleading here.
> >>
> >
> >Indeed thanks. Seems like I should add `export CFLAGS+=-Werror" to my dev box.
>
> so, are you ok that I just squash this?
>

Yes, of course.

-Emil
Lucas De Marchi April 30, 2024, 8:36 p.m. UTC | #8
On Tue, Apr 30, 2024 at 07:47:46PM GMT, Emil Velikov wrote:
>On Tue, 30 Apr 2024 at 19:43, Lucas De Marchi <lucas.demarchi@intel.com> wrote:
>>
>> On Tue, Apr 30, 2024 at 07:27:00PM GMT, Emil Velikov wrote:
>> >On Tue, 30 Apr 2024 at 19:18, Lucas De Marchi <lucas.demarchi@intel.com> wrote:
>> >>
>> >> On Tue, Apr 30, 2024 at 06:54:00PM GMT, Emil Velikov wrote:
>> >> >On Tue, 30 Apr 2024 at 18:39, Lucas De Marchi <lucas.demarchi@intel.com> wrote:
>> >> >>
>> >> >> On Mon, Feb 12, 2024 at 05:23:10PM GMT, Emil Velikov via B4 Relay wrote:
>> >> >> >From: Emil Velikov <emil.l.velikov@gmail.com>
>> >> >> >
>> >> >> >Since all the compression magic is always available now, we don't need
>> >> >> >to loop at runtime nor use alloca - latter of which comes with a handful
>> >> >> >of caveats.
>> >> >> >
>> >> >> >Simply throw in a few assert_cc(), which will trigger at build-time.
>> >> >> >
>> >> >> >Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
>> >> >> >---
>> >> >> > libkmod/libkmod-file.c | 22 ++++++++--------------
>> >> >> > 1 file changed, 8 insertions(+), 14 deletions(-)
>> >> >> >
>> >> >> >diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c
>> >> >> >index b69f1ef..5b88d6c 100644
>> >> >> >--- a/libkmod/libkmod-file.c
>> >> >> >+++ b/libkmod/libkmod-file.c
>> >> >> >@@ -410,7 +410,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
>> >> >> > {
>> >> >> >       struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
>> >> >> >       const struct comp_type *itr;
>> >> >> >-      size_t magic_size_max = 0;
>> >> >> >       int err = 0;
>> >> >> >
>> >> >> >       if (file == NULL)
>> >> >> >@@ -422,22 +421,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
>> >> >> >               goto error;
>> >> >> >       }
>> >> >> >
>> >> >> >-      for (itr = comp_types; itr->load != NULL; itr++) {
>> >> >> >-              if (magic_size_max < itr->magic_size)
>> >> >> >-                      magic_size_max = itr->magic_size;
>> >> >> >-      }
>> >> >> >-
>> >> >> >-      if (magic_size_max > 0) {
>> >> >> >-              char *buf = alloca(magic_size_max + 1);
>> >> >> >+      {
>> >> >> >+              char buf[7];
>> >> >> >               ssize_t sz;
>> >> >> >
>> >> >> >-              if (buf == NULL) {
>> >> >> >-                      err = -errno;
>> >> >> >-                      goto error;
>> >> >> >-              }
>> >> >> >-              sz = read_str_safe(file->fd, buf, magic_size_max + 1);
>> >> >> >+              assert_cc(sizeof(magic_zstd) < sizeof(buf));
>> >> >> >+              assert_cc(sizeof(magic_xz) < sizeof(buf));
>> >> >> >+              assert_cc(sizeof(magic_zlib) < sizeof(buf));
>> >> >>
>> >> >> ../libkmod/libkmod-file.c: In function 'kmod_file_open':
>> >> >> ../shared/macro.h:25:9: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
>> >> >
>> >> >Is there a particular use-case for explicitly forcing C90?
>> >>
>> >> not forcing C90, but forcing -Wdeclaration-after-statement as per
>> >> flag passed in configure.ac. I think the message given by gcc is
>> >> misleading here.
>> >>
>> >
>> >Indeed thanks. Seems like I should add `export CFLAGS+=-Werror" to my dev box.
>>
>> so, are you ok that I just squash this?
>>
>
>Yes, of course.

thanks, pushed everything except the last patch.

Lucas De Marchi

>
>-Emil
diff mbox series

Patch

diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c
index b69f1ef..5b88d6c 100644
--- a/libkmod/libkmod-file.c
+++ b/libkmod/libkmod-file.c
@@ -410,7 +410,6 @@  struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 {
 	struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
 	const struct comp_type *itr;
-	size_t magic_size_max = 0;
 	int err = 0;
 
 	if (file == NULL)
@@ -422,22 +421,17 @@  struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 		goto error;
 	}
 
-	for (itr = comp_types; itr->load != NULL; itr++) {
-		if (magic_size_max < itr->magic_size)
-			magic_size_max = itr->magic_size;
-	}
-
-	if (magic_size_max > 0) {
-		char *buf = alloca(magic_size_max + 1);
+	{
+		char buf[7];
 		ssize_t sz;
 
-		if (buf == NULL) {
-			err = -errno;
-			goto error;
-		}
-		sz = read_str_safe(file->fd, buf, magic_size_max + 1);
+		assert_cc(sizeof(magic_zstd) < sizeof(buf));
+		assert_cc(sizeof(magic_xz) < sizeof(buf));
+		assert_cc(sizeof(magic_zlib) < sizeof(buf));
+
+		sz = read_str_safe(file->fd, buf, sizeof(buf));
 		lseek(file->fd, 0, SEEK_SET);
-		if (sz != (ssize_t)magic_size_max) {
+		if (sz != (sizeof(buf) - 1)) {
 			if (sz < 0)
 				err = sz;
 			else