]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Fix directories being identified as executables on Windows
authorMathias Aparicio <aparicio@adacore.com>
Thu, 9 Apr 2026 09:55:37 +0000 (11:55 +0200)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Thu, 4 Jun 2026 08:42:16 +0000 (10:42 +0200)
On Windows the `__gnat_is_executable_file_attr` checks if a file
is executable but does not check if it is a regular file or a directory.

This may lead to the execution of a directory which will make the program
crash. This patch fixes the issue by checking if the file is regular
before checking if it is executable.

gcc/ada/ChangeLog:

* adaint.c (__gnat_is_executable_file_attr): Initialize
attr->regular using __gnat_is_regular_file_attr. Directly return
0 before checking if executable if the attr->regular is 0.
Remove the now unnecessary check for invalid_file_attributes.

gcc/ada/adaint.c

index db871d1e102e5165160171ae2fa8c8534002fa2e..a63408370f7b8f15c966305a573868a74a9fee24 100644 (file)
@@ -2176,6 +2176,13 @@ __gnat_is_executable_file_attr (char* name, struct file_attributes* attr)
    if (attr->executable == ATTR_UNSET)
      {
 #if defined (_WIN32)
+       __gnat_is_regular_file_attr (name, attr);
+       if (!attr->regular)
+        {
+          attr->executable = 0;
+          return 0;
+        }
+
        TCHAR wname [GNAT_MAX_PATH_LEN + 2];
        GENERIC_MAPPING GenericMapping;
 
@@ -2198,9 +2205,7 @@ __gnat_is_executable_file_attr (char* name, struct file_attributes* attr)
             while ((l = _tcsstr(last+1, _T(".exe"))))
               last = l;
 
-          attr->executable =
-            GetFileAttributes (wname) != INVALID_FILE_ATTRIBUTES
-            && (last - wname) == (int) (_tcslen (wname) - 4);
+          attr->executable = (last - wname) == (int)(_tcslen (wname) - 4);
         }
 #else
        __gnat_stat_to_attr (-1, name, attr);