Skip to content

Commit

Permalink
GroovyEngine.execute cause an OOM exception, fixes #909
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Dec 15, 2023
1 parent 277dd8c commit f3c60a3
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions console/src/main/java/org/jline/console/impl/DefaultPrinter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2022, the original author(s).
* Copyright (c) 2002-2023, 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.
Expand Down Expand Up @@ -337,14 +337,15 @@ private void internalPrintln(Map<String, Object> options, Object object) {
String style = (String) options.getOrDefault(Printer.STYLE, "");
options.put(Printer.STYLE, valueHighlighter(style));
int width = (int) options.get(Printer.WIDTH);
int maxrows = (int) options.get(Printer.MAXROWS);
if (!style.isEmpty() && object instanceof String) {
highlightAndPrint(width, (SyntaxHighlighter) options.get(Printer.STYLE), (String) object, true);
highlightAndPrint(width, (SyntaxHighlighter) options.get(Printer.STYLE), (String) object, true, maxrows);
} else if (style.equalsIgnoreCase("JSON")) {
if (engine == null) {
throw new IllegalArgumentException("JSON style not supported!");
}
String json = engine.toJson(object);
highlightAndPrint(width, (SyntaxHighlighter) options.get(Printer.STYLE), json, true);
highlightAndPrint(width, (SyntaxHighlighter) options.get(Printer.STYLE), json, true, maxrows);
} else if (options.containsKey(Printer.SKIP_DEFAULT_OPTIONS)) {
highlightAndPrint(options, object);
} else if (object instanceof Exception) {
Expand All @@ -354,7 +355,7 @@ private void internalPrintln(Map<String, Object> options, Object object) {
} else if (object instanceof String || object instanceof Number) {
String str = object.toString();
SyntaxHighlighter highlighter = (SyntaxHighlighter) options.getOrDefault(Printer.VALUE_STYLE, null);
highlightAndPrint(width, highlighter, str, doValueHighlight(options, str));
highlightAndPrint(width, highlighter, str, doValueHighlight(options, str), maxrows);
} else {
highlightAndPrint(options, object);
}
Expand Down Expand Up @@ -464,14 +465,20 @@ private boolean doValueHighlight(Map<String, Object> options, String value) {
}
}

private void highlightAndPrint(int width, SyntaxHighlighter highlighter, String object, boolean doValueHighlight) {
for (String s : object.split("\\r?\\n")) {
private void highlightAndPrint(
int width, SyntaxHighlighter highlighter, String object, boolean doValueHighlight, int maxrows) {
String[] rows = object.split("\\r?\\n", maxrows);
int lastRowIdx = rows.length == maxrows ? rows.length - 1 : rows.length;
for (int i = 0; i < lastRowIdx; i++) {
AttributedStringBuilder asb = new AttributedStringBuilder();
List<AttributedString> sas = asb.append(s).columnSplitLength(width);
List<AttributedString> sas = asb.append(rows[i]).columnSplitLength(width);
for (AttributedString as : sas) {
highlight(width, highlighter, as.toString(), doValueHighlight).println(terminal());
}
}
if (rows.length == maxrows) {
throw new TruncatedOutputException("Truncated output: " + maxrows);
}
}

private Map<String, Object> keysToString(Map<Object, Object> map) {
Expand Down Expand Up @@ -749,6 +756,7 @@ private boolean isNumber(String str) {
@SuppressWarnings("unchecked")
private void highlightAndPrint(Map<String, Object> options, Object obj) {
int width = (int) options.get(Printer.WIDTH);
int maxrows = (int) options.get(Printer.MAXROWS);
totLines = 0;
String message = null;
RuntimeException runtimeException = null;
Expand All @@ -758,10 +766,9 @@ private void highlightAndPrint(Map<String, Object> options, Object obj) {
highlightMap(options, keysToString((Map<Object, Object>) obj), width);
} else if (collectionObject(obj)) {
List<Object> collection = objectToList(obj);
if (collection.size() > (int) options.get(Printer.MAXROWS)) {
message = "Truncated output: " + options.get(Printer.MAXROWS) + "/" + collection.size();
collection =
collection.subList(collection.size() - (int) options.get(Printer.MAXROWS), collection.size());
if (collection.size() > maxrows) {
message = "Truncated output: " + maxrows + "/" + collection.size();
collection = collection.subList(collection.size() - maxrows, collection.size());
}
if (!collection.isEmpty()) {
if (collection.size() == 1 && !options.containsKey(Printer.ONE_ROW_TABLE)) {
Expand All @@ -771,7 +778,8 @@ private void highlightAndPrint(Map<String, Object> options, Object obj) {
} else if (canConvert(elem) && !options.containsKey(Printer.TO_STRING)) {
highlightMap(options, objectToMap(options, elem), width);
} else if (elem instanceof String && options.get(Printer.STYLE) != null) {
highlightAndPrint(width, (SyntaxHighlighter) options.get(Printer.STYLE), (String) elem, true);
highlightAndPrint(
width, (SyntaxHighlighter) options.get(Printer.STYLE), (String) elem, true, maxrows);
} else {
highlightValue(options, null, objectToString(options, obj))
.println(terminal());
Expand Down

0 comments on commit f3c60a3

Please sign in to comment.