Skip to content

Commit f3c60a3

Browse files
committedDec 15, 2023
GroovyEngine.execute cause an OOM exception, fixes #909
1 parent 277dd8c commit f3c60a3

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed
 

‎console/src/main/java/org/jline/console/impl/DefaultPrinter.java

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002-2022, the original author(s).
2+
* Copyright (c) 2002-2023, the original author(s).
33
*
44
* This software is distributable under the BSD license. See the terms of the
55
* BSD license in the documentation provided with this software.
@@ -337,14 +337,15 @@ private void internalPrintln(Map<String, Object> options, Object object) {
337337
String style = (String) options.getOrDefault(Printer.STYLE, "");
338338
options.put(Printer.STYLE, valueHighlighter(style));
339339
int width = (int) options.get(Printer.WIDTH);
340+
int maxrows = (int) options.get(Printer.MAXROWS);
340341
if (!style.isEmpty() && object instanceof String) {
341-
highlightAndPrint(width, (SyntaxHighlighter) options.get(Printer.STYLE), (String) object, true);
342+
highlightAndPrint(width, (SyntaxHighlighter) options.get(Printer.STYLE), (String) object, true, maxrows);
342343
} else if (style.equalsIgnoreCase("JSON")) {
343344
if (engine == null) {
344345
throw new IllegalArgumentException("JSON style not supported!");
345346
}
346347
String json = engine.toJson(object);
347-
highlightAndPrint(width, (SyntaxHighlighter) options.get(Printer.STYLE), json, true);
348+
highlightAndPrint(width, (SyntaxHighlighter) options.get(Printer.STYLE), json, true, maxrows);
348349
} else if (options.containsKey(Printer.SKIP_DEFAULT_OPTIONS)) {
349350
highlightAndPrint(options, object);
350351
} else if (object instanceof Exception) {
@@ -354,7 +355,7 @@ private void internalPrintln(Map<String, Object> options, Object object) {
354355
} else if (object instanceof String || object instanceof Number) {
355356
String str = object.toString();
356357
SyntaxHighlighter highlighter = (SyntaxHighlighter) options.getOrDefault(Printer.VALUE_STYLE, null);
357-
highlightAndPrint(width, highlighter, str, doValueHighlight(options, str));
358+
highlightAndPrint(width, highlighter, str, doValueHighlight(options, str), maxrows);
358359
} else {
359360
highlightAndPrint(options, object);
360361
}
@@ -464,14 +465,20 @@ private boolean doValueHighlight(Map<String, Object> options, String value) {
464465
}
465466
}
466467

467-
private void highlightAndPrint(int width, SyntaxHighlighter highlighter, String object, boolean doValueHighlight) {
468-
for (String s : object.split("\\r?\\n")) {
468+
private void highlightAndPrint(
469+
int width, SyntaxHighlighter highlighter, String object, boolean doValueHighlight, int maxrows) {
470+
String[] rows = object.split("\\r?\\n", maxrows);
471+
int lastRowIdx = rows.length == maxrows ? rows.length - 1 : rows.length;
472+
for (int i = 0; i < lastRowIdx; i++) {
469473
AttributedStringBuilder asb = new AttributedStringBuilder();
470-
List<AttributedString> sas = asb.append(s).columnSplitLength(width);
474+
List<AttributedString> sas = asb.append(rows[i]).columnSplitLength(width);
471475
for (AttributedString as : sas) {
472476
highlight(width, highlighter, as.toString(), doValueHighlight).println(terminal());
473477
}
474478
}
479+
if (rows.length == maxrows) {
480+
throw new TruncatedOutputException("Truncated output: " + maxrows);
481+
}
475482
}
476483

477484
private Map<String, Object> keysToString(Map<Object, Object> map) {
@@ -749,6 +756,7 @@ private boolean isNumber(String str) {
749756
@SuppressWarnings("unchecked")
750757
private void highlightAndPrint(Map<String, Object> options, Object obj) {
751758
int width = (int) options.get(Printer.WIDTH);
759+
int maxrows = (int) options.get(Printer.MAXROWS);
752760
totLines = 0;
753761
String message = null;
754762
RuntimeException runtimeException = null;
@@ -758,10 +766,9 @@ private void highlightAndPrint(Map<String, Object> options, Object obj) {
758766
highlightMap(options, keysToString((Map<Object, Object>) obj), width);
759767
} else if (collectionObject(obj)) {
760768
List<Object> collection = objectToList(obj);
761-
if (collection.size() > (int) options.get(Printer.MAXROWS)) {
762-
message = "Truncated output: " + options.get(Printer.MAXROWS) + "/" + collection.size();
763-
collection =
764-
collection.subList(collection.size() - (int) options.get(Printer.MAXROWS), collection.size());
769+
if (collection.size() > maxrows) {
770+
message = "Truncated output: " + maxrows + "/" + collection.size();
771+
collection = collection.subList(collection.size() - maxrows, collection.size());
765772
}
766773
if (!collection.isEmpty()) {
767774
if (collection.size() == 1 && !options.containsKey(Printer.ONE_ROW_TABLE)) {
@@ -771,7 +778,8 @@ private void highlightAndPrint(Map<String, Object> options, Object obj) {
771778
} else if (canConvert(elem) && !options.containsKey(Printer.TO_STRING)) {
772779
highlightMap(options, objectToMap(options, elem), width);
773780
} else if (elem instanceof String && options.get(Printer.STYLE) != null) {
774-
highlightAndPrint(width, (SyntaxHighlighter) options.get(Printer.STYLE), (String) elem, true);
781+
highlightAndPrint(
782+
width, (SyntaxHighlighter) options.get(Printer.STYLE), (String) elem, true, maxrows);
775783
} else {
776784
highlightValue(options, null, objectToString(options, obj))
777785
.println(terminal());

0 commit comments

Comments
 (0)
Please sign in to comment.