From f32405ed16b7e07a0d445f1ed19032acaf33246d Mon Sep 17 00:00:00 2001 From: Pierre Villard Date: Thu, 24 Sep 2020 12:34:30 +0200 Subject: [PATCH] NIFI-7844 - substring should return empty instead of throwing IndexOutOfBoundsException This closes #4553 Signed-off-by: Mike Thomsen --- .../evaluation/functions/SubstringEvaluator.java | 16 ++++++++++------ .../attribute/expression/language/TestQuery.java | 11 +++++++++++ .../main/asciidoc/expression-language-guide.adoc | 2 +- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/SubstringEvaluator.java b/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/SubstringEvaluator.java index 23990fb9e4..9cec7d3b1b 100644 --- a/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/SubstringEvaluator.java +++ b/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/SubstringEvaluator.java @@ -46,12 +46,16 @@ public class SubstringEvaluator extends StringEvaluator { if (subjectValue == null) { return new StringQueryResult(""); } - final int startIndexValue = startIndex.evaluate(evaluationContext).getValue().intValue(); - if (endIndex == null) { - return new StringQueryResult(subjectValue.substring(startIndexValue)); - } else { - final int endIndexValue = endIndex.evaluate(evaluationContext).getValue().intValue(); - return new StringQueryResult(subjectValue.substring(startIndexValue, endIndexValue)); + try { + final int startIndexValue = startIndex.evaluate(evaluationContext).getValue().intValue(); + if (endIndex == null) { + return new StringQueryResult(subjectValue.substring(startIndexValue)); + } else { + final int endIndexValue = endIndex.evaluate(evaluationContext).getValue().intValue(); + return new StringQueryResult(subjectValue.substring(startIndexValue, endIndexValue)); + } + } catch (IndexOutOfBoundsException e) { + return new StringQueryResult(""); } } diff --git a/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java b/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java index 74fb8ec7bb..986f8f6264 100644 --- a/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java +++ b/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java @@ -1636,6 +1636,17 @@ public class TestQuery { verifyEquals("${filename:substring(4)}", attributes, "-255"); } + @Test + public void testSubstringOOB() { + final Map attributes = new HashMap<>(); + attributes.put("filename", "file-255"); + + verifyEquals("${filename:substring(10, 20)}", attributes, ""); + verifyEquals("${filename:substring(10)}", attributes, ""); + verifyEquals("${filename:substring(-2)}", attributes, ""); + verifyEquals("${filename:substring(2, -2)}", attributes, ""); + } + @Test public void testToRadix() { final Map attributes = new HashMap<>(); diff --git a/nifi-docs/src/main/asciidoc/expression-language-guide.adoc b/nifi-docs/src/main/asciidoc/expression-language-guide.adoc index b79629e09d..5e4cfb45bc 100644 --- a/nifi-docs/src/main/asciidoc/expression-language-guide.adoc +++ b/nifi-docs/src/main/asciidoc/expression-language-guide.adoc @@ -652,7 +652,7 @@ Each of the following functions manipulates a String in some way. [.description]#If the _starting index_ is larger than the _ending index_, this function call will result in an error.# [.description]#If the _starting index_ or the _ending index_ is greater than the length of the Subject or has a value - less than 0, this function call will result in an error.# + less than 0, this function call will return an empty string.# *Subject Type*: [.subject]#String#