protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Coverity static analysis warning seen on latest (v30.2) message_lite.h

Open kmurtty opened this issue 7 months ago • 10 comments

What version of protobuf and what language are you using? Version: 6.30.2 Language: C++

What operating system (Linux, Windows, ...) and version? Linux, v5.15.72, aarch64

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

What did you do?

Ran Coverity static analysis of our software.

What did you expect to see

A clean Coverity run without any errors.

What did you see instead?

Coverity Static Analysis tool is complaining about the latest file “google/protobuf/message_lite.h” file. In particular, the syntax checker is complaining about the following lines of code in “message_lite.h”. The error message is saying that line number 284 is using an incomplete type.

Question: Can we safely ignore this error message? Any potential that this segment of code could lead to invalid code generation?

Following 7 lines are from “google/protobuf/message_lite.h”:

281 struct EnumTraitsImpl { 282 struct Undefined; 283 template <typename T> 284 static Undefined value; 285 }; 286 template <typename T> 287 using EnumTraits = decltype(EnumTraitsImpl::value<T>);

The full COVERITY warning message from our tool is as follows:

Type: Parse recovery warning (RW.INCOMPLETE_VAR_TYPE) Classification: Unclassified Severity: Unspecified Action: Undecided Owner: apotjenkins Defect only exists locally. /localdata/build/tmp/work/armv8a-linux/app-service/999-r0/recipe-sysroot/usr/include/google/protobuf/message_lite.h:284:3:

  1. incomplete_var_type: variable cannot have incomplete type "google::protobuf::internal::EnumTraitsImpl::Undefined"

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

We are using gRPC Library version 1.71.0.

kmurtty avatar Apr 30 '25 14:04 kmurtty

Yes, this is safe to ignore. I'm not surprised that Coverity would complain about it because it is an unusual construction, but it's harmless.

acozzette avatar Apr 30 '25 16:04 acozzette

This issue causes the nvcc compiler to return an error.

Simplified example:

file: example.cpp

struct EnumTraitsImpl {
    struct Undefined;
    template<typename T>
    static Undefined value;
};

template<typename T>
using EnumTraits = decltype(EnumTraitsImpl::value<T>);

int main() {
    EnumTraits<int> a;
}

Compiled with nvcc:

$ nvcc example.cu -arch=sm_86
example.cu(4): error: variable cannot have incomplete type "EnumTraitsImpl::Undefined"
      static Undefined value;
      ^

example.cu(11): error: incomplete type "EnumTraits<int>" (aka "EnumTraitsImpl::Undefined") is not allowed
      EnumTraits<int> a;
                      ^

Compiled with gcc:

$gcc example.cpp
example.cpp: In function 'int main()':
example.cpp:11:21: error: aggregate 'EnumTraits<int> a' has incomplete type and cannot be defined
   11 |     EnumTraits<int> a;
      |                     ^

Versions

$nvcc --version 
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2025 NVIDIA Corporation
Built on Fri_Feb_21_20:23:50_PST_2025
Cuda compilation tools, release 12.8, V12.8.93
Build cuda_12.8.r12.8/compiler.35583870_0


$gcc --version
gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Expected That both compiler to fail with the same error.

I ran into this problem when I was trying to bump Protobuf from version 28.3 to 30.2

Almskou avatar May 05 '25 09:05 Almskou

That simplified example shows a problem elsewhere. EnumTraits<int> is wrong because the trait is not defined for int. It is only defined for proto enums. If you see it as undefined, maybe you are missing the #include for the particular header that defines the enum?

sbenzaquen avatar May 05 '25 14:05 sbenzaquen

I know the usage of EnumTraits<int> is wrong, but I used it to provoke the error: variable cannot have incomplete type "EnumTraitsImpl::Undefined" when compiling with nvcc.

The implementation of EnumTraitsImpl is the same as here: https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/message_lite.h#L284

The error message I get from my project where I want to bump protobuf from 28.3 to 30.2 or newer.

bazel-out/k8-fastbuild/bin/external/protobuf~/src/google/protobuf/_virtual_includes/protobuf_lite/google/protobuf/message_lite.h(284): error: variable cannot have incomplete type "google::protobuf::internal::EnumTraitsImpl::Undefined"
    static Undefined value;
    ^

This error does not come when I use protobuf 28.3 in my project.

Both compiled with nvcc

Almskou avatar May 06 '25 04:05 Almskou

@Almskou, I get the same error when trying to compile tensorflow with latest protobuf. Did you find any workaround to the issue?

@acozzette, the issue is that nvcc chokes with the error and can't ignore the otherwise harmless warning.

isuruf avatar Aug 11 '25 18:08 isuruf

I know the usage of EnumTraits is wrong, but I used it to provoke the error: variable cannot have incomplete type "EnumTraitsImpl::Undefined" when compiling with nvcc.

Instantiating EnumTraits<int> will fail to compile. That is the whole purpose of that incomplete type. EnumTraits<T> should only compile for protobuf enums.

@Almskou, I get the same error when trying to compile tensorflow with latest protobuf. Did you find any workaround to the issue?

Does this happen on compiling the header? Or during an instantiation of the template? If so, which template? Can you share a more detailed compiler error?

sbenzaquen avatar Aug 12 '25 16:08 sbenzaquen

If this is happening directly on the header when parsing the template, can you try something?

Replace the declaration of EnumTraitsImpl with

struct EnumTraitsImpl {
  template <typename T>
  static std::enable_if_t<sizeof(T) != 0> value;
};

which should provide similar compile time diagnostics but might workaround the eager compiler error.

sbenzaquen avatar Aug 12 '25 16:08 sbenzaquen

It fails with

<PREFIX>/include/google/protobuf/message_lite.h(287): error: variable cannot have incomplete type "google::protobuf::internal::EnumTraitsImpl::Undefined"
     static Undefined value;

Sorry I don't have a detailed error yet. Will try to get one.

Full log

Let me try your example and get back to you.

isuruf avatar Aug 12 '25 16:08 isuruf

I tried your suggestion and I get an error of the form

<PREFIX>include/absl/container/internal/raw_hash_map.h:126:189: error: using template type parameter 'absl::lts_20250512::container_internal::IfRRef<const int&>::AddPtr<K>' after 'typename'
  126 |   ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, const &, const &,
      |                                                                                                                                                                                             ^         
<PREFIX>/include/absl/container/internal/raw_hash_map.h:126:263: error: using template type parameter 'absl::lts_20250512::container_internal::IfRRef<const int&>::AddPtr<V>' after 'typename'
  126 |   ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, const &, const &,
      |                                                                                                                                                                                                                                                                       ^         
<PREFIX>/include/absl/container/internal/raw_hash_map.h:126:274: error: template argument 5 is invalid
  126 |   ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, const &, const &,
      |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

I'm not sure if that's because of the change or something else.

isuruf avatar Aug 12 '25 17:08 isuruf

@sbenzaquen, yes this works. The absl error I mentioned is unrelated. (Bug in nvcc 12.6)

isuruf avatar Oct 13 '25 19:10 isuruf