[EGIT PATCH 03/31] Fix Repository.resolve() to not throw runtime exceptions
- From
- Marek Zawirski <marek.zawirski@gmail.com>
- Date
- Aug 17, 2008, 20:43 UTC
- Message-ID
- <1219005852-21496-4-git-send-email-marek.zawirski@gmail.com>
- In-Reply-To
- <1219005852-21496-3-git-send-email-marek.zawirski@gmail.com>
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@gmail.com> --- .../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