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 dc577a8cbc WorkbookUtil.validateSheetName: show control chars as
U+XXXX (#1435)
dc577a8cbc is described below
commit dc577a8cbcdec22ae5547615502d63b2051f35a6
Author: PJ Fanning <[email protected]>
AuthorDate: Sat Oct 17 22:29:29 2026 +0100
WorkbookUtil.validateSheetName: show control chars as U+XXXX (#1435)
Follow-up to #1433. The error for an invalid sheet-name char embedded the
char itself, which for 0x0000 and 0x0003 is invisible in logs. Show
control chars as a code point, and assert the full messages in the tests.
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
poi/src/main/java/org/apache/poi/ss/util/WorkbookUtil.java | 8 +++++++-
.../test/java/org/apache/poi/ss/util/TestWorkbookUtil.java | 13 +++++++++++--
2 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/poi/src/main/java/org/apache/poi/ss/util/WorkbookUtil.java
b/poi/src/main/java/org/apache/poi/ss/util/WorkbookUtil.java
index 3b8bb2c97e..ac882e0c74 100644
--- a/poi/src/main/java/org/apache/poi/ss/util/WorkbookUtil.java
+++ b/poi/src/main/java/org/apache/poi/ss/util/WorkbookUtil.java
@@ -17,6 +17,8 @@
package org.apache.poi.ss.util;
+import java.util.Locale;
+
/**
* Helper methods for when working with Usermodel Workbooks
*/
@@ -149,7 +151,11 @@ public class WorkbookUtil {
// all other chars OK
continue;
}
- throw new IllegalArgumentException("Invalid char (" + ch
+ // show control chars such as 0x0000 as a code point, not as an
invisible char
+ String shownChar = Character.isISOControl(ch)
+ ? String.format(Locale.ROOT, "U+%04X", (int) ch)
+ : String.valueOf(ch);
+ throw new IllegalArgumentException("Invalid char (" + shownChar
+ ") found at index (" + i + ") in sheet name '" +
sheetName + "'");
}
if (sheetName.charAt(0) == '\'' || sheetName.charAt(len-1) == '\'') {
diff --git a/poi/src/test/java/org/apache/poi/ss/util/TestWorkbookUtil.java
b/poi/src/test/java/org/apache/poi/ss/util/TestWorkbookUtil.java
index dbdee8ec8f..d816c916d6 100644
--- a/poi/src/test/java/org/apache/poi/ss/util/TestWorkbookUtil.java
+++ b/poi/src/test/java/org/apache/poi/ss/util/TestWorkbookUtil.java
@@ -91,9 +91,18 @@ final class TestWorkbookUtil {
@Test
void testValidateSheetNameRejectsControlCharacters() {
- assertThrows(IllegalArgumentException.class,
+ IllegalArgumentException e =
assertThrows(IllegalArgumentException.class,
() -> WorkbookUtil.validateSheetName("before\u0000after"));
- assertThrows(IllegalArgumentException.class,
+ assertEquals("Invalid char (U+0000) found at index (6) in sheet name
'before\u0000after'", e.getMessage());
+ e = assertThrows(IllegalArgumentException.class,
() -> WorkbookUtil.validateSheetName("before\u0003after"));
+ assertEquals("Invalid char (U+0003) found at index (6) in sheet name
'before\u0003after'", e.getMessage());
+ }
+
+ @Test
+ void testValidateSheetNameMessageShowsPrintableChar() {
+ IllegalArgumentException e =
assertThrows(IllegalArgumentException.class,
+ () -> WorkbookUtil.validateSheetName("a:b"));
+ assertEquals("Invalid char (:) found at index (1) in sheet name
'a:b'", e.getMessage());
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]