From: Mathias Aparicio <[email protected]>

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.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/adaint.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/gcc/ada/adaint.c b/gcc/ada/adaint.c
index db871d1e102..a63408370f7 100644
--- a/gcc/ada/adaint.c
+++ b/gcc/ada/adaint.c
@@ -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);
-- 
2.53.0

Reply via email to