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

Possible to create an uninitialized stat? #9

Closed
bscan opened this issue Nov 30, 2023 · 4 comments
Closed

Possible to create an uninitialized stat? #9

bscan opened this issue Nov 30, 2023 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@bscan
Copy link

bscan commented Nov 30, 2023

Hi, thanks for all your work on creating the FFI:: set of modules. I was wondering if it's possible to create an uninitialized or even zero-initialized FFI::C::Stat object. Specifically, I'm trying to use smbc_stat (from libsmbclient.h) which looks exactly like the examples in your documentation, where I have a function that requires a pointer to a stat as an input. However, FFI::C::Stat seems to always perform a variant of stat, or require an existing stat.

C function definition:

/**
 * @param st        pointer to a buffer that will be filled with
 *                  standard Unix struct stat information.
*/
int smbc_stat(const char *url, struct stat *st);

The following Perl code successfully runs and performs the stat, but something seems odd about my explicit use of malloc and the hardcoded number of bytes. Although it works, I'm wondering if I am using this module incorrectly. I'm certainly no FFI expert.

...

$ffi->type('object(FFI::C::Stat)' => 'stat');
$ffi->attach( 'smbc_stat' => ['string', 'stat'] => 'int' );

...

my $ptr = malloc(144); # Number of bytes found in C with:  printf("Size of struct stat: %lu bytes\n", sizeof(struct stat));
my $stat = FFI::C::Stat->clone($ptr);
free $ptr;
my $result = smbc_stat($url, $stat);

Thanks!

@plicease plicease added the enhancement New feature or request label Dec 1, 2023
@plicease plicease self-assigned this Dec 1, 2023
@plicease
Copy link
Member

plicease commented Dec 1, 2023

Yes that code is correct and works for the platform that you are running on, but it won't be portable if you need the code to run on platforms where stat is a different size (and it can and does vary).

There should be a way to construct an uninitialized stat though this is a thing that is missing from the interface. I'm thinking that calling new with no arguments should do exactly that:

  my $stat = FFI::C::Stat->new;

Since that is currently an error it will complain and die if you try to do that with an old version of FFI::C::Stat instead of silently erroring or corrupting memory.

Thank you for reporting this.

@plicease
Copy link
Member

plicease commented Dec 1, 2023

I think #10 will do what you need.

@plicease
Copy link
Member

plicease commented Dec 1, 2023

release #10 as 0.03. Let me know if you have any questions.

@plicease plicease closed this as completed Dec 1, 2023
@bscan
Copy link
Author

bscan commented Dec 1, 2023

Awesome, thanks! I just updated to 0.03 and it works for me with smbc_stat. Thank you for taking a look and for the fast turnaround on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

2 participants