]> git.ipfire.org Git - pakfire.git/commitdiff
xfopen: Add more tests for invalid inputs
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 10 Feb 2025 17:51:42 +0000 (17:51 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 10 Feb 2025 17:51:42 +0000 (17:51 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/xfopen.c
tests/libpakfire/xfopen.c

index c32d19c094d12bc835d3db3b396ebc97606b0630..108d44391469239ad5bdc1693601141d052be624 100644 (file)
@@ -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;
        }
 
index 5f7a417e29d63279715b700d5ab92e34e3ebd503..2ca8c929c3e6284815e5e288713822d4c555b1e8 100644 (file)
@@ -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[]) {