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

MS_EXPOSE_REP triggered for avro's BinaryMessageDecoder #1669

Closed
hejcz opened this issue Aug 28, 2021 · 4 comments
Closed

MS_EXPOSE_REP triggered for avro's BinaryMessageDecoder #1669

hejcz opened this issue Aug 28, 2021 · 4 comments

Comments

@hejcz
Copy link

hejcz commented Aug 28, 2021

I believe this is false positive. Why is BinaryMessageDecoder treated as an array?

MS: Public static method may expose internal representation by returning array (MS_EXPOSE_REP)

A public static method returns a reference to an array that is part of the static state of the class. Any code that calls this method can freely modify the underlying array. One fix is to return a copy of the array.
[INFO] --- spotbugs-maven-plugin:4.3.0:check (default) @ spotbugs-bug ---
[INFO] BugInstance size is 1
[INFO] Error size is 0
[INFO] Total bugs: 1
[ERROR] Medium: Public static io.github.hejcz.TestClass.getDecoder() may expose internal representation by returning TestClass.DECODER [io.github.hejcz.TestClass] At TestClass.java:[line 16] MS_EXPOSE_REP
package io.github.hejcz;

import org.apache.avro.message.BinaryMessageDecoder;
import org.apache.avro.specific.SpecificData;

public class TestClass {

    private static SpecificData MODEL$ = new SpecificData();

    public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("");

    private static final BinaryMessageDecoder<TestClass> DECODER =
            new BinaryMessageDecoder<TestClass>(MODEL$, SCHEMA$);

    public static BinaryMessageDecoder<TestClass> getDecoder() {
        return DECODER; // spotbugs detects bug in this line
    }

}
<plugin>
    <groupId>com.github.spotbugs</groupId>
    <artifactId>spotbugs-maven-plugin</artifactId>
    <version>4.3.0</version>
    <configuration>
        <failOnError>true</failOnError>
        <includeTests>false</includeTests>
    </configuration>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>
@welcome
Copy link

welcome bot commented Aug 28, 2021

Thanks for opening your first issue here! 😃
Please check our contributing guideline. Especially when you report a problem, make sure you share a Minimal, Complete, and Verifiable example to reproduce it in this issue.

@hejcz
Copy link
Author

hejcz commented Aug 28, 2021

I tried to create an example but the test seems to pass. Maybe there is some problem with a plugin or it is fixed on master or the problem is java 16 we use.

An attempt to reproduce the problem: hejcz@1be7ac0
Project that actually fails: https://github.com/hejcz/spotbugs-avro-fp

@kuzmeo
Copy link

kuzmeo commented Nov 18, 2021

Issue is reproducible if you set report level to "medium" and when your method returns some private static instance of class, that has some fields.
I think, I got what is meant under MS_EXPOSE_REP warning: if you return some static private instance of class, someone could set some fields in that instance, so it will be changed, but it should not be.

@gtoison
Copy link
Contributor

gtoison commented Aug 27, 2023

BinaryMessageDecoder is deemed mutable because it has the addSchema() method, see #1797
The misleading message (only mentionning arrays) was fixed in #2535

@gtoison gtoison closed this as completed Aug 27, 2023
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

3 participants