This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/trunk by this push:
new 40145607ac DataFormatter: adjust years to 4 digits in a loop rather
than recursively (#1443)
40145607ac is described below
commit 40145607ac5df515d58fbbb4b454639dd525e37d
Author: PJ Fanning <[email protected]>
AuthorDate: Sun Oct 18 11:22:40 2026 +0100
DataFormatter: adjust years to 4 digits in a loop rather than recursively
(#1443)
adjustTo4DigitYearsIfConfigured recursed once per "yy" and searched the
rest of the format each time, so a long format with many "yy" threw
StackOverflowError when 4 digit years are configured. The loop gives the
same output, including stopping at a 3 digit year.
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../org/apache/poi/ss/usermodel/DataFormatter.java | 33 ++++++++++------------
.../apache/poi/ss/usermodel/TestDataFormatter.java | 15 ++++++++++
2 files changed, 30 insertions(+), 18 deletions(-)
diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java
b/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java
index e1fb9332e6..6f9370c455 100644
--- a/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java
+++ b/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java
@@ -692,27 +692,24 @@ public class DataFormatter {
}
String adjustTo4DigitYearsIfConfigured(String format) {
- if (use4DigitYearsInAllDateFormats) {
- int ypos2 = format.indexOf("yy");
- if (ypos2 < 0) {
- return format;
+ if (!use4DigitYearsInAllDateFormats) {
+ return format;
+ }
+ // a loop rather than recursing once per "yy", which overflowed the
stack on a long format
+ StringBuilder sb = new StringBuilder(format.length() + 16);
+ int pos = 0;
+ for (int ypos = format.indexOf("yy"); ypos >= 0; ypos =
format.indexOf("yy", pos)) {
+ if (format.startsWith("yyyy", ypos)) {
+ sb.append(format, pos, ypos + 4);
+ pos = ypos + 4;
+ } else if (format.startsWith("yyy", ypos)) {
+ break;
} else {
- int ypos3 = format.indexOf("yyy");
- int ypos4 = format.indexOf("yyyy");
- if (ypos4 == ypos2) {
- String part1 = format.substring(0, ypos2 + 4);
- String part2 = format.substring(ypos2 + 4);
- return part1 + adjustTo4DigitYearsIfConfigured(part2);
- } else if (ypos3 == ypos2) {
- return format;
- } else {
- String part1 = format.substring(0, ypos2 + 2);
- String part2 = format.substring(ypos2 + 2);
- return part1 + "yy" +
adjustTo4DigitYearsIfConfigured(part2);
- }
+ sb.append(format, pos, ypos + 2).append("yy");
+ pos = ypos + 2;
}
}
- return format;
+ return sb.append(format, pos, format.length()).toString();
}
private static char toLowerAscii(char c) {
diff --git
a/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java
b/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java
index 950c1f3b21..6de9ac274e 100644
--- a/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java
+++ b/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java
@@ -1426,6 +1426,21 @@ class TestDataFormatter {
String formatString4 = "[$-F400]m/d/yy h:mm:ss\\ AM/PM;[$-F400]m/d/yy
h:mm:ss\\ AM/PM;_-* \"\"??_-;_-@_-";
assertEquals(formatString4.replace("yy", "yyyy"),
dataFormatter.adjustTo4DigitYearsIfConfigured(formatString4));
+
+ // a 3 digit year stops the adjustment of the rest of the format
+ assertEquals("yyyy/yyy/yy",
dataFormatter.adjustTo4DigitYearsIfConfigured("yy/yyy/yy"));
+ }
+
+ @Test
+ void testAdjustTo4DigitYearsWithLongFormat() {
+ DataFormatter dataFormatter = new DataFormatter();
+ dataFormatter.setUse4DigitYearsInAllDateFormats(true);
+
+ // this used to recurse once per "yy" and throw StackOverflowError
+ String format = "yy/".repeat(50_000);
+ String result = assertTimeoutPreemptively(Duration.ofSeconds(10),
+ () -> dataFormatter.adjustTo4DigitYearsIfConfigured(format));
+ assertEquals("yyyy/".repeat(50_000), result);
}
@Test
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]