aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIvan Frade <ifrade@google.com>2024-12-09 10:54:51 -0800
committerIvan Frade <ifrade@google.com>2024-12-09 12:49:43 -0800
commite081e9b9049ff3ffb085cae40d50ebdfb563ef17 (patch)
treea6ffa2d2047c5053d6b7ad72b66759e089e7df2d
parentb4017a6f8eb97a6509d3772d5ab7aa8ce451c8a9 (diff)
downloadjgit-e081e9b9049ff3ffb085cae40d50ebdfb563ef17.tar.gz
jgit-e081e9b9049ff3ffb085cae40d50ebdfb563ef17.zip
RawParseUtils: Default to UTC in invalid timezones
PersonIdent used to translate invalid timezones to UTC [1], but the new java.time code just throws an exception. Also the parsing used happen on demand, but now is done in the constructor, so the exception is thrown even if the timezone is not used at all. Check the parsed timezone and default to UTC if it is out of range. [1] https://docs.oracle.com/javase/8/docs/api/java/util/TimeZone.html#getTimeZone-java.lang.String- Change-Id: I90dd7d842ac8f44caef3b76d57375dead76bebde
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RawParseUtils_ParsePersonIdentTest.java15
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java10
2 files changed, 23 insertions, 2 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RawParseUtils_ParsePersonIdentTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RawParseUtils_ParsePersonIdentTest.java
index e517889c83..6d23db81d8 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RawParseUtils_ParsePersonIdentTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RawParseUtils_ParsePersonIdentTest.java
@@ -107,6 +107,21 @@ public class RawParseUtils_ParsePersonIdentTest {
assertPersonIdent("Me <me@example.com 1234567890 -0700", null);
}
+ @Test
+ public void testParsePersonIdent_badTz() {
+ PersonIdent tooBig = RawParseUtils
+ .parsePersonIdent("Me <me@example.com> 1234567890 +8315");
+ assertEquals(tooBig.getZoneOffset().getTotalSeconds(), 0);
+
+ PersonIdent tooSmall = RawParseUtils
+ .parsePersonIdent("Me <me@example.com> 1234567890 -8315");
+ assertEquals(tooSmall.getZoneOffset().getTotalSeconds(), 0);
+
+ PersonIdent notATime = RawParseUtils
+ .parsePersonIdent("Me <me@example.com> 1234567890 -0370");
+ assertEquals(notATime.getZoneOffset().getTotalSeconds(), 0);
+ }
+
private static void assertPersonIdent(String line, PersonIdent expected) {
PersonIdent actual = RawParseUtils.parsePersonIdent(line);
assertEquals(expected, actual);
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java
index 1fc695959a..3ed72516c7 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java
@@ -32,6 +32,7 @@ import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
+import java.time.DateTimeException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
@@ -473,12 +474,17 @@ public final class RawParseUtils {
* @param ptrResult
* optional location to return the new ptr value through. If null
* the ptr value will be discarded.
- * @return the ZoneOffset represention of the timezone offset string
+ * @return the ZoneOffset represention of the timezone offset string.
+ * Invalid offsets default to UTC.
*/
private static ZoneId parseZoneOffset(final byte[] b, int ptr,
MutableInteger ptrResult) {
int hhmm = parseBase10(b, ptr, ptrResult);
- return ZoneOffset.ofHoursMinutes(hhmm / 100, hhmm % 100);
+ try {
+ return ZoneOffset.ofHoursMinutes(hhmm / 100, hhmm % 100);
+ } catch (DateTimeException e) {
+ return UTC;
+ }
}
/**