protocolbuffers / protobuf

Protocol Buffers - Google's data interchange format
http://protobuf.dev
Other
65.75k stars 15.51k forks source link

StringValue.of compiles when passed a null value #18672

Open rooday-doordash opened 1 month ago

rooday-doordash commented 1 month ago

Related to https://github.com/detekt/detekt/issues/7707

What version of protobuf and what language are you using? Version: protobuf-java:3.25.3 Language: Kotlin

What operating system (Linux, Windows, ...) and version? Mac OS X 14.7 aarch64

What runtime / compiler are you using (e.g., python version or gcc version) Gradle 8.8, Kotlin 1.6.10

What did you do? Steps to reproduce the behavior:

  1. Write code that uses StringValue.of(null)
  2. Compile (this will be successful)
  3. Run and get an NPE

More specifically, I wrote Kotlin code like this:

SomeProtobufBuilder.apply {
    nullableString.let {
        this.stringProperty = StringValue.of(it)
    }
}

This code is buggy, as it allows StringValue.of(nullableString) even when it is null. It should've been:

SomeProtobufBuilder.apply {
    nullableString?.let {
        this.stringProperty = StringValue.of(it)
    }
}

However, neither IntelliJ nor Detekt flagged this during static analysis. Detekt says this isn't an issue on their end if the code compiles properly, so I'm guess there's an issue with how StringValue.of (and potentially all wrapper classes .of) type check their passed arguments.

When I hover over .of in IntelliJ, I see

@NotNull     
@Contract("_->new")     
public static StringValue of(
    @NotNull String value
)

It says it takes a @NotNull String, yet it didn't throw a compiler error when a nullable string was passed.

What did you expect to see I expected to get a compiler error.

What did you see instead? The error was hidden until runtime.

Make sure you include information that can help us debug (full error message, exception listing, stack trace, logs).

Anything else we should know about your project / environment Potentially this is an issue of Kotlin <-> Java interop?

rooday-doordash commented 1 day ago

Hi @googleberg, just wanted to check if there are any updates here. Thanks!