From 81d74043e943db4471a85a7ec9ca28978ae3643f Mon Sep 17 00:00:00 2001 From: Sven Strickroth Date: Fri, 2 Feb 2024 22:48:12 +0100 Subject: [PATCH] Eliminate unnecessary nested statement(s) within else clauses Signed-off-by: Sven Strickroth --- .../java/org/owasp/html/AttributePolicy.java | 3 +- src/main/java/org/owasp/html/CssTokens.java | 51 +++++++++---------- src/main/java/org/owasp/html/Handler.java | 3 +- .../java/org/owasp/html/HtmlEntities.java | 5 +- src/main/java/org/owasp/html/HtmlLexer.java | 7 ++- .../org/owasp/html/HtmlPolicyBuilder.java | 3 +- .../org/owasp/html/HtmlStreamRenderer.java | 31 ++++++----- .../java/org/owasp/html/PolicyFactory.java | 11 ++-- src/main/java/org/owasp/html/Trie.java | 3 +- .../java/org/owasp/html/ExamplesTest.java | 3 +- .../owasp/html/HtmlSanitizerFuzzerTest.java | 3 +- 11 files changed, 54 insertions(+), 69 deletions(-) diff --git a/src/main/java/org/owasp/html/AttributePolicy.java b/src/main/java/org/owasp/html/AttributePolicy.java index 56668f87..ce174941 100644 --- a/src/main/java/org/owasp/html/AttributePolicy.java +++ b/src/main/java/org/owasp/html/AttributePolicy.java @@ -93,9 +93,8 @@ static final class AttributePolicyJoiner Optional> split(AttributePolicy x) { if (x instanceof JoinedAttributePolicy) { return Optional.of(((JoinedAttributePolicy) x).policies); - } else { - return Optional.empty(); } + return Optional.empty(); } @Override diff --git a/src/main/java/org/owasp/html/CssTokens.java b/src/main/java/org/owasp/html/CssTokens.java index 4c2ec3ad..a72a07f5 100644 --- a/src/main/java/org/owasp/html/CssTokens.java +++ b/src/main/java/org/owasp/html/CssTokens.java @@ -736,16 +736,15 @@ private boolean consumeIgnorable() { if (ast < 0) { pos = cssLimit; // Unclosed /* comment */ break; - } else { - // Advance over a run of '*'s. - pos = ast + 1; - while (pos < cssLimit && css.charAt(pos) == '*') { - ++pos; - } - if (pos < cssLimit && css.charAt(pos) == '/') { - ++pos; - break; - } + } + // Advance over a run of '*'s. + pos = ast + 1; + while (pos < cssLimit && css.charAt(pos) == '*') { + ++pos; + } + if (pos < cssLimit && css.charAt(pos) == '/') { + ++pos; + break; } } } else if (next == '/') { // Non-standard but widely supported @@ -778,10 +777,9 @@ private boolean consumeIgnorable() { } if (pos == posBefore) { return false; - } else { - breakOutput(); - return true; } + breakOutput(); + return true; } private void breakOutput() { @@ -846,9 +844,8 @@ private boolean consumeAtKeyword() { --pos; // back up over '@' sb.setLength(bufferLengthBeforeWrite); // Unwrite the '@' return false; - } else { - return true; } + return true; } @@ -1122,11 +1119,10 @@ private TokenType consumeString() { pos += 2; } continue; - } else { - decoded = consumeAndDecodeEscapeSequence(); - if (decoded < 0) { - break; - } + } + decoded = consumeAndDecodeEscapeSequence(); + if (decoded < 0) { + break; } } else { ++pos; @@ -1137,11 +1133,11 @@ private TokenType consumeString() { if (closed) { sb.append('\''); return TokenType.STRING; - } else { // Drop s - sb.setLength(startOfStringOnOutput); - breakOutput(); - return TokenType.WHITESPACE; } + // Drop s + sb.setLength(startOfStringOnOutput); + breakOutput(); + return TokenType.WHITESPACE; } private @Nullable TokenType consumeHash() { @@ -1258,11 +1254,10 @@ private boolean consumeUnicodeRange() { sb.setCharAt(bufferStart + 1, 'r'); sb.setCharAt(bufferStart + 2, 'l'); return TokenType.URL; - } else { - sb.setLength(bufferStart); - breakOutput(); - return TokenType.WHITESPACE; } + sb.setLength(bufferStart); + breakOutput(); + return TokenType.WHITESPACE; } else if (parenAfter) { openBracket('('); ++pos; diff --git a/src/main/java/org/owasp/html/Handler.java b/src/main/java/org/owasp/html/Handler.java index 73339a96..5ead7ddd 100644 --- a/src/main/java/org/owasp/html/Handler.java +++ b/src/main/java/org/owasp/html/Handler.java @@ -57,9 +57,8 @@ public void handle(Object x) { public void handle(Throwable th) { if (th instanceof RuntimeException) { throw (RuntimeException) th; - } else { - throw new AssertionError(null, th); } + throw new AssertionError(null, th); } }; } diff --git a/src/main/java/org/owasp/html/HtmlEntities.java b/src/main/java/org/owasp/html/HtmlEntities.java index bc5a80c0..fc95720f 100644 --- a/src/main/java/org/owasp/html/HtmlEntities.java +++ b/src/main/java/org/owasp/html/HtmlEntities.java @@ -2467,10 +2467,9 @@ public static int appendDecodedEntity( if (codepoint < 0) { sb.append('&'); return offset + 1; - } else { - sb.appendCodePoint(codepoint); - return tail; } + sb.appendCodePoint(codepoint); + return tail; } private static boolean isHtmlIdContinueChar(char ch) { diff --git a/src/main/java/org/owasp/html/HtmlLexer.java b/src/main/java/org/owasp/html/HtmlLexer.java index cc6d2bbf..592fe3d7 100644 --- a/src/main/java/org/owasp/html/HtmlLexer.java +++ b/src/main/java/org/owasp/html/HtmlLexer.java @@ -146,11 +146,10 @@ protected HtmlToken produce() { state = State.SAW_EQ; // Skip the '=' token return produce(); - } else { - // Reclassify as attribute name - token = HtmlInputSplitter.reclassify( - token, HtmlTokenType.ATTRNAME); } + // Reclassify as attribute name + token = HtmlInputSplitter.reclassify( + token, HtmlTokenType.ATTRNAME); } else { state = State.IN_TAG; } diff --git a/src/main/java/org/owasp/html/HtmlPolicyBuilder.java b/src/main/java/org/owasp/html/HtmlPolicyBuilder.java index 54c0305f..3e07cb5b 100644 --- a/src/main/java/org/owasp/html/HtmlPolicyBuilder.java +++ b/src/main/java/org/owasp/html/HtmlPolicyBuilder.java @@ -855,9 +855,8 @@ private HtmlTagSkipType getHtmlTagSkipType(String elementName) { if (htmlTagSkipType == null) { if (DEFAULT_SKIP_TAG_MAP_IF_EMPTY_ATTR.containsKey(elementName)) { return HtmlTagSkipType.SKIP_BY_DEFAULT; - } else { - return HtmlTagSkipType.DO_NOT_SKIP_BY_DEFAULT; } + return HtmlTagSkipType.DO_NOT_SKIP_BY_DEFAULT; } return htmlTagSkipType; diff --git a/src/main/java/org/owasp/html/HtmlStreamRenderer.java b/src/main/java/org/owasp/html/HtmlStreamRenderer.java index c0c9d5bf..fdb68189 100644 --- a/src/main/java/org/owasp/html/HtmlStreamRenderer.java +++ b/src/main/java/org/owasp/html/HtmlStreamRenderer.java @@ -269,23 +269,22 @@ private final void writeCloseTag(String uncanonElementName) if (!lastTagOpened.equals(elementName)) { error("Tag content cannot appear inside CDATA element", elementName); return; - } else { - StringBuilder cdataContent = pendingUnescaped; - pendingUnescaped = null; - Encoding.stripBannedCodeunits(cdataContent); - int problemIndex = checkHtmlCdataCloseable(lastTagOpened, cdataContent); - if (problemIndex == -1) { - if (cdataContent.length() != 0) { - output.append(cdataContent); - } - } else { - error( - "Invalid CDATA text content", - cdataContent.subSequence( - problemIndex, - Math.min(problemIndex + 10, cdataContent.length()))); - // Still output the close tag. + } + StringBuilder cdataContent = pendingUnescaped; + pendingUnescaped = null; + Encoding.stripBannedCodeunits(cdataContent); + int problemIndex = checkHtmlCdataCloseable(lastTagOpened, cdataContent); + if (problemIndex == -1) { + if (cdataContent.length() != 0) { + output.append(cdataContent); } + } else { + error( + "Invalid CDATA text content", + cdataContent.subSequence( + problemIndex, + Math.min(problemIndex + 10, cdataContent.length()))); + // Still output the close tag. } if ("plaintext".equals(elementName)) { return; } } diff --git a/src/main/java/org/owasp/html/PolicyFactory.java b/src/main/java/org/owasp/html/PolicyFactory.java index cc26aaf4..0025c63b 100644 --- a/src/main/java/org/owasp/html/PolicyFactory.java +++ b/src/main/java/org/owasp/html/PolicyFactory.java @@ -95,12 +95,11 @@ public HtmlSanitizer.Policy apply( @Nullable CTX context) { if (listener == null) { return apply(out); - } else { - HtmlChangeReporter r = new HtmlChangeReporter<>( - out, listener, context); - r.setPolicy(apply(r.getWrappedRenderer())); - return r.getWrappedPolicy(); - } + } + HtmlChangeReporter r = new HtmlChangeReporter<>( + out, listener, context); + r.setPolicy(apply(r.getWrappedRenderer())); + return r.getWrappedPolicy(); } /** A convenience function that sanitizes a string of HTML. */ diff --git a/src/main/java/org/owasp/html/Trie.java b/src/main/java/org/owasp/html/Trie.java index 2a923c43..8da1bcd2 100644 --- a/src/main/java/org/owasp/html/Trie.java +++ b/src/main/java/org/owasp/html/Trie.java @@ -80,9 +80,8 @@ private Trie( this.childMap = ZERO_CHARS; this.children = ((Trie[]) ZERO_TRIES); return; - } else { - ++pos; } + ++pos; } else { this.value = null; } diff --git a/src/test/java/org/owasp/html/ExamplesTest.java b/src/test/java/org/owasp/html/ExamplesTest.java index adb8e251..e7c5a78b 100644 --- a/src/test/java/org/owasp/html/ExamplesTest.java +++ b/src/test/java/org/owasp/html/ExamplesTest.java @@ -66,9 +66,8 @@ public static final void testExamplesRun() throws Exception { + captured.toString("UTF-8")); if (ex instanceof RuntimeException) { throw (RuntimeException) ex; - } else { - throw new AssertionError(null, ex); } + throw new AssertionError(null, ex); } finally { System.setIn(stdin); System.setOut(stdout); diff --git a/src/test/java/org/owasp/html/HtmlSanitizerFuzzerTest.java b/src/test/java/org/owasp/html/HtmlSanitizerFuzzerTest.java index c302dd8e..17502126 100644 --- a/src/test/java/org/owasp/html/HtmlSanitizerFuzzerTest.java +++ b/src/test/java/org/owasp/html/HtmlSanitizerFuzzerTest.java @@ -149,9 +149,8 @@ public void run() { if (failure != null) { if (failure instanceof RuntimeException) { throw (RuntimeException) failure; - } else { - throw new AssertionError(null, failure); } + throw new AssertionError(null, failure); } }