]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
OSS-Fuzz: Add new fuzzers targets libsmartcols
authorArthur Chan <arthur.chan@adalogics.com>
Wed, 17 Jun 2026 19:24:46 +0000 (20:24 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 24 Jun 2026 09:20:03 +0000 (11:20 +0200)
[kzak@redhat.com: - fix indention]

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
libsmartcols/src/Makemodule.am
libsmartcols/src/fuzz.c [new file with mode: 0644]
tools/oss-fuzz.sh

index a1f30bcde1e7838fbd6f17ad1f206972cb3a6844..3359e28822602588c9c1c00c8de7f3e61682ce16 100644 (file)
@@ -105,6 +105,19 @@ libsmartcols_la_LDFLAGS += -version-info $(LIBSMARTCOLS_VERSION_INFO)
 EXTRA_DIST += \
        libsmartcols/src/libsmartcols.sym
 
+if FUZZING_ENGINE
+check_PROGRAMS += test_smartcols_fuzz
+
+test_smartcols_fuzz_SOURCES = libsmartcols/src/fuzz.c
+
+# https://google.github.io/oss-fuzz/getting-started/new-project-guide/#Requirements
+nodist_EXTRA_test_smartcols_fuzz_SOURCES = dummy.cxx
+
+test_smartcols_fuzz_CFLAGS = $(sample_scols_cflags) -DFUZZ_TARGET
+test_smartcols_fuzz_LDFLAGS = -static
+test_smartcols_fuzz_LDADD = $(sample_scols_ldadd) libcommon.la $(LIB_FUZZING_ENGINE)
+endif
+
 # move lib from $(usrlib_execdir) to $(libdir) if needed
 install-exec-hook-libsmartcols:
        if test "$(usrlib_execdir)" != "$(libdir)" -a -f "$(DESTDIR)$(usrlib_execdir)/libsmartcols.so"; then \
diff --git a/libsmartcols/src/fuzz.c b/libsmartcols/src/fuzz.c
new file mode 100644 (file)
index 0000000..cb50385
--- /dev/null
@@ -0,0 +1,65 @@
+#include "fuzz.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "libsmartcols.h"
+
+static void process_string(const char *str)
+{
+    /* Parse the input as a libsmartcols filter expression. This drives the
+       flex scanner, the bison grammar, parameter/holder construction and the
+       regcomp() done for the =~ operator. */
+    struct libscols_filter *fltr = scols_new_filter(str);
+
+    scols_unref_filter(fltr);
+}
+
+int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
+{
+    char *str = malloc(size + 1);
+    if (!str)
+        return 0;
+
+    memcpy(str, data, size);
+    str[size] = '\0';
+
+    process_string(str);
+
+    free(str);
+    return 0;
+}
+
+#ifndef FUZZ_TARGET
+int main(int argc, char **argv)
+{
+    for (int i = 1; i < argc; i++) {
+        FILE *f = fopen(argv[i], "r");
+        char *buf;
+        long len;
+        size_t n;
+
+        if (!f)
+            continue;
+        fseek(f, 0, SEEK_END);
+        len = ftell(f);
+        fseek(f, 0, SEEK_SET);
+        if (len < 0) {
+            fclose(f);
+            continue;
+        }
+        buf = malloc(len + 1);
+        if (!buf) {
+            fclose(f);
+            continue;
+        }
+        n = fread(buf, 1, len, f);
+        fclose(f);
+        buf[n] = '\0';
+        process_string(buf);
+        free(buf);
+    }
+    return 0;
+}
+#endif
index c94a5f94df3ea91af71c57cc40c4cbea9ae25438..72bdd7c6e7560b32e8807f76a38facaa4d1b572b 100755 (executable)
@@ -24,7 +24,7 @@ if [[ "$SANITIZER" == undefined ]]; then
     CXXFLAGS+=" $UBSAN_FLAGS"
 fi
 
-CONFIGURE_ARGS="--disable-all-programs --enable-libuuid --enable-libfdisk --enable-last --enable-fuzzing-engine --enable-libmount --enable-libblkid"
+CONFIGURE_ARGS="--disable-all-programs --enable-libuuid --enable-libfdisk --enable-last --enable-fuzzing-engine --enable-libmount --enable-libblkid --enable-libsmartcols"
 
 LIBC_VERSION="$(dpkg -s libc6 | grep Version | cut -d' ' -f2)"