Skip to content

Commit 61ca231

Browse files
wenshaoclaude
andcommitted
fix: DateUtils multiple bugs — bitwise OR typo, bounds check off-by-one, wrong offset and error message length
- parseLocalTime10/11/12 (byte[] and char[]): `minute` was OR'd twice instead of checking `millis`, so negative millis values silently produced null instead of being caught - parseLocalTime6/7 (byte[] and char[]): bounds check was `off + 5` but methods access up to `off + 5` / `off + 6`, causing potential ArrayIndexOutOfBoundsException - parseLocalDateTime(String, int, int): after getChars copy to a new array starting at index 0, the original `off` was still passed to the parse method, reading from wrong positions - parseMillis (byte[] and char[] variants): error message used `new String(chars, off, len - off)` which double-subtracts offset, producing truncated or incorrect error messages Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent b3eb46c commit 61ca231

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

‎core/src/main/java/com/alibaba/fastjson2/util/DateUtils.java‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public static LocalDateTime parseLocalDateTime(String str, int off, int len) {
314314
} else {
315315
char[] chars = new char[len];
316316
str.getChars(off, off + len, chars, 0);
317-
ldt = parseLocalDateTime(chars, off, len);
317+
ldt = parseLocalDateTime(chars, 0, len);
318318
}
319319

320320
if (ldt == null) {
@@ -483,7 +483,7 @@ public static LocalTime parseLocalTime5(char[] str, int off) {
483483
* @return the parsed LocalTime object, or null if the input array is null or too short
484484
*/
485485
public static LocalTime parseLocalTime6(byte[] str, int off) {
486-
if (off + 5 > str.length) {
486+
if (off + 6 > str.length) {
487487
return null;
488488
}
489489

@@ -519,7 +519,7 @@ public static LocalTime parseLocalTime6(byte[] str, int off) {
519519
* @return the parsed LocalTime object, or null if the input array is null or too short
520520
*/
521521
public static LocalTime parseLocalTime6(char[] str, int off) {
522-
if (off + 5 > str.length) {
522+
if (off + 6 > str.length) {
523523
return null;
524524
}
525525

@@ -547,7 +547,7 @@ public static LocalTime parseLocalTime6(char[] str, int off) {
547547
}
548548

549549
public static LocalTime parseLocalTime7(byte[] str, int off) {
550-
if (off + 5 > str.length) {
550+
if (off + 7 > str.length) {
551551
return null;
552552
}
553553

@@ -575,7 +575,7 @@ public static LocalTime parseLocalTime7(byte[] str, int off) {
575575
}
576576

577577
public static LocalTime parseLocalTime7(char[] str, int off) {
578-
if (off + 5 > str.length) {
578+
if (off + 7 > str.length) {
579579
return null;
580580
}
581581

@@ -678,7 +678,7 @@ public static LocalTime parseLocalTime10(byte[] str, int off) {
678678
if (millis > 0) {
679679
millis *= 100000000;
680680
}
681-
return ((hour | minute | second | minute) < 0)
681+
return ((hour | minute | second | millis) < 0)
682682
? null
683683
: LocalTime.of(hour, minute, second, millis);
684684
}
@@ -695,7 +695,7 @@ public static LocalTime parseLocalTime10(char[] str, int off) {
695695
if (millis > 0) {
696696
millis *= 100000000;
697697
}
698-
return ((hour | minute | second | minute) < 0)
698+
return ((hour | minute | second | millis) < 0)
699699
? null
700700
: LocalTime.of(hour, minute, second, millis);
701701
}
@@ -728,7 +728,7 @@ public static LocalTime parseLocalTime11(char[] str, int off) {
728728
if (millis > 0) {
729729
millis *= 10000000;
730730
}
731-
return ((hour | minute | second | minute) < 0)
731+
return ((hour | minute | second | millis) < 0)
732732
? null
733733
: LocalTime.of(hour, minute, second, millis);
734734
}
@@ -761,7 +761,7 @@ public static LocalTime parseLocalTime12(char[] str, int off) {
761761
if (millis > 0) {
762762
millis *= 1000000;
763763
}
764-
return ((hour | minute | second | minute) < 0)
764+
return ((hour | minute | second | millis) < 0)
765765
? null
766766
: LocalTime.of(hour, minute, second, millis);
767767
}
@@ -1015,7 +1015,7 @@ public static long parseMillis(byte[] chars, int off, int len, Charset charset,
10151015
) {
10161016
ZonedDateTime zdt = parseZonedDateTime(chars, off, len, zoneId);
10171017
if (zdt == null) {
1018-
String input = new String(chars, off, len - off);
1018+
String input = new String(chars, off, len);
10191019
throw new DateTimeParseException("illegal input " + input, input, 0);
10201020
}
10211021
millis = zdt.toInstant().toEpochMilli();
@@ -1107,7 +1107,7 @@ public static long parseMillis(char[] chars, int off, int len, ZoneId zoneId) {
11071107
) {
11081108
ZonedDateTime zdt = parseZonedDateTime(chars, off, len, zoneId);
11091109
if (zdt == null) {
1110-
String input = new String(chars, off, len - off);
1110+
String input = new String(chars, off, len);
11111111
throw new DateTimeParseException("illegal input " + input, input, 0);
11121112
}
11131113
millis = zdt.toInstant().toEpochMilli();
@@ -1157,7 +1157,7 @@ && getIntLE(chars, off + 8) == 0x300030L
11571157
}
11581158

11591159
if (ldt == null) {
1160-
String input = new String(chars, off, len - off);
1160+
String input = new String(chars, off, len);
11611161
throw new DateTimeParseException("illegal input " + input, input, 0);
11621162
}
11631163

0 commit comments

Comments
 (0)