]> source.dussan.org Git - jgit.git/commitdiff
Fix NPE in URIish when parsing an empty URI 66/3466/2
authorKetan Padegaonkar <KetanPadegaonkar@gmail.com>
Thu, 19 May 2011 07:38:26 +0000 (13:08 +0530)
committerChris Aniszczyk <caniszczyk@gmail.com>
Thu, 19 May 2011 13:58:17 +0000 (08:58 -0500)
Change-Id: Id1c42dc9843f62c581b9904b02150de53cf7777c
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/URIishTest.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java
org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java

index 01d3820184ded79da0e0043c937e721f90d32243..9b4ebf90d80f845181131dcbcbb4b300fafb6198 100644 (file)
@@ -62,6 +62,26 @@ public class URIishTest {
 
        private static final String GIT_SCHEME = "git://";
 
+       @Test
+       public void shouldRaiseErrorOnEmptyURI() throws Exception {
+               try {
+                       new URIish("");
+                       fail("expecting an exception");
+               } catch (URISyntaxException e) {
+                       // expected
+               }
+       }
+
+       @Test
+       public void shouldRaiseErrorOnNullURI() throws Exception {
+               try {
+                       new URIish((String) null);
+                       fail("expecting an exception");
+               } catch (URISyntaxException e) {
+                       // expected
+               }
+       }
+
        @Test
        public void testUnixFile() throws Exception {
                final String str = "/home/m y";
index 1c9397922afcbc4d19719a7bcdd3b0dc47129dcd..8fe7b2aa519b2aeb4dde98ba80e9d859c6e20479 100644 (file)
@@ -55,6 +55,7 @@ import java.util.regex.Pattern;
 
 import org.eclipse.jgit.JGitText;
 import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.util.StringUtils;
 
 /**
  * This URI like construct used for referencing Git archives over the net, as
@@ -193,6 +194,10 @@ public class URIish implements Serializable {
         * @throws URISyntaxException
         */
        public URIish(String s) throws URISyntaxException {
+               if (StringUtils.isEmptyOrNull(s)) {
+                       throw new URISyntaxException("The uri was empty or null",
+                                       JGitText.get().cannotParseGitURIish);
+               }
                Matcher matcher = SINGLE_SLASH_FILE_URI.matcher(s);
                if (matcher.matches()) {
                        scheme = matcher.group(1);
index 59f3d83ccf1fad0b3fd779897dc2a67aa4effb47..5e43b26931b38bda57509723e9939bfbc0b32d0f 100644 (file)
@@ -222,4 +222,15 @@ public final class StringUtils {
        private StringUtils() {
                // Do not create instances
        }
+
+       /**
+        * Test if a string is empty or null.
+        *
+        * @param stringValue
+        *            the string to check
+        * @return <code>true</code> if the string is <code>null</code> or empty
+        */
+       public static boolean isEmptyOrNull(String stringValue) {
+               return stringValue == null || stringValue.length() == 0;
+       }
 }