Skip to content

Commit 9bc68b9

Browse files
nosanmbhave
authored andcommitted
Limit ChronoField values to their range
See gh-19595
1 parent faa38c9 commit 9bc68b9

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/CentralDirectoryFileHeader.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,8 +17,11 @@
1717
package org.springframework.boot.loader.jar;
1818

1919
import java.io.IOException;
20-
import java.time.LocalDateTime;
2120
import java.time.ZoneId;
21+
import java.time.ZonedDateTime;
22+
import java.time.temporal.ChronoField;
23+
import java.time.temporal.ChronoUnit;
24+
import java.time.temporal.ValueRange;
2225

2326
import org.springframework.boot.loader.data.RandomAccessData;
2427

@@ -124,10 +127,14 @@ public long getTime() {
124127
* @return the date and time as milliseconds since the epoch
125128
*/
126129
private long decodeMsDosFormatDateTime(long datetime) {
127-
LocalDateTime localDateTime = LocalDateTime.of((int) (((datetime >> 25) & 0x7f) + 1980),
128-
(int) ((datetime >> 21) & 0x0f), (int) ((datetime >> 16) & 0x1f), (int) ((datetime >> 11) & 0x1f),
129-
(int) ((datetime >> 5) & 0x3f), (int) ((datetime << 1) & 0x3e));
130-
return localDateTime.toEpochSecond(ZoneId.systemDefault().getRules().getOffset(localDateTime)) * 1000;
130+
int year = getChronoValue(((datetime >> 25) & 0x7f) + 1980, ChronoField.YEAR);
131+
int month = getChronoValue((datetime >> 21) & 0x0f, ChronoField.MONTH_OF_YEAR);
132+
int day = getChronoValue((datetime >> 16) & 0x1f, ChronoField.DAY_OF_MONTH);
133+
int hour = getChronoValue((datetime >> 11) & 0x1f, ChronoField.HOUR_OF_DAY);
134+
int minute = getChronoValue((datetime >> 5) & 0x3f, ChronoField.MINUTE_OF_HOUR);
135+
int second = getChronoValue((datetime << 1) & 0x3e, ChronoField.SECOND_OF_MINUTE);
136+
return ZonedDateTime.of(year, month, day, hour, minute, second, 0, ZoneId.systemDefault()).toInstant()
137+
.truncatedTo(ChronoUnit.SECONDS).toEpochMilli();
131138
}
132139

133140
public long getCrc() {
@@ -172,4 +179,9 @@ public static CentralDirectoryFileHeader fromRandomAccessData(RandomAccessData d
172179
return fileHeader;
173180
}
174181

182+
private static int getChronoValue(long value, ChronoField field) {
183+
ValueRange range = field.range();
184+
return Math.toIntExact(Math.min(Math.max(value, range.getMinimum()), range.getMaximum()));
185+
}
186+
175187
}

0 commit comments

Comments
 (0)