From 427360f8c2e46d7919d26e13837e57bf18e6ae73 Mon Sep 17 00:00:00 2001 From: Marek Zawirski Date: Sun, 17 Aug 2008 22:43:44 +0200 Subject: [PATCH] Fix Repository.resolve() to not throw runtime exceptions 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 Signed-off-by: Robin Rosenberg --- .../src/org/spearce/jgit/lib/Repository.java | 56 +++++++++++++++++----- 1 file 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 cfc0186c..628136d8 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; } -- 2.11.4.GIT