JDK-4794652 : (process) Arg with space and trailing backslash mangled (windows)
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.0_04,5.0
  • Priority: P2
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_2000,windows_xp
  • CPU: x86
  • Submitted: 2002-12-17
  • Updated: 2003-02-20
  • Resolved: 2003-02-14
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other Other Other
1.4.0_04 04Fixed 1.4.1_03Fixed 1.4.2Fixed
Related Reports
Relates :  
Description
Runtime.exec(String[]) does not correctly handle a command argument that
contains a space and ends with a backslash.

-- The following program demonstrates the problem

import java.io.*;
import java.util.*;

public class Cmd {

    private static String getJavaCommand() {
        String javaHome = System.getProperty("java.home");
        if (javaHome != null && javaHome.length() > 0)
            return (javaHome
                    + File.separatorChar + "bin"
                    + File.separatorChar + "java");
        else
            return "java";
    }

    public static void main(String[] args) throws Exception {

        if (args.length > 0) {
            System.err.println("child:  " + args[0]);
            return;
        }

        String[] cmd = new String[3];
        cmd[0] = getJavaCommand();
        cmd[1] = "Cmd";
        cmd[2] = "foo bar\\baz\\";
        System.err.println("parent: " + cmd[2]);

        Process process = Runtime.getRuntime().exec(cmd);
        InputStream in = process.getErrorStream();
        byte[] buf = new byte[1024];
        int n;
        while ((n = in.read(buf)) >= 0)
            System.out.write(buf, 0, n);
 
    }

}

-- Expected output

parent: foo bar\bazchild:  foo bar\baz
-- Actual output

parent: foo bar\bazchild:  foo bar\baz"

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: 1.4.0_04 1.4.1_03 mantis-beta tiger FIXED IN: 1.4.0_04 1.4.1_03 mantis-beta tiger INTEGRATED IN: 1.4.0_04 1.4.1_03 mantis-b17 mantis-beta tiger tiger-b05
14-06-2004

WORK AROUND Do not use spaces, or replace all backslashes with forward slashes.
11-06-2004

SUGGESTED FIX Check each argument to which we add quotes -- if such an argument ends with a backslash, then simply add another one. *** /tmp/geta23643 Fri Jan 10 10:34:17 2003 --- Win32Process.java Fri Jan 10 10:34:17 2003 *************** *** 33,38 **** --- 33,41 ---- if (s.charAt(0) != '"') { cmdbuf.append('"'); cmdbuf.append(s); + if (s.endsWith("\\")) { + cmdbuf.append("\\"); + } cmdbuf.append('"'); } else if (s.endsWith("\"")) { /* The argument has already been quoted. */ -- ###@###.### 2003/1/10
01-11-0176

EVALUATION The root problem is that the Windows command processor does not consistently interpret backslashes within quoted command arguments. If a backslash occurs in the middle a quoted argument then it's left alone, but if it occurs at the end, just before the closing quote character, then it's treated as quoting the quote character itself. -- ###@###.### 2003/1/10
01-11-0176