Skip to content

Fix undefined behavior in callback functions' parameter passing #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mrannanj
Copy link

@mrannanj mrannanj commented Feb 8, 2025

There is undefined behavior in how the callback functions arg_resetfn, arg_scanfn, arg_checkfn, and arg_errorfn are used currently. The original type of those functions has the parent type as void*, but the actual function implementations have the void* parent as something like struct arg_date* parent, but they are cast to the function type with void* parent. This is apparently undefined behavior, and the clang undefined behavior sanitizer catches this.

So, to see the errors, use this change to CMakeLists.txt:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 67cee27..f267a19 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -69,6 +69,9 @@ if(ARGTABLE3_ENABLE_CONAN AND EXISTS "${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath-link,${LINK_FLAGS}")
 endif()
 
+add_compile_options("-fsanitize=undefined")
+add_link_options("-fsanitize=undefined")
+
 set(ARGTABLE3_AMALGAMATION_SRC_FILE ${PROJECT_SOURCE_DIR}/dist/argtable3.c)
 set(ARGTABLE3_SRC_FILES
   ${PROJECT_SOURCE_DIR}/src/arg_cmd.c

The errors will looks like:

./tests/test_amalgamation 
/home/martti/gh/argtable3/dist/argtable3.c:5374:13: runtime error: call to function arg_lit_resetfn through pointer to incorrect function type 'void (*)(void *)'
/home/martti/gh/argtable3/dist/argtable3.c:3378: note: arg_lit_resetfn defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /home/martti/gh/argtable3/dist/argtable3.c:5374:13 
/home/martti/gh/argtable3/dist/argtable3.c:5227:37: runtime error: call to function arg_lit_scanfn through pointer to incorrect function type 'int (*)(void *, const char *)'
/home/martti/gh/argtable3/dist/argtable3.c:3383: note: arg_lit_scanfn defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /home/martti/gh/argtable3/dist/argtable3.c:5227:37 
/home/martti/gh/argtable3/dist/argtable3.c:5361:29: runtime error: call to function arg_lit_checkfn through pointer to incorrect function type 'int (*)(void *)'
/home/martti/gh/argtable3/dist/argtable3.c:3394: note: arg_lit_checkfn defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /home/martti/gh/argtable3/dist/argtable3.c:5361:29 
/home/martti/gh/argtable3/dist/argtable3.c:5270:41: runtime error: call to function arg_lit_scanfn through pointer to incorrect function type 'int (*)(void *, const char *)'
/home/martti/gh/argtable3/dist/argtable3.c:3383: note: arg_lit_scanfn defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /home/martti/gh/argtable3/dist/argtable3.c:5270:41 
/home/martti/gh/argtable3/dist/argtable3.c:5319:21: runtime error: call to function arg_int_scanfn through pointer to incorrect function type 'int (*)(void *, const char *)'
/home/martti/gh/argtable3/dist/argtable3.c:3187: note: arg_int_scanfn defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /home/martti/gh/argtable3/dist/argtable3.c:5319:21 

@mrannanj mrannanj changed the title Fix callback functions to use void* as the parent parameter Fix undefined behavior in callback functions' parameter passing Feb 10, 2025
@tomghuang
Copy link
Contributor

@mrannanj : Really appreciate that you found this function prototype mismatch issue. I'm going to merge it. However, I just found that the actions/upload-artifact@v3 has been deprecated and we need to upgrade to actions/upload-artifact@v4 in the GitHub workflow. This is why the CI checks failed.

I've merged a patch to fix this GitHub action issue on the master branch. Could you please rebase your feature branch on the latest master and then push it again? Otherwise, I don't know how to make this pull request pass the CI checks. Thanks.

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