[Win32utils-devel] _mktemp on Windows

Heesob Park phasis at gmail.com
Mon Jan 16 09:36:59 EST 2012


Hi,

2012/1/16 Daniel Berger <djberg96 at gmail.com>:
> On Sun, Jan 15, 2012 at 8:44 PM, Heesob Park <phasis at gmail.com> wrote:
>> Hi,
>>
>> 2012/1/14 Daniel Berger <djberg96 at gmail.com>:
>>> Is it odd that calling _mktemp more than once in the same process doesn't work?
>>>
>>> require 'ffi'
>>>
>>> class Win
>>>  extend FFI::Library
>>>
>>>  ffi_lib 'msvcrt'
>>>  attach_function :_mktemp, [:string], :string
>>>
>>>  def self.mktemp
>>>    5.times{ p _mktemp("rb_file_temp_XXXXXX") }
>>>  end
>>> end
>>>
>>> Win.mktemp
>>>
>>> # Output
>>> "rb_file_temp_a03680"
>>> nil
>>> nil
>>> nil
>>> nil
>> It is odd indeed.
>> _mktemp not working in the loop.
>>
>> require 'ffi'
>>
>> class Win
>>  extend FFI::Library
>>
>>  ffi_lib 'msvcrt'
>>  attach_function :_mktemp, [:string], :string
>>
>>  def self.mktemp
>>   p _mktemp("rb_file_temp_XXXXXX")
>>   p _mktemp("rb_file_temp_XXXXXX")
>>   p _mktemp("rb_file_temp_XXXXXX")
>>   p _mktemp("rb_file_temp_XXXXXX")
>>   p _mktemp("rb_file_temp_XXXXXX")
>>  5.times {
>>   p _mktemp("rb_loop_temp_XXXXXX")
>>  }
>>  end
>> end
>>
>> Win.mktemp
>>
>>  # Output
>> "rb_file_temp_a05972"
>> "rb_file_temp_a05972"
>> "rb_file_temp_a05972"
>> "rb_file_temp_a05972"
>> "rb_file_temp_a05972"
>> "rb_loop_temp_a05972"
>> nil
>> nil
>> nil
>> nil
>
> I noticed that in the example of the bottom of the _mktemp
> documentation that they don't use the char* string directly, but
> instead strcpy it to an array of chars and use that. I guess there's a
> limit of 26 names.
>
I want to know why sequence of _mktemp works and _mktemp in the loop
not works after first call.

More over _mktemp_s segfaults in the loop after first call.

#code
require 'ffi'

class Win
 extend FFI::Library

 ffi_lib 'msvcrt'
 ffi_convention :stdcall
 attach_function :_mktemp_s, [:string,:int], :string

 def self.mktemp
3.times {
   str = "rb_file_temp_XXXXXX"
   _mktemp_s(str,21)
   p str
}
 end
end

Win.mktemp

#output
"rb_file_temp_a03212"
mt.rb:13: [BUG] Segmentation fault
ruby 1.9.3p0 (2011-10-30) [i386-mingw32]

-- Control frame information -----------------------------------------------
c:0008 p:---- s:0022 b:0022 l:000021 d:000021 CFUNC  :_mktemp_s
c:0007 p:0027 s:0017 b:0017 l:000008 d:000016 BLOCK  mt.rb:13
c:0006 p:---- s:0014 b:0014 l:000013 d:000013 FINISH
c:0005 p:---- s:0012 b:0012 l:000011 d:000011 CFUNC  :times
c:0004 p:0012 s:0009 b:0009 l:000008 d:000008 METHOD mt.rb:11
c:0003 p:0037 s:0006 b:0006 l:001d0c d:00150c EVAL   mt.rb:19
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH
c:0001 p:0000 s:0002 b:0002 l:001d0c d:001d0c TOP

-- Ruby level backtrace information ----------------------------------------
mt.rb:19:in `<main>'
mt.rb:11:in `mktemp'
mt.rb:11:in `times'
mt.rb:13:in `block in mktemp'
mt.rb:13:in `_mktemp_s'

-- C level backtrace information -------------------------------------------
C:\Windows\SysWOW64\ntdll.dll(ZwWaitForSingleObject+0x15) [0x7756f8b1]
C:\Windows\syswow64\kernel32.dll(WaitForSingleObjectEx+0x43) [0x75151194]
C:\Windows\syswow64\kernel32.dll(WaitForSingleObject+0x12) [0x75151148]
C:\Ruby193\bin\msvcrt-ruby191.dll(rb_vm_bugreport+0xf9) [0x62e5acc9]
C:\Ruby193\bin\msvcrt-ruby191.dll(rb_name_err_mesg_new+0x17a) [0x62d3a68e]
C:\Ruby193\bin\msvcrt-ruby191.dll(rb_bug+0x2f) [0x62d3b3ef]
C:\Ruby193\bin\msvcrt-ruby191.dll(rb_check_safe_str+0x1a4) [0x62ded27c]
 [0x004011e6]
C:\Windows\syswow64\kernel32.dll(GetProfileStringW+0x12aa3) [0x7519003f]
C:\Windows\SysWOW64\ntdll.dll(RtlKnownExceptionFilter+0xb7) [0x775c74df]

-- Other runtime information -----------------------------------------------

* Loaded script: mt.rb

* Loaded features:

    0 enumerator.so
    1 C:/Ruby193/lib/ruby/1.9.1/i386-mingw32/enc/encdb.so
    2 C:/Ruby193/lib/ruby/1.9.1/i386-mingw32/enc/cp949.so
    3 C:/Ruby193/lib/ruby/1.9.1/i386-mingw32/enc/trans/transdb.so
    4 C:/Ruby193/lib/ruby/1.9.1/rubygems/defaults.rb
    5 C:/Ruby193/lib/ruby/1.9.1/i386-mingw32/rbconfig.rb
    6 C:/Ruby193/lib/ruby/1.9.1/rubygems/deprecate.rb
    7 C:/Ruby193/lib/ruby/1.9.1/rubygems/exceptions.rb
    8 C:/Ruby193/lib/ruby/1.9.1/rubygems/defaults/operating_system.rb
    9 C:/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb
   10 C:/Ruby193/lib/ruby/1.9.1/rubygems.rb
   11 C:/Ruby193/lib/ruby/1.9.1/rubygems/version.rb
   12 C:/Ruby193/lib/ruby/1.9.1/rubygems/requirement.rb
   13 C:/Ruby193/lib/ruby/1.9.1/rubygems/platform.rb
   14 C:/Ruby193/lib/ruby/1.9.1/rubygems/specification.rb
   15 C:/Ruby193/lib/ruby/1.9.1/rubygems/path_support.rb
   16 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi_c.so
   17 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/platform.rb
   18 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/types.rb
   19 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/library.rb
   20 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/errno.rb
   21 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/pointer.rb
   22 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/memorypointer.rb
   23 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/struct_layout_build
er.rb
   24 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/struct.rb
   25 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/union.rb
   26 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/managedstruct.rb
   27 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/callback.rb
   28 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/io.rb
   29 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/autopointer.rb
   30 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/variadic.rb
   31 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/enum.rb
   32 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/ffi.rb
   33 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi.rb
   34 C:/Ruby193/lib/ruby/1.9.1/i386-mingw32/enc/utf_16le.so
   35 C:/Ruby193/lib/ruby/1.9.1/i386-mingw32/enc/trans/korean.so
   36 C:/Ruby193/lib/ruby/1.9.1/i386-mingw32/enc/trans/utf_16_32.so
   37 C:/Ruby193/lib/ruby/1.9.1/i386-mingw32/enc/trans/single_byte.so

[NOTE]
You may have encountered a bug in the Ruby interpreter or extension libraries.
Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html


This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.


But, calling without loop block works.

require 'ffi'

class Win
 extend FFI::Library

 ffi_lib 'msvcrt'
 ffi_convention :stdcall
 attach_function :_mktemp_s, [:string,:int], :string

 def self.mktemp
   str = "rb_file_temp_XXXXXX"
   _mktemp_s(str,21)
   p str
   str = "rb_file_temp_XXXXXX"
   _mktemp_s(str,21)
   p str
   str = "rb_file_temp_XXXXXX"
   _mktemp_s(str,21)
   p str
 end
end

Win.mktemp

#output
"rb_file_temp_a03940"
"rb_file_temp_a03940"
"rb_file_temp_a03940"



Regards,
Park Heesob


More information about the win32utils-devel mailing list