jmckaskill / luaffi

Standalone FFI library for calling C functions from lua. Compatible with the luajit FFI interface.
469 stars 84 forks source link

Crash in copy_submembers() #21

Closed jjensen closed 12 years ago

jjensen commented 12 years ago

Using the ffi call below with latest luaffi, the following crashes in copy_submembers() on this line:

        ct = *(const struct ctype*) lua_touserdata(L, -1);

Running under LuaJIT yields a proper result and no crash.

Thanks for the help!

-Josh

ffi = require 'ffi'

ffi.cdef [[
typedef struct _SYSTEM_INFO {
  union {
    unsigned long  dwOemId;
    struct {
      unsigned short wProcessorArchitecture;
      unsigned short wReserved;
    } ;
  } ;
  unsigned long     dwPageSize;
  void*    lpMinimumApplicationAddress;
  void*    lpMaximumApplicationAddress;
  unsigned long dwActiveProcessorMask;
  unsigned long     dwNumberOfProcessors;
  unsigned long     dwProcessorType;
  unsigned long     dwAllocationGranularity;
  unsigned short      wProcessorLevel;
  unsigned short      wProcessorRevision;
} SYSTEM_INFO;

void GetSystemInfo(
  SYSTEM_INFO* lpSystemInfo
);
]]

local info = ffi.new("SYSTEM_INFO")
ffi.C.GetSystemInfo(info)

print(info.dwNumberOfProcessors)
jmckaskill commented 12 years ago

On Thu, May 10, 2012 at 3:18 PM, Josh Jensen < reply@reply.github.com

wrote:

typedef struct _SYSTEM_INFO { union { unsigned long dwOemId; struct { unsigned short wProcessorArchitecture; unsigned short wReserved; } ; } ; unsigned long dwPageSize; void* lpMinimumApplicationAddress; void* lpMaximumApplicationAddress; unsigned long dwActiveProcessorMask; unsigned long dwNumberOfProcessors; unsigned long dwProcessorType; unsigned long dwAllocationGranularity; unsigned short wProcessorLevel; unsigned short wProcessorRevision; } SYSTEM_INFO;

dwActiveProcessorMask should be DWORD_PTR or uintptr_t btw. When I first ran this I was running in windows 64 bit and was wondering why I had 0 processors... See http://msdn.microsoft.com/en-us/library/windows/desktop/ms724958(v=vs.85).aspx

Thanks for the report. I've pushed a fix.

-- James