Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OutOfMemoryError when using TailTipWidget (fixes #974) #975

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 66 additions & 0 deletions console/src/test/java/org/jline/widget/TailTipWidgetsTest.java
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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;
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
Original file line number Diff line number Diff line change
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