[1/2,pre-commit] Reject symlinks in check-file-mode

Message ID 20260901202742.320498-2-tdevries@suse.de
State New
Headers
Series Two symlink fixes |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Tom de Vries Sept. 1, 2026, 8:27 p.m. UTC
  In a recent commit I introduced two symlinks.

After reading a bit more about it, I realized that this can be problematic on
platforms without proper support for it [1].

Which is probably also the reason why there aren't any symlinks in the repo
other than those two.

Eliminate the symlinks, and extend check-file-mode to reject symlinks.

While we're at it, move the existing check into a function, and improve
comments and error message a bit.

[1] https://gitforwindows.org/symbolic-links.html
---
 .pre-commit-config.yaml        |  4 ++
 gdb/contrib/check-file-mode.sh | 77 +++++++++++++++++++++++-----------
 gdbserver/.shellcheckrc        |  4 +-
 gdbsupport/.shellcheckrc       |  4 +-
 4 files changed, 63 insertions(+), 26 deletions(-)
 mode change 120000 => 100644 gdbserver/.shellcheckrc
 mode change 120000 => 100644 gdbsupport/.shellcheckrc
  

Patch

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 0b4285e6c82..34d2cba40b9 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -151,6 +151,10 @@  repos:
         language: unsupported_script
         entry: gdb/contrib/check-file-mode.sh
         files: *gdb_files
+        # With the default types == [file], because types and types_or are
+        # and-ed, no symlinks will be selected, so we use types == [] instead.
+        types: []
+        types_or: [file, symlink]
       - id: &id5 shellcheck
         name: *id5
         files: '^(gdb|gdbsupport|gdbserver)/'
diff --git a/gdb/contrib/check-file-mode.sh b/gdb/contrib/check-file-mode.sh
index 5a9b0e89fbe..e9cfaf02ce4 100755
--- a/gdb/contrib/check-file-mode.sh
+++ b/gdb/contrib/check-file-mode.sh
@@ -17,30 +17,59 @@ 
 set -e
 set -o pipefail
 
-no_exec_files=()
-for f in "$@"; do
-    case $f in
-	*/*.py \
-	    | */*.sh \
-	    | */configure \
-	    | gdb/gstack-1.in \
-	    | gdb/gcore-1.in \
-	    | gdb/po/gdbtext \
-	    | gdb/make-init-c \
-	    | gdb/testsuite/lib/notty-wrap )
-	    continue
-	    ;;
-	*)
-	    no_exec_files=("${no_exec_files[@]}" "$f")
-	    ;;
-    esac
-done
+# Flag files that are executable, but not meant to be executable.
+check_exec ()
+{
+    no_exec_files=()
+    for f in "$@"; do
+	case $f in
+	    */*.py \
+		| */*.sh )
+		# Shell script or python.
+		continue
+		;;
+	    gdb/po/gdbtext \
+		| gdb/make-init-c \
+		| gdb/testsuite/lib/notty-wrap )
+		# Shell script without .sh extension.
+		continue
+		;;
+	    */configure \
+		| gdb/gstack-1.in \
+		| gdb/gcore-1.in )
+		# Used to generate shell script.
+		continue
+		;;
+	    *)
+		no_exec_files=("${no_exec_files[@]}" "$f")
+		;;
+	esac
+    done
 
-if [ ${#no_exec_files[@]} -eq 0 ]; then
-    exit 0
-fi
+    if [ ${#no_exec_files[@]} -eq 0 ]; then
+	return
+    fi
 
-# Flag files that are executable, but not meant to be executable.
+    if ! git ls-files --stage -- "${no_exec_files[@]}" \
+	    | (! grep '^100755 '); then
+	echo "Found executable mode (100755) on file without .sh or .py"
+	echo "Please fix or add to exception list in $0"
+	exit 1
+    fi
+}
+
+# Flag symlinks.  Symlinks are support by git, but can be problematic on
+# platforms without proper support for it [1].
+# [1] https://gitforwindows.org/symbolic-links.html
+check_symlinks ()
+{
+    if ! git ls-files --stage -- "$@" \
+	    | (! grep '^120000 '); then
+	echo "Found symlink mode (120000)"
+	echo "Please replace by copy"
+	exit 1
+    fi
+}
 
-git ls-files --stage -- "${no_exec_files[@]}" \
-    | (! grep '^100755 ')
+check_exec "$@"
+check_symlinks "$@"
diff --git a/gdbserver/.shellcheckrc b/gdbserver/.shellcheckrc
deleted file mode 120000
index 2a49a004d21..00000000000
--- a/gdbserver/.shellcheckrc
+++ /dev/null
@@ -1 +0,0 @@ 
-../gdb/.shellcheckrc
\ No newline at end of file
diff --git a/gdbserver/.shellcheckrc b/gdbserver/.shellcheckrc
new file mode 100644
index 00000000000..91b66edaf7a
--- /dev/null
+++ b/gdbserver/.shellcheckrc
@@ -0,0 +1,3 @@ 
+# SC2002 was disabled by default in 0.11.0.  Turn it on for
+# compatibility with 0.10.0.
+enable=useless-use-of-cat
diff --git a/gdbsupport/.shellcheckrc b/gdbsupport/.shellcheckrc
deleted file mode 120000
index 2a49a004d21..00000000000
--- a/gdbsupport/.shellcheckrc
+++ /dev/null
@@ -1 +0,0 @@ 
-../gdb/.shellcheckrc
\ No newline at end of file
diff --git a/gdbsupport/.shellcheckrc b/gdbsupport/.shellcheckrc
new file mode 100644
index 00000000000..91b66edaf7a
--- /dev/null
+++ b/gdbsupport/.shellcheckrc
@@ -0,0 +1,3 @@ 
+# SC2002 was disabled by default in 0.11.0.  Turn it on for
+# compatibility with 0.10.0.
+enable=useless-use-of-cat