Skip to content

Commit

Permalink
Fix OutOfMemoryError when using TailTipWidget (fix #974) (#975)
Browse files Browse the repository at this point in the history
Co-authored-by: Kay Werndli <kay@spinque.com>
  • Loading branch information
VerKWer and Kay Werndli committed Apr 24, 2024
1 parent 98d3d08 commit 1ab69e3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
66 changes: 66 additions & 0 deletions console/src/test/java/org/jline/widget/TailTipWidgetsTest.java
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2024, the original author(s).
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*
* https://opensource.org/licenses/BSD-3-Clause
*/
package org.jline.widget;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.jline.reader.LineReader;
import org.jline.reader.impl.LineReaderImpl;
import org.jline.terminal.Terminal;
import org.jline.terminal.impl.DumbTerminal;
import org.jline.utils.InfoCmp.Capability;
import org.jline.utils.Status;
import org.jline.widget.TailTipWidgets.TipType;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public final class TailTipWidgetsTest {

/** A simple extension of {@link DumbTerminal} doing the minimal amount of work so that a {@link Status} constructed
* from it is "supported". */
private static final class SupportedDumbTerminal extends DumbTerminal {
private SupportedDumbTerminal() throws IOException {
super(new ByteArrayInputStream(new byte[0]), new ByteArrayOutputStream());
strings.put(Capability.change_scroll_region, "");
strings.put(Capability.save_cursor, "");
strings.put(Capability.restore_cursor, "");
strings.put(Capability.cursor_address, "");
}
}

/** Subclass of {@link Status} exposing the {@code supported} field. For testing only. */
private static final class TestStatus extends Status {
private TestStatus(Terminal terminal) {
super(terminal);
}

private boolean isSupported() {
return supported;
}
}

/** A dummy {@link LineReader} that's immediately resized to 0x0. */
private static final class ZeroSizeLineReader extends LineReaderImpl {
private ZeroSizeLineReader(Terminal terminal) throws IOException {
super(terminal);
display.resize(0, 0);
}
}

@Test
public void enableTest() throws Exception {
Terminal terminal = new SupportedDumbTerminal();
assertTrue(new TestStatus(terminal).isSupported());
LineReader reader = new ZeroSizeLineReader(terminal);
new TailTipWidgets(reader, __ -> null, 1, TipType.COMBINED).enable();
}
}
2 changes: 1 addition & 1 deletion terminal/src/main/java/org/jline/utils/Display.java
Expand Up @@ -68,7 +68,7 @@ public void setDelayLineWrap(boolean v) {

public void resize(int rows, int columns) {
if (rows == 0 || columns == 0) {
columns = Integer.MAX_VALUE - 1;

This comment has been minimized.

Copy link
@wendigo

wendigo May 2, 2024

Why?

This comment has been minimized.

Copy link
@gnodet

gnodet May 2, 2024

Member

See description in #975 .
Did you see any problem with this value ?

This comment has been minimized.

Copy link
@wendigo

wendigo May 2, 2024

yes, this change has caused a regression in Trino CLI. 3.26.0 works fine.

This comment has been minimized.

Copy link
@gnodet

gnodet May 2, 2024

Member

Could you raise an issue describing the problem and provide a PR to revert that line change ?

This comment has been minimized.

Copy link
@wendigo

wendigo May 2, 2024

It seems that in our case redundant 0x08 is echoed (backspace?) when the CLI is invoked as a headless java Process that we read stdout/stderr from.

columns = 1;
rows = 1;
}
if (this.rows != rows || this.columns != columns) {
Expand Down
3 changes: 2 additions & 1 deletion terminal/src/main/java/org/jline/utils/Status.java
Expand Up @@ -117,8 +117,9 @@ public void update(List<AttributedString> lines, boolean flush) {

lines = new ArrayList<>(lines);
// add border
int rows = display.rows;
int columns = display.columns;
if (border == 1 && !lines.isEmpty()) {
if (border == 1 && !lines.isEmpty() && rows > 1) {
lines.add(0, getBorderString(columns));
}
// trim or complete lines to the full width
Expand Down

0 comments on commit 1ab69e3

Please sign in to comment.