SCOREC / core

parallel finite element unstructured meshes
Other
179 stars 63 forks source link

Compile Error on Clang #356

Closed JaredCrean2 closed 2 years ago

JaredCrean2 commented 2 years ago

Got this error:

/home/jared/build/core/gmi/gmi_base.c:45:11: error: performing pointer subtraction with a null pointer has undefined behavior [-Werror,-Wnull-pointer-subtraction]
  uid = p - ((char*)0);
          ^ ~~~~~~~~~~
1 error generated.

with

jared@jared-desktop:~/build/core/build$ mpicxx --version
clang version 14.0.0 (https://github.com/llvm/llvm-project.git 4c1e1e05cb901a2ed9055e5d6ac6ce60b826a288)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/jared/build/llvm//bin
cwsmith commented 2 years ago

Hi @JaredCrean2. Thanks for reporting this.

I have not used/seen null pointer subtraction before and need to look into this more. From my initial scan of some stack overflow comments it seems like it is used to check for type alignment.... I'm not sure how that applies here though.

cwsmith commented 2 years ago

It looks like clang 13 should also produce this warning: https://releases.llvm.org/13.0.0/tools/clang/docs/ReleaseNotes.html#new-compiler-flags

cwsmith commented 2 years ago

Looking at the code again:

https://github.com/SCOREC/core/blob/8a38feae942ab5a46dc3f9fa393a68884ddddad6/gmi/gmi_base.c#L39-L46

it appears that the purpose of the NULL subtraction is to recover an integer for the geometric model entity id. Simply casting produces a type size warning:

https://godbolt.org/z/EhGz1aMe5

edit: casting to an intptr_t appears to work... https://godbolt.org/z/sdsjEPzr8

cwsmith commented 2 years ago

@JaredCrean2 Can you try building with the gmiNullPtr branch? If that works for you I'll merge this into develop. Note, I tested with gcc 10.1.0 on a 64b system.

There are two commits to that branch:

JaredCrean2 commented 2 years ago

Builds with no warnings or errors.

cwsmith commented 2 years ago

Thank you. The branch was merged into develop: https://github.com/SCOREC/core/commit/d1caa0b420bface29e2d111c4226b8099d2e628d .