From: Emil Velikov Date: Mon, 12 Feb 2024 17:23:11 +0000 Subject: [PATCH kmod 10/13] libkmod: tidy-up kmod_file_open() MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20240212-decompression-fixes-v1-10-06f92ad07985@gmail.com> References: <20240212-decompression-fixes-v1-0-06f92ad07985@gmail.com> In-Reply-To: <20240212-decompression-fixes-v1-0-06f92ad07985@gmail.com> To: linux-modules@vger.kernel.org Cc: Emil Velikov X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1707758583; l=2378; i=emil.l.velikov@gmail.com; s=20230301; h=from:subject:message-id; bh=UfzSecSP8vIYsHmd21SFJIic/SxgSObW4SMoY5ZZ5mI=; b=Gh/abScOoWeSs/fl6sH1LlQ+em0F5Pa5ZcmJMSceZ/nUmxJtEHe2B0HizqB1/Uh2wukLUt2Lh x7AwKrpVhUKBFQ/7P/bBNO25hPnZDM58kXESBA3oGaNMEHo3UCyWLkq X-Developer-Key: i=emil.l.velikov@gmail.com; a=ed25519; pk=qeUTVTNyI3rcR2CfNNWsloTihgzmtbZo98GdxwZKCkY= X-Endpoint-Received: by B4 Relay for emil.l.velikov@gmail.com/20230301 with auth_id=35 List-Id: B4 Relay Submissions This commit cleans up the indentation and the error path of the function. It bears no functional changes. Signed-off-by: Emil Velikov --- libkmod/libkmod-file.c | 60 +++++++++++++++++++++----------------------------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c index 5b88d6c..c4893fd 100644 --- a/libkmod/libkmod-file.c +++ b/libkmod/libkmod-file.c @@ -410,41 +410,40 @@ 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; - int err = 0; + char buf[7]; + ssize_t sz; if (file == NULL) return NULL; file->fd = open(filename, O_RDONLY|O_CLOEXEC); if (file->fd < 0) { - err = -errno; - goto error; + free(file); + return NULL; } - { - 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)); - - sz = read_str_safe(file->fd, buf, sizeof(buf)); - lseek(file->fd, 0, SEEK_SET); - if (sz != (sizeof(buf) - 1)) { - if (sz < 0) - err = sz; - else - err = -EINVAL; - goto error; - } + assert_cc(sizeof(magic_zstd) < sizeof(buf)); + assert_cc(sizeof(magic_xz) < sizeof(buf)); + assert_cc(sizeof(magic_zlib) < sizeof(buf)); - for (itr = comp_types; itr->load != NULL; itr++) { - if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) { - file->load = itr->load; - file->compression = itr->compression; - break; - } + sz = read_str_safe(file->fd, buf, sizeof(buf)); + lseek(file->fd, 0, SEEK_SET); + if (sz != (sizeof(buf) - 1)) { + if (sz < 0) + errno = -sz; + else + errno = EINVAL; + + close(file->fd); + free(file); + return NULL; + } + + for (itr = comp_types; itr->load != NULL; itr++) { + if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) { + file->load = itr->load; + file->compression = itr->compression; + break; } } @@ -455,15 +454,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, file->ctx = ctx; -error: - if (err < 0) { - if (file->fd >= 0) - close(file->fd); - free(file); - errno = -err; - return NULL; - } - return file; } -- 2.43.0