Skip to content

Commit

Permalink
Fix JVM crash when running java (fixes #216) (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet authored Oct 4, 2023
1 parent 41c2ac6 commit 1e4edb5
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/fusesource/jansi/AnsiMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public static void main(String... args) throws IOException {
System.out.println("java.version= " + System.getProperty("java.version") + ", "
+ "java.vendor= " + System.getProperty("java.vendor") + ","
+ " java.home= " + System.getProperty("java.home"));
System.out.println("Console: " + System.console());

System.out.println();

Expand Down
35 changes: 20 additions & 15 deletions src/main/native/jansi_isatty.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ JNIEXPORT jint JNICALL CLibrary_NATIVE(isatty)

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

name = nameinfo->Name.Buffer;
name[nameinfo->Name.Length / 2] = 0;

//fprintf( stderr, "Standard stream %d: pipe name: %S\n", arg0, name);

/*
* Check if this could be a MSYS2 pty pipe ('msys-XXXX-ptyN-XX')
* or a cygwin pty pipe ('cygwin-XXXX-ptyN-XX')
*/
if ((wcsstr(name, L"msys-") || wcsstr(name, L"cygwin-")) && wcsstr(name, L"-pty")) {
rc = 1;
} else {
// This is definitely not a tty
rc = 0;
if (name == NULL) {
rc = 0;
}
else {
name[nameinfo->Name.Length / 2] = 0;

//fprintf( stderr, "Standard stream %d: pipe name: %S\n", arg0, name);

/*
* Check if this could be a MSYS2 pty pipe ('msys-XXXX-ptyN-XX')
* or a cygwin pty pipe ('cygwin-XXXX-ptyN-XX')
*/
if ((wcsstr(name, L"msys-") || wcsstr(name, L"cygwin-")) && wcsstr(name, L"-pty")) {
rc = 1;
} else {
// This is definitely not a tty
rc = 0;
}
}
}
}
}
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
36 changes: 36 additions & 0 deletions src/test/java/org/fusesource/jansi/AnsiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@
*/
package org.fusesource.jansi;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.fusesource.jansi.Ansi.Color;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

Expand Down Expand Up @@ -165,6 +173,34 @@ public void testColorDisabled() {
}
}

@Test
@EnabledOnOs(OS.WINDOWS)
@Disabled("Does not really fail: launch `javaw -jar jansi-xxx.jar` directly instead")
public void testAnsiMainWithNoConsole() throws Exception {
Path javaHome = Paths.get(System.getProperty("java.home"));
Path java = javaHome.resolve("bin\\javaw.exe");
String cp = System.getProperty("java.class.path");

Process process = new ProcessBuilder()
.command(java.toString(), "-cp", cp, AnsiMain.class.getName())
.start();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (InputStream in = process.getInputStream()) {
byte[] buffer = new byte[8192];
while (true) {
int nb = in.read(buffer);
if (nb > 0) {
baos.write(buffer, 0, nb);
} else {
break;
}
}
}

assertTrue(baos.toString().contains("test on System.out"), baos.toString());
}

private static void assertAnsi(String expected, Ansi actual) {
assertEquals(expected.replace("ESC", "\033"), actual.toString());
}
Expand Down

0 comments on commit 1e4edb5

Please sign in to comment.