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

Array index out of bounds exception on FindDeadLocalStores #2994

Closed
miguel-vila opened this issue May 22, 2024 · 8 comments · Fixed by #3000
Closed

Array index out of bounds exception on FindDeadLocalStores #2994

miguel-vila opened this issue May 22, 2024 · 8 comments · Fixed by #3000

Comments

@miguel-vila
Copy link
Contributor

miguel-vila commented May 22, 2024

I'm getting this error when running spotbugs:

The following errors occurred during analysis:
  Exception analyzing BaseUrl using detector edu.umd.cs.findbugs.detect.FindDeadLocalStores
    java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
      At edu.umd.cs.findbugs.BugCounts.addError(PackageStats.java:50)
      At edu.umd.cs.findbugs.PackageStats.addError(PackageStats.java:240)
      At edu.umd.cs.findbugs.ProjectStats.addBug(ProjectStats.java:273)
      At edu.umd.cs.findbugs.SortedBugCollection.add(SortedBugCollection.java:960)
      At edu.umd.cs.findbugs.SortedBugCollection.add(SortedBugCollection.java:227)
      At edu.umd.cs.findbugs.BugCollectionBugReporter.doReportBug(BugCollectionBugReporter.java:110)
      At edu.umd.cs.findbugs.AbstractBugReporter.reportBug(AbstractBugReporter.java:179)
      At edu.umd.cs.findbugs.DelegatingBugReporter.reportBug(DelegatingBugReporter.java:69)
      At edu.umd.cs.findbugs.FilterBugReporter.reportBug(FilterBugReporter.java:49)
      At edu.umd.cs.findbugs.FilterBugReporter.reportBug(FilterBugReporter.java:49)
      At edu.umd.cs.findbugs.BugAccumulator.reportBug(BugAccumulator.java:199)
      At edu.umd.cs.findbugs.BugAccumulator.reportAccumulatedBugs(BugAccumulator.java:180)
      At edu.umd.cs.findbugs.detect.FindDeadLocalStores.analyzeMethod(FindDeadLocalStores.java:647)
      At edu.umd.cs.findbugs.detect.FindDeadLocalStores.visitClassContext(FindDeadLocalStores.java:198)
      At edu.umd.cs.findbugs.DetectorToDetector2Adapter.visitClass(DetectorToDetector2Adapter.java:76)
      At edu.umd.cs.findbugs.FindBugs2.lambda$analyzeApplication$1(FindBugs2.java:1108)
      At java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
      At edu.umd.cs.findbugs.CurrentThreadExecutorService.execute(CurrentThreadExecutorService.java:86)
      At java.base/java.util.concurrent.AbstractExecutorService.invokeAll(AbstractExecutorService.java:247)
      At edu.umd.cs.findbugs.FindBugs2.analyzeApplication(FindBugs2.java:1118)
      At edu.umd.cs.findbugs.FindBugs2.execute(FindBugs2.java:309)
      At com.siriusxm.sbt.library.SxmJavaSpotBugsPlugin$.$anonfun$projectSettings$5(SxmJavaSpotBugsPlugin.scala:71)
      At com.siriusxm.sbt.library.SxmJavaSpotBugsPlugin$.$anonfun$projectSettings$5$adapted(SxmJavaSpotBugsPlugin.scala:39)
      At scala.Function1.$anonfun$compose$1(Function1.scala:49)
      At sbt.internal.util.$tilde$greater.$anonfun$$u2219$1(TypeFunctions.scala:63)
      At sbt.std.Transform$$anon$4.work(Transform.scala:69)
      At sbt.Execute.$anonfun$submit$2(Execute.scala:283)
      At sbt.internal.util.ErrorHandling$.wideConvert(ErrorHandling.scala:24)
      At sbt.Execute.work(Execute.scala:292)
      At sbt.Execute.$anonfun$submit$1(Execute.scala:283)
      At sbt.ConcurrentRestrictions$$anon$4.$anonfun$submitValid$1(ConcurrentRestrictions.scala:265)
      At sbt.CompletionService$$anon$2.call(CompletionService.scala:65)
      At java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
      At java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
      At java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
      At java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
      At java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
      At java.base/java.lang.Thread.run(Thread.java:833)

the contents of the class are:

import io.vavr.control.Validation;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Optional;

public class BaseUrl {
  public final String value;

  private BaseUrl(String value) {
    this.value = value;
  }

  public static Validation<String, Optional<BaseUrl>> fromString(String value) {
    if (value.isEmpty()) {
      return Validation.valid(Optional.empty());
    } else {
      if (isValidUrl(value)) {
        return Validation.valid(Optional.of(new BaseUrl(value)));
      } else {
        return Validation.invalid(
            "baseUrl '"
                + value
                + "' doesn't match expected format (must be a valid URL with env/region variables)");
      }
    }
  }

  private static String replace(String value) {
    return value.replace("${region}", "region").replace("${env}", "env");
  }

  private static Boolean isValidUrl(String value) {
    try {
      new URL(replace(value)).toURI();
      return true;
    } catch (MalformedURLException e) {
      return false;
    } catch (URISyntaxException e) {
      return false;
    }
  }

  @Override
  public boolean equals(Object other) {
    if (this == other) {
      return true;
    }
    if (other == null || getClass() != other.getClass()) {
      return false;
    }
    BaseUrl baseUrl = (BaseUrl) other;
    return value.equals(baseUrl.value);
  }

  @Override
  public int hashCode() {
    return value.hashCode();
  }
}

I've tried reproducing it in a test (had to remove the io.vavr import) but it succeeds. I have other files in which the same error raises but they are a bit more complex, I could try minimizing them.

version 4.8.5

Copy link

welcome bot commented May 22, 2024

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.

@hazendaz
Copy link
Member

Which version of spotbugs are you using?

@miguel-vila
Copy link
Contributor Author

4.8.5

@miguel-vila
Copy link
Contributor Author

I enabled fdls.debug and got this:

    Analyzing method BaseUrl.isValidUrl
    Store at 39@20 is dead
Previous is:   19: areturn[176](1)
BaseUrl.java : isValidUrl
priority: 5
Reporting DLS: Dead store to local variable
{   A_LITTLE_BIT_LOWER_PRIORITY edu.umd.cs.findbugs.detect.DeadLocalStoreProperty.NO_LOADS      true
  FALSE_POSITIVE        edu.umd.cs.findbugs.detect.DeadLocalStoreProperty.EXCEPTION_HANDLER     true
  NO_ADJUSTMENT edu.umd.cs.findbugs.detect.DeadLocalStoreProperty.LOCAL_NAME    ?
}

    Store at 41@26 is dead
Previous is:   25: areturn[176](1)
BaseUrl.java : isValidUrl
priority: 5
Reporting DLS: Dead store to local variable
{   A_LITTLE_BIT_LOWER_PRIORITY edu.umd.cs.findbugs.detect.DeadLocalStoreProperty.NO_LOADS      true
  FALSE_POSITIVE        edu.umd.cs.findbugs.detect.DeadLocalStoreProperty.EXCEPTION_HANDLER     true
  NO_ADJUSTMENT edu.umd.cs.findbugs.detect.DeadLocalStoreProperty.LOCAL_NAME    ?
}

this is pointing to this line: new URL(replace(value)).toURI();. I tried reproducing this through a test but it seems no DLS_DEAD_LOCAL_STORE bug is being reported so maybe I'm not setting up the test correctly.

I got another class that are also throwing the same error, but the method that's failing looks something like:

public Deserializer(Format format) {
  super(Instant.class);
  this.format = format;
}

(this is a class constructor)

@gtoison
Copy link
Contributor

gtoison commented May 28, 2024

What's SxmJavaSpotBugsPlugin?
It looks to me that it is running SpotBugs and possibly tweaking the edu.umd.cs.findbugs.detect.DeadLocalStoreProperty.NO_LOADS property to A_LITTLE_BIT_LOWER_PRIORITY in some unexpected way

@miguel-vila
Copy link
Contributor Author

That's an internal SBT plugin that we developed in order to use spotbugs to check sbt modules. I don't think it's intentionally overriding anything. This is the source code: https://gist.github.com/miguel-vila/e96efe3af7eaa1f687104e8c8eaabd62

This is the setup code: https://gist.github.com/miguel-vila/e96efe3af7eaa1f687104e8c8eaabd62#file-sxmjavaspotbugsplugin-scala-L53-L75 which for the most part is just initializing things with defaults.

I'm running it with these parameters:

    spotbugsPriorityThreshold := PriorityThreshold.Normal,
    spotbugsRelaxed := true,

@gtoison
Copy link
Contributor

gtoison commented May 28, 2024

Thanks for clarifying, would you know why you're setting spotbugsRelaxed ?
I vaguely remember seeing similar issues with that and to be honest I'm not sure what it does

@gtoison
Copy link
Contributor

gtoison commented May 28, 2024

See #2269 and #943
These two issues seem to be the same problem

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

Successfully merging a pull request may close this issue.

3 participants