yGuard icon indicating copy to clipboard operation
yGuard copied to clipboard

Class in default package causes StringIndexOutOfBoundsException

Open bernhardf-ro opened this issue 6 months ago • 3 comments

A class in the default package causes a StringIndexOutOfBoundsException in the following code: https://github.com/yWorks/yGuard/blob/06fc74781e21bc9dd05ed19aed34bee7eb4a42f0/src/main/java/com/yworks/yguard/ant/PackageSection.java#L68-L79 match.substring(0, slashIndex) gets a slashIndex of -1 from match.lastIndexOf('/')

Obfuscating any JAR with a class in the default package should be enough to reproduce the issue.

Ideally such classes would be handled like other classes. But if they are considered unsupported a clear exception would be appreciated.

bernhardf-ro avatar Jun 13 '25 15:06 bernhardf-ro

Just out of curiosity @bernhardf-ro did you use unnamed classes? This was a recent addition in Java 21, which would place code in the default package. Otherwise, this behaviour is strongly discouraged. I'm saying this, because this code has worked relatively well for over two decades... 😆

Fohlen avatar Jun 17 '25 10:06 Fohlen

Nevermind, it appears unnamed code is placed in the unnamed package. Anyhow - I'm curious as to what your use case is!

Fohlen avatar Jun 17 '25 11:06 Fohlen

We do not have any use-case for classes in the default package. We found this issue purely by accident. However, finding the reason for the exception required looking into the source code. Fixing the exception (as apparently already done) or throwing an exception with clear a message are both equally fine solutions.

bernhardf-ro avatar Jun 17 '25 14:06 bernhardf-ro