Skip to content

Harden the cmake Address Sanitizer detection code.#960

Open
pmqs wants to merge 6 commits intozlib-ng:developfrom
pmqs:sanitizer
Open

Harden the cmake Address Sanitizer detection code.#960
pmqs wants to merge 6 commits intozlib-ng:developfrom
pmqs:sanitizer

Conversation

@pmqs
Copy link
Contributor

@pmqs pmqs commented Feb 10, 2026

Currently the sanitizer detection code will silently ignore error cases -- like when given an unknown option or when it is asked for a valid sanitizer that isn't available for the platform. This one has stung me a few times.

This change makes it a fatal error to

  1. ask for an address sanitizer option that doesn't exist
  2. asks for a valid option that the current platform doesn't support.

One thng that dropped out with this change is the use of the Memory Sanitizer in build.yml, Turns out that it is not available on Ubuntu, so has been adding nothing to our test coverage. I think that may be a limitation of virtualisation. I've just disabled it for now.

This is what build.yml had when (not) detecting the memory Sanitizer:

-- Checking for Address Sanitizer Option
-- Performing Test HAVE_SANITIZER_memory
-- Performing Test HAVE_SANITIZER_memory - Failed
-- Memory sanitizer is not supported

@nmoinvaz
Copy link
Member

The sanitizer CMake is too borrowed from zlib-ng, so I'm uncomfortable changing it.

CMakeLists.txt Outdated
endif()
if(NOT MZ_STATUS)
set(MZ_SANITIZER_ENABLED OFF)
message(FATAL_ERROR "Cannot enable the Address Sanitizer '${MZ_SANITIZER}' option")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just log "Sanitizer" without "Address", as it can be any of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

CMakeLists.txt Outdated
endif()
elseif(MZ_SANITIZER_LOWER STREQUAL "none")
set(MZ_SANITIZER_ENABLED OFF)
message(STATUS " Address Sanitizer not enabled")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just log "Sanitizer" without "Address", as it can be any of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

CMakeLists.txt Outdated
add_feature_info(MZ_BUILD_UNIT_TESTS MZ_BUILD_UNIT_TESTS "Builds minizip unit test project")
add_feature_info(MZ_BUILD_FUZZ_TESTS MZ_BUILD_FUZZ_TESTS "Builds minizip fuzzer executables")
add_feature_info(MZ_CODE_COVERAGE MZ_CODE_COVERAGE "Builds with code coverage flags")
add_feature_info(MZ_SANITIZER MZ_SANITIZER_ENABLED "Builds with Address Sanitizer")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just log "Sanitizer Support" instead of "Address Sanitizer", as it can be any of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@Coeur
Copy link
Collaborator

Coeur commented Feb 14, 2026

The sanitizer CMake is too borrowed from zlib-ng, so I'm uncomfortable changing it.

The change looks good to me.

@pmqs can you also offer a PR for the zlib-ng repository eventually?

@pmqs
Copy link
Contributor Author

pmqs commented Feb 14, 2026

The sanitizer CMake is too borrowed from zlib-ng, so I'm uncomfortable changing it.

The change looks good to me.

@pmqs can you also offer a PR for the zlib-ng repository eventually?

@Coeur, as it happens I'm ahead of you. Already looking at the equivalent change in zlib-ng because there is a knock-on impact with it for Sanitizer support with the Microsoft C compiler. Will write up the details in the zlib-ng PR.

I'll park this change for now & get the same fix done in the much simpler zlib-ng use case sorted first.

add_thread_sanitizer()
elseif(MZ_SANITIZER STREQUAL "Undefined")
add_undefined_sanitizer()
message(STATUS "Checking for Address Sanitizer Option")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe remove "Address" here?

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.

3 participants