From e4a408d26e69ee0be1dd1debf0d12034c2a59fcc Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Wed, 12 Jan 2022 16:02:46 +0300 Subject: [PATCH] classlib: throw exception from URL constructor when invalid ':' provided --- .../src/main/java/org/teavm/classlib/java/net/TURL.java | 2 +- .../test/java/org/teavm/classlib/java/net/URLTest.java | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/net/TURL.java b/classlib/src/main/java/org/teavm/classlib/java/net/TURL.java index 0aba53fa2..fe3086bd7 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/net/TURL.java +++ b/classlib/src/main/java/org/teavm/classlib/java/net/TURL.java @@ -72,7 +72,7 @@ public final class TURL implements Serializable { throw new TMalformedURLException(e.toString()); } int startIPv6Addr = spec.indexOf('['); - if (index >= 0) { + if (index > 0) { if (startIPv6Addr == -1 || index < startIPv6Addr) { protocol = spec.substring(0, index); // According to RFC 2396 scheme part should match diff --git a/tests/src/test/java/org/teavm/classlib/java/net/URLTest.java b/tests/src/test/java/org/teavm/classlib/java/net/URLTest.java index 1768a5da8..5a40be35d 100644 --- a/tests/src/test/java/org/teavm/classlib/java/net/URLTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/net/URLTest.java @@ -228,6 +228,15 @@ public class URLTest { } catch (MalformedURLException e) { fail("Unexpected exception (jar protocol, relative path)" + e); } + + // no protocol + caught = false; + try { + u = new URL(":"); + } catch (MalformedURLException e) { + caught = true; + } + assertTrue("7 Failed to throw MalformedURLException", caught); } @Test