Skip to content

Fix #152 crash on Apple M1 by casting 0 to (OBJECT *) explicitly. #214

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

Merged
merged 1 commit into from
Feb 21, 2023
Merged

Fix #152 crash on Apple M1 by casting 0 to (OBJECT *) explicitly. #214

merged 1 commit into from
Feb 21, 2023

Conversation

biergaizi
Copy link
Contributor

Currently, when the NULL-terminated variadic function call_rule() is invoked, the value 0 is passed as the last argument to act as a terminator. However, this is an integer value, which is incompatible with the pointer data type expected by call_rule().

This is undefined behavior in C, correct operation is not guaranteed. In fact, it causes b2 to crash on Apple M1 when GCC is used - the loop is not terminated when it should, instead, it keeps running, creating the following error:

lol_add failed due to reached limit of 19 elements

In some cases, it can even corrupt the internal state of the program, creating an infinite loop.

This commit fixes the problem (Issue #152) by explicitly casting the value 0 to the correct pointer type (OBJECT *). According to Rich Felker, the primary author of musl libc, type casting from 0 is technically safer than using the value NULL.

P.S: I'm not sure which is the correct branch to merge, I selected 4.9.3 only as a placeholder.

Currently, when the NULL-terminated variadic function call_rule()
is invoked, the value "0" is passed as the last argument to act
as a terminator. However, this is an integer value, which is
incompatible with the pointer data type expected by call_rule().

This is undefined behavior in C, correct operation is not
guaranteed. In fact, it causes b2 to crash on Apple M1 when GCC
is used - the loop is not terminated when it should, instead, it
keeps running, creating the following error:

> lol_add failed due to reached limit of 19 elements

In some cases, it can even corrupt the internal state of the program,
creating an infinite loop.

This commit fixes the problem by explicitly casting the value 0 to
the correct pointer type (OBJECT *).

Signed-off-by: Yifeng Li <[email protected]>
@grafikrobot
Copy link
Member

This is great. Thanks for finding this!

P.S: I'm not sure which is the correct branch to merge, I selected 4.9.3 only as a placeholder.

You should do it against the version/4.9.4 branch. I'll be releasing that one in the next couple of days.

@biergaizi
Copy link
Contributor Author

biergaizi commented Feb 20, 2023

I've changed the base branch to v4.9.4. Please take a look and let me know if it can be merged.

@grafikrobot grafikrobot merged commit 7a36e13 into bfgroup:version/4.9.4 Feb 21, 2023
@rektbuildr
Copy link

Thank you for this. Spent 48+ hours trying to build libbitcoin using gcc on a Mac until I found your patch

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