Skip to content

Fix undefined behavior from incompatible function pointer casts in ref_count destroy callbacks#798

Open
bjosv wants to merge 1 commit intoawslabs:mainfrom
Nordix:ubsan-fixes
Open

Fix undefined behavior from incompatible function pointer casts in ref_count destroy callbacks#798
bjosv wants to merge 1 commit intoawslabs:mainfrom
Nordix:ubsan-fixes

Conversation

@bjosv
Copy link
Copy Markdown
Contributor

@bjosv bjosv commented Mar 26, 2026

This PR fixes UBSan failures caused by calling functions through incompatible pointer types.
aws_ref_count_init expects a callback with signature void (*)(void *) but we were casting typed destroy functions (e.g., void (*)(struct aws_async_input_stream *)) to aws_simple_completion_callback *.
This is undefined behavior per the C standard, and UBSan rightfully flags it.

Fixes

  • async_stream.c: Add a wrapper function that casts and forwards to the vtable destroy, instead of casting the vtable destroy directly.
  • pkcs11_tls_op_handler.c: Change s_aws_custom_key_op_handler_destroy to accept void * and cast internally, removing the incompatible function pointer cast at the call site.

How to reproduce
Build as described in https://github.com/awslabs/aws-c-s3?tab=readme-ov-file#building but enable UBSan via
-DCMAKE_C_FLAGS="-fsanitize=undefined -fno-sanitize-recover=all", then run S3 tests:
cd build && ctest --output-on-failure

UBSan will report errors like:
runtime error: call to function s_aws_custom_key_op_handler_destroy through pointer to incorrect function type 'void (*)(void *)'

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Change destroy callbacks passed to aws_ref_count_init to match the
expected aws_simple_completion_callback (void*) signature instead of
casting typed function pointers. Add a wrapper for async_input_stream
and fix pkcs11_tls_op_handler's destroy to accept void* directly.

Signed-off-by: Björn Svensson <bjorn.a.svensson@est.tech>
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.

1 participant