Java runtime.exec() 错误命令 [1] = null

Java runtime.exec() Error tainted command[1] = null

我在 Android shell 中通过 runtime.getRuntime.exec(command,arguments,working directory)b

执行命令时遇到此错误
FATAL EXCEPTION: main
04-07 02:56:18.539 23705 23705 E   AndroidRuntime                               Process: com.learning, PID: 23705
04-07 02:56:18.539 23705 23705 E   AndroidRuntime                               java.lang.NullPointerException: taintedCommand[1] == null
04-07 02:56:18.539 23705 23705 E   AndroidRuntime                               at java.lang.ProcessManager.exec(ProcessManager.java:184)
04-07 02:56:18.539 23705 23705 E   AndroidRuntime                               at java.lang.Runtime.exec(Runtime.java:174)
04-07 02:56:18.539 23705 23705 E   AndroidRuntime                               at com.learning.BackgroundJob.<init>(BackgroundJob.java:38)
04-07 02:56:18.539 23705 23705 E   AndroidRuntime                               at com.learning.MainActivityTerminal0000000.onEditorAction(MainActivityTerminal.java:58)
04-07 02:56:18.539 23705 23705 E   AndroidRuntime                               at android.widget.TextView.onEditorAction(TextView.java:4489)
04-07 02:56:18.539 23705 23705 E   AndroidRuntime                               at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:143)
04-07 02:56:18.539 23705 23705 E   AndroidRuntime                               at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:304)
04-07 02:56:18.539 23705 23705 E   AndroidRuntime                               at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
04-07 02:56:18.539 23705 23705 E   AndroidRuntime                               at android.os.Handler.dispatchMessage(Handler.java:102)
04-07 02:56:18.539 23705 23705 E   AndroidRuntime                               at android.os.Looper.loop(Looper.java:135)
04-07 02:56:18.539 23705 23705 E   AndroidRuntime                               at android.app.ActivityThread.main(ActivityThread.java:5318)
04-07 02:56:18.539 23705 23705 E   AndroidRuntime                               at java.lang.reflect.Method.invoke(Native Method)
04-07 02:56:18.539 23705 23705 E   AndroidRuntime                               at java.lang.reflect.Method.invoke(Method.java:372)
04-07 02:56:18.539 23705 23705 E   AndroidRuntime                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922)
04-07 02:56:18.539 23705 23705 E   AndroidRuntime                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717)
04-07 02:54:04.839 23439 23439 W   System.err                                   at java.lang.reflect.Method.invoke(Method.java:372)
04-07 02:54:04.839 23439 23439 W   System.err                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922)
04-07 02:54:04.839 23439 23439 W   System.err                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717)
04-07 02:54:04.839 23439 23439 W   System.err                                   Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
04-07 02:54:04.839 23439 23439 W   System.err                                   at libcore.io.Posix.open(Native Method)
04-07 02:54:04.839 23439 23439 W   System.err                                   at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
04-07 02:54:04.839 23439 23439 W   System.err                                   at libcore.io.IoBridge.open(IoBridge.java:442)
04-07 02:54:04.839 23439 23439 W   System.err                                   ... 18 more

代码

Process process;
        try {
            process = Runtime.getRuntime().exec(progArray, env, new File(cwd));
        } catch (IOException e) {
            mProcess = null;
            // TODO: Visible error message?
            Log.e(LOG_TAG, "Failed running background job: " + processDescription, e);
            return;
        }

要执行的变量 progarry 命令,env 当前环境,cwd 当前工作目录。

Runtime.exec() 抛出 NullPointerException 如果 cmdarraynull,或者 cmdarray 的元素之一是 null,或者一个envp 的元素是 null.

因此,您最好检查这些条件,例如

if (progArray == null) {
    // do something else or log
    return;
} 
...