diff --git a/doc/ext_ffi_api.html b/doc/ext_ffi_api.html index 2d69cb49..e865a5f7 100644 --- a/doc/ext_ffi_api.html +++ b/doc/ext_ffi_api.html @@ -336,14 +336,16 @@ objects.

Utility Functions

-

err = ffi.errno()

+

err = ffi.errno([newerr])

Returns the error number set by the last C function call which -indicated an error condition. +indicated an error condition. If the optional newerr argument +is present, the error number is set to the new value and the previous +value is returned.

-This function offers a portable and OS-independent way to get the error -number. Note that only some C functions set the error +This function offers a portable and OS-independent way to get and set the +error number. Note that only some C functions set the error number. And it's only significant if the function actually indicated an error condition (e.g. with a return value of -1 or NULL). Otherwise, it may or may not contain any previously set diff --git a/src/lib_ffi.c b/src/lib_ffi.c index 321de499..0f8d5013 100644 --- a/src/lib_ffi.c +++ b/src/lib_ffi.c @@ -545,7 +545,10 @@ LJLIB_CF(ffi_offsetof) LJLIB_CF(ffi_errno) { - setintV(L->top++, errno); + int err = errno; + if (L->top > L->base) + errno = ffi_checkint(L, 1); + setintV(L->top++, err); return 1; }