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

Use of #pragma pack breaks standard library functionality #86

Closed
br-privacore opened this issue Mar 30, 2016 · 1 comment
Closed

Use of #pragma pack breaks standard library functionality #86

br-privacore opened this issue Mar 30, 2016 · 1 comment

Comments

@br-privacore
Copy link

In types.h you use various "#pragma pack" statements, but unfortunately you do not use push and pop, so the last #pragma pack(4) is enabled for other files included after types.h. This is obviously very dangerous if you are not aware of it.

In Hostdb.cc you include gb-include.h, which includes types.h. Further down the code you include sys/types.h and ifaddrs.h. The strucs in these files are impacted by the open #pragma pack(4) :-(

The ugly hack to "swap ints" in the getLocalIps function is a direct result of a problem caused by this open #pragma pack. It modifies the layout of the ifaddrs struct, causing you to use a misaligned address pointer.

Move the include of the two system files up above the gb-include.h file, and you can remove the swapInts function and have the ifa_addr work as expected.

The correct fix of types.h (and zlib.h) would be to use #pragma pack(push, 4) and #pragma pack(pop), but who knows what that will break now that #pragma pack(4) has been "standard" in all files.

Try this code:
printf("offsetof struct ifaddrs.ifa_next: %zu\n", offsetof(struct ifaddrs, ifa_next));
printf("offsetof struct ifaddrs.ifa_name: %zu\n", offsetof(struct ifaddrs, ifa_name));
printf("offsetof struct ifaddrs.ifa_flags: %zu\n", offsetof(struct ifaddrs, ifa_flags));
printf("offsetof struct ifaddrs.ifa_addr: %zu\n", offsetof(struct ifaddrs, ifa_addr));
printf("offsetof struct ifaddrs.ifa_netmask: %zu\n", offsetof(struct ifaddrs, ifa_netmask));

With no types.h included:
offsetof struct ifaddrs.ifa_next: 0
offsetof struct ifaddrs.ifa_name: 8
offsetof struct ifaddrs.ifa_flags: 16
offsetof struct ifaddrs.ifa_addr: 24
offsetof struct ifaddrs.ifa_netmask: 32

With types.h included (or just #pragma pack(4) specified):
offsetof struct ifaddrs.ifa_next: 0
offsetof struct ifaddrs.ifa_name: 8
offsetof struct ifaddrs.ifa_flags: 16
offsetof struct ifaddrs.ifa_addr: 20
offsetof struct ifaddrs.ifa_netmask: 28

Notice the ifa_addr offset ...

Partial fix in our fork: privacore@f360546

@isj-privacore
Copy link
Contributor

Solved in our fork in commit privacore@e4323f3
but for that you also need: privacore@3f8e094
privacore@1f63050
privacore@ce5a6cf
privacore@ef3b8a3
privacore@e57fb2f
privacore@b825365
privacore@f6efc87
privacore@7f6b3e4
privacore@8072189

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

No branches or pull requests

2 participants