Re: FFI opaque pointer approach

  • From: Michael Reid <kid.meier@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 2 Sep 2015 10:52:02 -0400

On Wed, Sep 2, 2015 at 10:40 AM, Alex <initrd.gz@xxxxxxxxx> wrote:

That C code can't possibly work without the definition of `krb5_context`,
because it needs the struct size (it's probably tucked away in a header
somewhere). Add the full definition to the FFI C string and your first
approach will work.

Not sure about that. The original C program specifically defines
krb_context as an opaque pointer, which is a common C design pattern.

I think the solution is that the Lua code should mirror the original
definition, i.e.:

ffi.cdef[[
typedef int krb5_int32;
typedef krb5_int32 krb5_error_code;
struct _krb5_context;
typedef struct _krb5_context *krb5_context; // MATCHES C DEF'N
krb5_error_code krb5_init_context(krb5_context *context);
]]
krb5 = ffi.load("krb5")
...
local ctx = ffi.new('krb5_context[1]')
local ret = krb5.krb5_init_context(ctx)
if ret == 0 then
ngx.say("SUCCESS->krb5_init_context ret 0")
else
ngx.say("FAILURE->krb5_init_context ret not 0")
end

This seems to work for me. And by "work" I mean no errors were given
and the value of ctx[0] seems to be populated by the call to
krb5_init_context.

/mike.

Other related posts: