From f2591397f92fe911257bef216b87bb2f233efb89 Mon Sep 17 00:00:00 2001 From: Lars Kanis Date: Fri, 5 Jun 2020 19:41:22 +0200 Subject: [PATCH 1/3] Revert to msvcrt.dll as LIBC on MINGW ffi-1.13.0 switched FFI::Library::LIBC from msvcrt.dll to ucrtbase.dll as part of #779 in commit c67468366e63bcf84512837384049fbba7e4748c . As described in #788 ucrtbase.dll has behavioural changes which shouldn't be released as part of a minor version change of ffi. While the change makes sense for mswin, we revert it for mingw. Fixes #788 --- lib/ffi/platform.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/ffi/platform.rb b/lib/ffi/platform.rb index 959a72cff..ebc9d8b1c 100644 --- a/lib/ffi/platform.rb +++ b/lib/ffi/platform.rb @@ -129,7 +129,11 @@ def self.is_os(os) end LIBC = if IS_WINDOWS - "ucrtbase.dll" + if RbConfig::CONFIG['host_os'] =~ /mingw/i + RbConfig::CONFIG['RUBY_SO_NAME'].split('-')[-2] + '.dll' + else + "ucrtbase.dll" + end elsif IS_GNU GNU_LIBC elsif OS == 'cygwin' From 90f4fcabc2ee729bfac83a103dac0780c37f05fd Mon Sep 17 00:00:00 2001 From: Lars Kanis Date: Tue, 9 Jun 2020 15:20:36 +0200 Subject: [PATCH 2/3] Revert usage of libc's time() on MINGW Since libc is reverted to msvcrt.dll, we can use classic time() function again. --- bench/bench_time.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bench/bench_time.rb b/bench/bench_time.rb index 67148460e..aaec64f36 100644 --- a/bench/bench_time.rb +++ b/bench/bench_time.rb @@ -4,7 +4,7 @@ module BenchTime module Posix extend FFI::Library ffi_lib FFI::Library::LIBC - if FFI::Platform.windows? + if RUBY_PLATFORM =~ /mswin/ attach_function :time, :_time64, [ :buffer_out ], :uint64, ignore_error: true else attach_function :time, [ :buffer_out ], :ulong, ignore_error: true From f2d35a96a0cf639b7a603533323ef702c84c00ac Mon Sep 17 00:00:00 2001 From: Lars Kanis Date: Tue, 9 Jun 2020 15:53:00 +0200 Subject: [PATCH 3/3] Add a spec regarding issue #788 --- spec/ffi/library_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/ffi/library_spec.rb b/spec/ffi/library_spec.rb index ff18f9b71..b622a29ac 100644 --- a/spec/ffi/library_spec.rb +++ b/spec/ffi/library_spec.rb @@ -64,6 +64,20 @@ class StructUCDP < FFI::Struct end end + if RbConfig::CONFIG['host_os'] =~ /mingw/ + # See https://github.com/ffi/ffi/issues/788 + it "libc functions shouldn't call an invalid parameter handler" do + mod = Module.new do + extend FFI::Library + ffi_lib 'c' + attach_function(:get_osfhandle, :_get_osfhandle, [:int], :intptr_t) + end + + expect( mod.get_osfhandle(42) ).to eq(-1) + end + end + + describe "ffi_lib" do it "empty name list should raise error" do expect {