[EGIT PATCH 03/31] Fix Repository.resolve() to not throw runtime exceptions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



resolve() was throwing undocumented runtime exceptions.
ArrayIndexOutOfBoundsException was thrown when it couldn't find commit's
parents and NumberFormatException when it couldn't parse parents number.

Now it returns null when it can't find appropriate commit's parents
and (already checked) RevisionSyntaxException when it can't parse number.

Signed-off-by: Marek Zawirski <marek.zawirski@xxxxxxxxx>
---
 .../src/org/spearce/jgit/lib/Repository.java       |   56 ++++++++++++++++----
 1 files changed, 45 insertions(+), 11 deletions(-)

diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
index 17cdb40..756e3b9 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
@@ -569,9 +569,22 @@ public class Repository {
 								break;
 						}
 						String parentnum = new String(rev, i+1, j-i-1);
-						int pnum = Integer.parseInt(parentnum);
-						if (pnum != 0)
-							refId = ((Commit)ref).getParentIds()[pnum - 1];
+						int pnum;
+						try {
+							pnum = Integer.parseInt(parentnum);
+						} catch (NumberFormatException e) {
+							throw new RevisionSyntaxException(
+									"Invalid commit parent number",
+									revstr);
+						}
+						if (pnum != 0) {
+							final ObjectId parents[] = ((Commit) ref)
+									.getParentIds();
+							if (pnum > parents.length)
+								refId = null;
+							else
+								refId = parents[pnum - 1];
+						}
 						i = j - 1;
 						break;
 					case '{':
@@ -632,17 +645,27 @@ public class Repository {
 						break;
 					default:
 						ref = mapObject(refId, null);
-						if (ref instanceof Commit)
-							refId = ((Commit)ref).getParentIds()[0];
-						else
+						if (ref instanceof Commit) {
+							final ObjectId parents[] = ((Commit) ref)
+									.getParentIds();
+							if (parents.length == 0)
+								refId = null;
+							else
+								refId = parents[0];
+						} else
 							throw new IncorrectObjectTypeException(refId,  Constants.TYPE_COMMIT);
 						
 					}
 				} else {
 					ref = mapObject(refId, null);
-					if (ref instanceof Commit)
-						refId = ((Commit)ref).getParentIds()[0];
-					else
+					if (ref instanceof Commit) {
+						final ObjectId parents[] = ((Commit) ref)
+								.getParentIds();
+						if (parents.length == 0)
+							refId = null;
+						else
+							refId = parents[0];
+					} else
 						throw new IncorrectObjectTypeException(refId,  Constants.TYPE_COMMIT);
 				}
 				break;
@@ -658,9 +681,20 @@ public class Repository {
 						break;
 				}
 				String distnum = new String(rev, i+1, l-i-1);
-				int dist = Integer.parseInt(distnum);
+				int dist;
+				try {
+					dist = Integer.parseInt(distnum);
+				} catch (NumberFormatException e) {
+					throw new RevisionSyntaxException(
+							"Invalid ancestry length", revstr);
+				}
 				while (dist >= 0) {
-					refId = ((Commit)ref).getParentIds()[0];
+					final ObjectId[] parents = ((Commit) ref).getParentIds();
+					if (parents.length == 0) {
+						refId = null;
+						break;
+					}
+					refId = parents[0];
 					ref = mapCommit(refId);
 					--dist;
 				}
-- 
1.5.6.3

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux