Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive BED_BOGUS_EXCEPTION_DECLARATION for method that calls String.getBytes(String) #406

Closed
pzygielo opened this issue Aug 5, 2020 · 5 comments

Comments

@pzygielo
Copy link

pzygielo commented Aug 5, 2020

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#getBytes(java.lang.String) throws, unlike other getBytes overloads.

Reproducer: https://github.com/pzrep/fb-contrib-BED_BOGUS_EXCEPTION_DECLARATION.
Call ./mvnw verify to observe:

[INFO] --- spotbugs-maven-plugin:4.0.4:check (default) @ BED_BOGUS_EXCEPTION_DECLARATION ---
[INFO] BugInstance size is 1
[INFO] Error size is 0
[INFO] Total bugs: 1
[ERROR] Medium: Non derivable method pzrep.C.m(String) declares throwing an exception that isn't thrown [pzrep.C] At C.java:[line 7] BED_BOGUS_EXCEPTION_DECLARATION

where C.java:

     1	package pzrep;
     2	
     3	import java.io.UnsupportedEncodingException;
     4	
     5	public class C {
     6	    private void m(String p) throws UnsupportedEncodingException {
     7	        byte[] v = p.getBytes("UTF8");
     8	    }
     9	}

SB plugin version: com.mebigfatguy.sb-contrib:sb-contrib:7.4.7

@pzygielo
Copy link
Author

pzygielo commented Aug 5, 2020

I think it is because j.l.String is listed here

private static final Set<String> safeClasses = UnmodifiableSet.create(
// @formatter:off
Values.SLASHED_JAVA_LANG_OBJECT, Values.SLASHED_JAVA_LANG_STRING, Values.SLASHED_JAVA_LANG_INTEGER,
Values.SLASHED_JAVA_LANG_LONG, Values.SLASHED_JAVA_LANG_FLOAT, Values.SLASHED_JAVA_LANG_DOUBLE,
Values.SLASHED_JAVA_LANG_SHORT, Values.SLASHED_JAVA_LANG_BYTE, Values.SLASHED_JAVA_LANG_BOOLEAN
// @formatter:on
);
which then (on opcode invokevirtual (182) in this case) prevents to take the branch at

In effect exception is not removed at

removeThrownExceptionHierarchy(thrownException);

@pzygielo

This comment was marked as outdated.

@pzygielo
Copy link
Author

pzygielo commented Dec 2, 2023

Hi, @mebigfatguy

As this repo seems to be alive again - could you check if this one is fixable, please?

If needed, reproducer updated to use sb-contrib:7.6.2, can be published again.

@mebigfatguy
Copy link
Owner

targetted for 7.6.3

@pzygielo
Copy link
Author

pzygielo commented Dec 3, 2023

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants