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

Current usage of intptr_t prevents API mishaps to be detected #96

Open
jengelh opened this issue Apr 13, 2021 · 3 comments
Open

Current usage of intptr_t prevents API mishaps to be detected #96

jengelh opened this issue Apr 13, 2021 · 3 comments
Assignees

Comments

@jengelh
Copy link

jengelh commented Apr 13, 2021

The definition of libpff_item_t is compatible to libpff_record_set_t (and possibly others). This is very unfortunate, because it masks developer errors, for example when a logically different-typed thing is passed:

#include <libpff.h>
void f(libpff_record_set_t *x) {}
int main()
{
        libpff_item_t *x = NULL;
        f(x);
}

Observed:

» gcc x.c -Wall
»

Expected to see:

» gcc x.c -Wall
x.c: In function ‘main’:
x.c:11:4: warning: passing argument 1 of ‘f’ from incompatible pointer type [-Wincompatible-pointer-types]
   11 |  f(x);
x.c:7:29: note: expected ‘libpff_record_set_t *’ but argument is of type ‘libpff_item_t *’
@joachimmetz
Copy link
Member

joachimmetz commented Apr 13, 2021

@jengelh what do you propose here as the solution?

I've ruled out

  • public structs, these can break at the ABI and resolve in hard to debug issues
  • public structs as proxy pointers, these are not supported by every compiler

@jengelh
Copy link
Author

jengelh commented Apr 13, 2021

Off the top of my head, two options. A forward declaration of an incomplete struct is common:

struct libpff_file;
extern int file_get_item(struct libpff_file *x, int itmidx);

Alternatively, one could wrap intptr into a struct. Since both have the same size, passing the entire struct as an argument shouldn't incur any extra cost.

struct libpff_file {
    uintptr_t realptr; // at this point one could also just use void *.
};
extern int file_get_item(struct libpff_file x, int itmidx);

which is just a more fancy way of the first.

@joachimmetz
Copy link
Member

thx for the suggestions I'll take a closer look at these

@joachimmetz joachimmetz self-assigned this Apr 13, 2021
@joachimmetz joachimmetz changed the title API accidents are not sufficiently prevented Current usage of intptr_t prevents API mishaps to be detected Apr 14, 2021
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