From 254d5fc75cf402691a6d69fa889564615c92c352 Mon Sep 17 00:00:00 2001 From: Aaron Segal Date: Mon, 20 Nov 2023 17:32:34 -0500 Subject: [PATCH] Added safer limit clamping function --- .../jinjava/interpret/JinjavaInterpreter.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java index 1fb6a5e94..1bb10fad0 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java +++ b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java @@ -301,10 +301,7 @@ public String render(Node root, boolean processExtendRoots) { * @return rendered result */ private String render(Node root, boolean processExtendRoots, long renderLimit) { - long safeRenderSize = (config.getMaxOutputSize() == 0) - ? renderLimit - : Math.min(renderLimit, config.getMaxOutputSize()); - OutputList output = new OutputList(safeRenderSize); + OutputList output = new OutputList(clampOutputSizeSafely(renderLimit)); for (Node node : root.getChildren()) { lineNumber = node.getLineNumber(); position = node.getStartPosition(); @@ -927,6 +924,20 @@ private String getWrappedErrorMessage( } } + private long clampOutputSizeSafely(long providedLimit) { + long configMaxOutput = config.getMaxOutputSize(); + + if (configMaxOutput == 0) { + return providedLimit; + } + + if (providedLimit <= 0) { + return configMaxOutput; + } + + return Math.min(providedLimit, configMaxOutput); + } + @Override @SuppressWarnings("unchecked") public T appendPyishString(T appendable)