4

The Coverity Scan Build Tool fails to compile any C file that includes <stdlib.h> on Ubuntu 18.04 when _GNU_SOURCE is defined:

$ cat > main.c
#include <stdlib.h>
int main() {
}
$ 
$ gcc -D_GNU_SOURCE=1 -o main main.c 
$ 
$ /opt/cov-analysis/bin/cov-build --dir cov-int gcc -D_GNU_SOURCE=1 -o main main.c 
Coverity Build Capture (64-bit) version 2017.07 on Linux 4.15.0-20-generic x86_64
...
[WARNING] Emitted 0 C/C++ compilation units (0%) successfully
...
$ 

The same build works perfectly on Ubuntu 16.04 or without _GNU_SOURCE defined:

$ /volatile/local/cov-analysis/bin/cov-build --dir cov-int gcc -o main main.c 
Coverity Build Capture (64-bit) version 2017.07 on Linux 4.15.0-20-generic x86_64
...
Emitted 1 C/C++ compilation units (100%) successfully
...
$ 

How to get Coverity Scan to build C sources with _GNU_SOURCEdefined on Ubuntu 18.04?

For those interested file cov-int/build-log.txt can be found here:
https://gist.github.com/DimitriPapadopoulos/0dcd9018eed26401cc6095087d9cc1d5

Dimitri
  • 161
  • 1
  • 3
  • 1
    Apparently, Coverity needs to be taught about `_Float32`, `_Float32x`, `_Float64`, `_Float64x`, and `_Float128` types. Before that happens, you can paper over the issues by defining them as macros: add `-D_Float32=float -D_Float64=double -D_Float32x=double -D_Float64x="long double" -D_Float128="long double"` to the gcc command line. Do note that none of the `_Float128`-using functions will work with this hack, though. – Nominal Animal May 20 '18 at 13:19
  • 1
    Also, if you do a web search on `Coverity`, `_Float128`, `_Float32`, `_Float64`, `_Float32x`, `_Float64x`, you will ll find [this page](https://www.mail-archive.com/sssd-devel@lists.fedorahosted.org/msg37312.html), where the suggested "fix" is basically the same as my above macro suggestion, just in an external header file to be included before any other files. – Nominal Animal May 20 '18 at 13:30
  • Defining these types as macros doesn't work because of `/include/x86_64-linux-gnu/bits/floatn.h` which contains `typedef double _Float64;` but thanks for the general idea and the link to the `sssd` developers' list. – Dimitri May 20 '18 at 14:39
  • I tried getting rid of `_GNU_SOURCE` but unfortunately it is required by calls to *memmem()* and *strcasestr()* in our code base. – Dimitri May 20 '18 at 14:54

2 Answers2

2

After contacting Coverity support, it appears this is known bug. They suggested I work around it by switching from the default Ubuntu 18.04 compiler (GCC 7) to the previous version (GCC 6):

sudo apt install gcc-6

Indeed _Float32, _Float32x, _Float64, _Float64x and _Float128 were introduced in GCC 7.

Dimitri
  • 161
  • 1
  • 3
2

Coverity is failing to define the types GCC would define, but then it's claiming to be GCC anyway. Here's a workaround: https://gist.github.com/vathpela/0cede6d6eb5b0ec0791c6afc4282c340#file-fix_coverity-h

Just be sure you do:

#include "fix_coverity.h"

before stdlib.h gets included, whether directly or indirectly.

Adrian W
  • 4,563
  • 11
  • 38
  • 52
vathpela
  • 21
  • 1