From: Michael Tremer Date: Mon, 10 Feb 2025 17:51:42 +0000 (+0000) Subject: xfopen: Add more tests for invalid inputs X-Git-Tag: 0.9.30~43 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7544bc2565b95d6420fc02729da23f18446d2d97;p=pakfire.git xfopen: Add more tests for invalid inputs Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/xfopen.c b/src/pakfire/xfopen.c index c32d19c0..108d4439 100644 --- a/src/pakfire/xfopen.c +++ b/src/pakfire/xfopen.c @@ -65,11 +65,13 @@ const struct compressor { FILE* pakfire_xfopen(FILE* f, const char* mode) { char buffer[MAX_MAGIC_LENGTH]; + // Check file handle if (!f) { - errno = EBADFD; + errno = EBADF; return NULL; } + // Check mode if (!mode) { errno = EINVAL; return NULL; @@ -181,6 +183,12 @@ FILE* pakfire_gzfopen(FILE* f, const char* mode) { return NULL; } + // Check mode + if (!mode) { + errno = EINVAL; + return NULL; + } + // Fetch the file descriptor fd = fileno(f); if (fd < 0) { @@ -362,11 +370,13 @@ FILE* pakfire_xzfopen(FILE* f, const char* mode) { lzma_ret ret; int r; + // Check file handle if (!f) { - errno = EBADFD; + errno = EBADF; return NULL; } + // Check mode if (!mode) { errno = EINVAL; return NULL; @@ -586,7 +596,7 @@ FILE* pakfire_zstdfopen(FILE* f, const char* mode) { // Check for a valid file handle if (!f) { - errno = EBADFD; + errno = EBADF; return NULL; } diff --git a/tests/libpakfire/xfopen.c b/tests/libpakfire/xfopen.c index 5f7a417e..2ca8c929 100644 --- a/tests/libpakfire/xfopen.c +++ b/tests/libpakfire/xfopen.c @@ -44,6 +44,10 @@ static int read_test(const struct test* t, // Open file ASSERT(f = fopen(path, "r")); + // Invalid inputs + ASSERT_ERRNO(function(NULL, "r") == NULL, EBADF); + ASSERT_ERRNO(function(f, NULL) == NULL, EINVAL); + // Engage decompressor ASSERT(f = function(f, "r")); @@ -117,14 +121,26 @@ static int test_zstdfopen_write(const struct test* t) { } static int test_xfopen(const struct test* t) { + FILE* f = NULL; + int r = EXIT_FAILURE; + ASSERT_SUCCESS(read_test(t, pakfire_xfopen, "data/compress/data.gz")); ASSERT_SUCCESS(read_test(t, pakfire_xfopen, "data/compress/data.xz")); ASSERT_SUCCESS(read_test(t, pakfire_xfopen, "data/compress/data.zst")); - return EXIT_SUCCESS; + // Open something random + f = fopen("/dev/urandom", "r"); + + ASSERT_ERRNO(pakfire_xfopen(f, "w") == NULL, ENOTSUP); + + // Success + r = EXIT_SUCCESS; FAIL: - return EXIT_FAILURE; + if (f) + fclose(f); + + return r; } int main(int argc, const char* argv[]) {