Skip to content
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

Mask out all but low byte when checking boolean #1568

Closed
wants to merge 1 commit into from

Conversation

sebbASF
Copy link

@sebbASF sebbASF commented Nov 26, 2023

On macOS (M1) at least, a boolean return type is only guaranteed to be 0 or non-zero in the low-order byte.

This is handled correctly in the case of a method called via Native.register, but the NativeLibrary.getFunction nearly always gives true as the upper bytes are random

@matthiasblaesing
Copy link
Member

matthiasblaesing commented Nov 26, 2023

How does the typedef for boolean looks in this case? JNA definition of boolean comes from windows where it is typedeffed from int. That is obviously not true in your case and I suspect you are looking at a "native boolean" and that is not what is covered by default by JNA. See discussion here: #1076. You can determine which case you see by adding a printf("sizeof(bool): %ld\n", sizeof(bool));. If that reports 1, you are using the wrong mapping.

A already mentioned in #1560 the author information of the commit is incomplete and needs to be fixed before considered for merge. The same is true for #1561.

@sebbASF
Copy link
Author

sebbASF commented Nov 26, 2023

AIUI bool is a valid type in C99, and is certainly used on macOS, for example:

https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlopen_preflight.3.html

As to boolean not being a direct mapping for bool, it works OK when using Native register, but not when using Function#invoke(boolean.class,...)
This is inconsistent.

As to the author information, I don't intend to change it further.
Feel free to treat the commit as your own; I don't want attribution.

@dbwiddis
Copy link
Contributor

You can determine which case you see by adding a printf("sizeof(bool): %ld\n", sizeof(bool));. If that reports 1, you are using the wrong mapping.

For the record, I ran this on a Mac M2 and got

Size of bool: 1 bytes

Since the submitter has elected not to attribute their work, I'm closing this PR.

@dbwiddis dbwiddis closed this Nov 27, 2023
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