Skip to content

Commit 937ce1c

Browse files
committedOct 12, 2023
Fix JVM crash when running java (fixes #216) (#265)
1 parent 7bed2b3 commit 937ce1c

File tree

6 files changed

+57
-15
lines changed

6 files changed

+57
-15
lines changed
 

‎src/main/java/org/fusesource/jansi/AnsiMain.java

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public static void main(String... args) throws IOException {
9595
System.out.println("java.version= " + System.getProperty("java.version") + ", "
9696
+ "java.vendor= " + System.getProperty("java.vendor") + ","
9797
+ " java.home= " + System.getProperty("java.home"));
98+
System.out.println("Console: " + System.console());
9899

99100
System.out.println();
100101

‎src/main/native/jansi_isatty.c

+20-15
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ JNIEXPORT jint JNICALL CLibrary_NATIVE(isatty)
5757

5858
/* check if fd is a pipe */
5959
HANDLE h = (HANDLE) _get_osfhandle(arg0);
60-
DWORD t = GetFileType(h);
61-
if (t == FILE_TYPE_CHAR) {
60+
DWORD t = h != NULL ? GetFileType(h) : 0;
61+
if (h != NULL && t == FILE_TYPE_CHAR) {
6262
// check that this is a real tty because the /dev/null
6363
// and /dev/zero streams are also of type FILE_TYPE_CHAR
6464
rc = GetConsoleMode(h, &mode) != 0;
@@ -84,20 +84,25 @@ JNIEXPORT jint JNICALL CLibrary_NATIVE(isatty)
8484
else {
8585

8686
name = nameinfo->Name.Buffer;
87-
name[nameinfo->Name.Length / 2] = 0;
88-
89-
//fprintf( stderr, "Standard stream %d: pipe name: %S\n", arg0, name);
90-
91-
/*
92-
* Check if this could be a MSYS2 pty pipe ('msys-XXXX-ptyN-XX')
93-
* or a cygwin pty pipe ('cygwin-XXXX-ptyN-XX')
94-
*/
95-
if ((wcsstr(name, L"msys-") || wcsstr(name, L"cygwin-")) && wcsstr(name, L"-pty")) {
96-
rc = 1;
97-
} else {
98-
// This is definitely not a tty
99-
rc = 0;
87+
if (name == NULL) {
88+
rc = 0;
10089
}
90+
else {
91+
name[nameinfo->Name.Length / 2] = 0;
92+
93+
//fprintf( stderr, "Standard stream %d: pipe name: %S\n", arg0, name);
94+
95+
/*
96+
* Check if this could be a MSYS2 pty pipe ('msys-XXXX-ptyN-XX')
97+
* or a cygwin pty pipe ('cygwin-XXXX-ptyN-XX')
98+
*/
99+
if ((wcsstr(name, L"msys-") || wcsstr(name, L"cygwin-")) && wcsstr(name, L"-pty")) {
100+
rc = 1;
101+
} else {
102+
// This is definitely not a tty
103+
rc = 0;
104+
}
105+
}
101106
}
102107
}
103108
}
Binary file not shown.
Binary file not shown.
Binary file not shown.

‎src/test/java/org/fusesource/jansi/AnsiTest.java

+36
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@
1515
*/
1616
package org.fusesource.jansi;
1717

18+
import java.io.ByteArrayOutputStream;
19+
import java.io.InputStream;
20+
import java.nio.file.Path;
21+
import java.nio.file.Paths;
22+
1823
import org.fusesource.jansi.Ansi.Color;
24+
import org.junit.jupiter.api.Disabled;
1925
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.condition.EnabledOnOs;
27+
import org.junit.jupiter.api.condition.OS;
2028
import org.junit.jupiter.params.ParameterizedTest;
2129
import org.junit.jupiter.params.provider.CsvSource;
2230

@@ -181,6 +189,34 @@ public void testColorDisabled() {
181189
}
182190
}
183191

192+
@Test
193+
@EnabledOnOs(OS.WINDOWS)
194+
@Disabled("Does not really fail: launch `javaw -jar jansi-xxx.jar` directly instead")
195+
public void testAnsiMainWithNoConsole() throws Exception {
196+
Path javaHome = Paths.get(System.getProperty("java.home"));
197+
Path java = javaHome.resolve("bin\\javaw.exe");
198+
String cp = System.getProperty("java.class.path");
199+
200+
Process process = new ProcessBuilder()
201+
.command(java.toString(), "-cp", cp, AnsiMain.class.getName())
202+
.start();
203+
204+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
205+
try (InputStream in = process.getInputStream()) {
206+
byte[] buffer = new byte[8192];
207+
while (true) {
208+
int nb = in.read(buffer);
209+
if (nb > 0) {
210+
baos.write(buffer, 0, nb);
211+
} else {
212+
break;
213+
}
214+
}
215+
}
216+
217+
assertTrue(baos.toString().contains("test on System.out"), baos.toString());
218+
}
219+
184220
private static void assertAnsi(String expected, Ansi actual) {
185221
assertEquals(expected.replace("ESC", "\033"), actual.toString());
186222
}

0 commit comments

Comments
 (0)
Please sign in to comment.