--- /dev/null
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <linux/loop.h>
+
+void usage(const char *progname)
+{
+ fprintf(stderr, "usage: %s PATH [64]\n", progname);
+ exit(EXIT_FAILURE);
+}
+
+int main(int argc, char **argv)
+{
+ unsigned long cmd;
+ int ret;
+ int fd;
+
+ if (argc != 2 && argc != 3)
+ usage(argv[0]);
+
+ if (argc == 3) {
+ if (strcmp(argv[2], "64") == 0)
+ cmd = LOOP_GET_STATUS64;
+ else
+ usage(argv[0]);
+ } else {
+ cmd = LOOP_GET_STATUS;
+ }
+
+ fd = open(argv[1], O_RDONLY);
+ if (fd == -1) {
+ perror("open");
+ return EXIT_FAILURE;
+ }
+
+ ret = ioctl(fd, cmd, NULL);
+ if (ret == -1) {
+ if (errno == EINVAL) {
+ printf("Got EINVAL\n");
+ } else {
+ perror("ioctl");
+ close(fd);
+ return EXIT_FAILURE;
+ }
+ } else {
+ printf("Did not get EINVAL\n");
+ }
+
+ close(fd);
+ return EXIT_SUCCESS;
+}
--- /dev/null
+#!/bin/bash
+#
+# Test the LOOP_GET_STATUS{,64} error case where the passed argument is NULL.
+# Regression test for patch "loop: fix LOOP_GET_STATUS lock imbalance".
+#
+# Copyright (C) 2018 Omar Sandoval
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+DESCRIPTION="call LOOP_GET_STATUS{,64} with a NULL arg"
+
+QUICK=1
+
+requires() {
+ _have_src_program loop_get_status_null
+}
+
+test() {
+ local loop_dev
+
+ echo "Running ${TEST_NAME}"
+
+ truncate -s 1M "$TMPDIR/file"
+ if ! loop_dev="$(losetup -f --show "$TMPDIR/file")"; then
+ return 1
+ fi
+
+ "$SRCDIR/loop_get_status_null" "$loop_dev"
+ "$SRCDIR/loop_get_status_null" "$loop_dev" 64
+
+ losetup -d "$loop_dev"
+
+ echo "Test complete"
+}