You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Yudan Liu (Jira)" <ji...@apache.org> on 2025/07/30 09:11:00 UTC

[jira] [Commented] (LANG-1774) ClassUtils.getShortCanonicalName can not handle invalid input data

    [ https://issues.apache.org/jira/browse/LANG-1774?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18010867#comment-18010867 ] 

Yudan Liu commented on LANG-1774:
---------------------------------

!screenshot-1.png|width=688,height=241!

I've successfully reproduced this problem and would like to address it.

I'd like to suggest an approach to resolve this issue:
 # Add proper validation to the `{{{}getShortCanonicalName(String canonicalName){}}}` method
 # Implement checks to ensure input conforms to JVM class name specifications
 # Throw clear `{{{}IllegalArgumentException`{}}} with descriptive messages for invalid inputs

Additionally, I will add unit test cases to cover edge scenarios.

How does that sound to you? I will commit the changes as soon as I've completed the implementation and testing.

> ClassUtils.getShortCanonicalName can not handle invalid input data
> ------------------------------------------------------------------
>
>                 Key: LANG-1774
>                 URL: https://issues.apache.org/jira/browse/LANG-1774
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.*
>    Affects Versions: 3.17.0
>            Reporter: Zhongxin Yan
>            Priority: Major
>         Attachments: screenshot-1.png
>
>
> {code:java}
> // code placeholder
> @Test
> public void getShortCanonicalName(){
>     String a = ClassUtils.getShortCanonicalName("[[");
>     String b = ClassUtils.getShortCanonicalName("[[L");
>     String c = ClassUtils.getShortCanonicalName("[IPackageName.TestClassUtils");
>     System.out.println("a="+a+" b="+b+" c="+c);
> }{code}
>  
> output result:  a=][][]  b=][][]  c=int[]
>  ----------------------------------------------------------------
> The method comment of ClassUtils.getShortCanonicalName(final String canonicalName) states "The string passed in is assumed to be a class name - it is not checked". However, when the input parameter canonicalName does not conform to the JVM class name specification, the behavior of the getShortCanonicalName method may be unpredictable, which may produce misleading results for developers, for example, returning "][][]", which is obviously not a reasonable result. Developers may expect the method to handle more edge cases, or at least give a more reasonable response to obviously wrong input, such as returning an empty string or throwing an exception.
> Suggestions:
> 1. Input validation: Add a check inside the ClassUtils.getShortCanonicalName or getCanonicalName(String className) method to check whether the input string conforms to the JVM class name specification. For illegal input, you can throw `IllegalArgumentException` or return an empty string to avoid misleading results, such as "][][]".
> 2. Update the document: Make it clear that the method is only applicable to input that conforms to the JVM class name specification, and remind the caller to verify the input first when they are unsure of the legality of the input.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)