Skip to content

build: avoid ODR violation from different struct layout per TU#326

Merged
vincent-richard merged 1 commit intokisli:masterfrom
jengelh:odr
Jul 21, 2025
Merged

build: avoid ODR violation from different struct layout per TU#326
vincent-richard merged 1 commit intokisli:masterfrom
jengelh:odr

Conversation

@jengelh
Copy link
Contributor

@jengelh jengelh commented Jul 8, 2025

When building with ASAN, the linker warns about struct declarations being different between TUs, which is undefined behavior.

[  1%] Linking CXX shared library build/lib/libwmime.so
/usr/bin/cmake -E cmake_link_script CMakeFiles/wmime.dir/link.txt --verbose=1
/usr/bin/c++ -fPIC -D_REENTRANT=1 -W -Wall -pedantic -Wpointer-arith
-Wold-style-cast -Wconversion -Wcast-align -Wno-long-long -fvisibility=hidden
-fvisibility-inlines-hidden -O2 -Wall -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3
-fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables
-fstack-clash-protection -Werror=return-type -flto=auto -g -O0 -ggdb3
-fsanitize=address,undefined -lasan -lubsan
-Wl,--dependency-file=CMakeFiles/wmime.dir/link.d -flto=auto -Wl,--as-needed
-Wl,--no-undefined -Wl,-z,now -lasan -lubsan -shared -Wl,-soname,libwmime.so.2
-o build/lib/libwmime.so.2.0.0 CMakeFiles/wmime.dir/src/vmime/address.cpp.o...

~/src/vmime/security/sasl/SASLContext.hpp:47:20:
warning: type ‘struct SASLContext’ violates the C++ One Definition Rule [-Wodr]
   47 | class VMIME_EXPORT SASLContext : public object, public enable_shared_from_this <SASLContext> {
~/src/vmime/security/sasl/SASLContext.hpp:47:20: note: a different type is defined in another translation unit
   47 | class VMIME_EXPORT SASLContext : public object, public enable_shared_from_this <SASLContext> {
~/src/vmime/security/sasl/SASLContext.hpp:123:15: note: the first difference of corresponding definitions is field ‘m_gsaslContext’
  123 |         void* m_gsaslContext;
~/src/vmime/security/sasl/SASLContext.hpp:121:16: note: a field of same name but different type is defined in another translation unit
  121 |         Gsasl* m_gsaslContext;

When building with ASAN, the linker warns about struct declarations being different
between TUs, which is undefined behavior.

```
[  1%] Linking CXX shared library build/lib/libwmime.so
/usr/bin/cmake -E cmake_link_script CMakeFiles/wmime.dir/link.txt --verbose=1
/usr/bin/c++ -fPIC -D_REENTRANT=1 -W -Wall -pedantic -Wpointer-arith
-Wold-style-cast -Wconversion -Wcast-align -Wno-long-long -fvisibility=hidden
-fvisibility-inlines-hidden -O2 -Wall -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3
-fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables
-fstack-clash-protection -Werror=return-type -flto=auto -g -O0 -ggdb3
-fsanitize=address,undefined -lasan -lubsan
-Wl,--dependency-file=CMakeFiles/wmime.dir/link.d -flto=auto -Wl,--as-needed
-Wl,--no-undefined -Wl,-z,now -lasan -lubsan -shared -Wl,-soname,libwmime.so.2
-o build/lib/libwmime.so.2.0.0 CMakeFiles/wmime.dir/src/vmime/address.cpp.o...

~/src/vmime/security/sasl/SASLContext.hpp:47:20:
warning: type ‘struct SASLContext’ violates the C++ One Definition Rule [-Wodr]
   47 | class VMIME_EXPORT SASLContext : public object, public enable_shared_from_this <SASLContext> {
~/src/vmime/security/sasl/SASLContext.hpp:47:20: note: a different type is defined in another translation unit
   47 | class VMIME_EXPORT SASLContext : public object, public enable_shared_from_this <SASLContext> {
~/src/vmime/security/sasl/SASLContext.hpp:123:15: note: the first difference of corresponding definitions is field ‘m_gsaslContext’
  123 |         void* m_gsaslContext;
~/src/vmime/security/sasl/SASLContext.hpp:121:16: note: a field of same name but different type is defined in another translation unit
  121 |         Gsasl* m_gsaslContext;
```
@vincent-richard vincent-richard merged commit 7046a43 into kisli:master Jul 21, 2025
1 check passed
@jengelh jengelh deleted the odr branch July 21, 2025 10:30
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 this pull request may close these issues.

2 participants