This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit bc6a4e6fc6ae515c81a4018d8d75dab7354cd57e
Author: Marco Casaroli <[email protected]>
AuthorDate: Mon Oct 12 05:32:52 2026 +0200

    binfmt: Try the script handler after the other formats.
    
    register_binfmt() adds a handler to the head of the list, so the script
    handler, registered last, was tried first.  It opened and read every
    program before the ELF or builtin handler could load it, and when no
    handler could load a file, the error came from the last one tried
    instead of from the script handler (ENODATA instead of ENOENT for a
    missing interpreter).
    
    Register the script handler first, so that it is tried last.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 binfmt/binfmt_initialize.c | 21 +++++++++++++--------
 binfmt/script.c            |  5 +++--
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/binfmt/binfmt_initialize.c b/binfmt/binfmt_initialize.c
index 9151c259708..2556181c5fb 100644
--- a/binfmt/binfmt_initialize.c
+++ b/binfmt/binfmt_initialize.c
@@ -50,6 +50,19 @@ void binfmt_initialize(void)
 {
   int ret;
 
+  /* Each handler goes to the head of the list, so the first one registered
+   * is tried last.  The script handler reads the file of every program
+   * that it is asked to load: try it after the other formats.
+   */
+
+#ifdef CONFIG_BINFMT_SCRIPT
+  ret = script_initialize();
+  if (ret < 0)
+    {
+      berr("ERROR: script_initialize failed: %d\n", ret);
+    }
+#endif
+
 #ifdef CONFIG_BUILTIN
   ret = builtin_initialize();
   if (ret < 0)
@@ -74,14 +87,6 @@ void binfmt_initialize(void)
     }
 #endif
 
-#ifdef CONFIG_BINFMT_SCRIPT
-  ret = script_initialize();
-  if (ret < 0)
-    {
-      berr("ERROR: script_initialize failed: %d\n", ret);
-    }
-#endif
-
   UNUSED(ret);
 }
 
diff --git a/binfmt/script.c b/binfmt/script.c
index 67a985fed15..d729c293705 100644
--- a/binfmt/script.c
+++ b/binfmt/script.c
@@ -290,8 +290,9 @@ static int script_loadbinary(FAR struct binary_s *binp,
  *
  * Description:
  *   Register the script binary format handler.  Called from
- *   binfmt_initialize() AFTER builtin and ELF handlers so those
- *   formats are tried first.
+ *   binfmt_initialize() BEFORE the other handlers: register_binfmt() adds
+ *   to the head of the list, so the builtin and ELF formats are tried
+ *   first.
  *
  ****************************************************************************/
 

Reply via email to