Project

General

Profile

Actions

Bug #21083

open

current SystemCallError.new(-1) behavior on windows is not in accordance with the intent of the source code.

Added by YO4 (Yoshinao Muramatsu) 15 days ago. Updated 12 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:120748]

Description

spec/ruby/core/exception/system_call_error_spec.rb says

    platform_is :windows do
      SystemCallError.new(-1).message.should == "The operation completed successfully."
    end

but source code says if (e < 0) e = GetLastError(); at win32/win32.c(2838,9) [1]
So it appears that the intent is to retrieve previous errors.
This is not what the spec intended.

It seems that at some point the behavior was broken because the WIN32API TlsGetValue() sets LastError even if it succeeds.
SystemCallError.new(-1).errno returns -1 instead of GetLastError() returns, so the current behavior is not very useful.

In my opinion, it is preferable to return "Unknown Error #{e}" as on other platforms.
This change needs to revert ruby/spec@328ac37 [2]

[1] https://github.com/ruby/ruby/blob/2b1db0faecf03d3fa1b7a1b102cc4c51aa962d5f/win32/win32.c#L2838
[2] https://github.com/ruby/spec/commit/328ac37

BACKGROUND

I state the situation because I may be wrong in my guess.
I was trying to make changes to allow Language TLS to be used on Windows, but found this spec fails.

  1)
  SystemCallError.new sets an 'unknown error' message when an unknown error number FAILED
  Expected "The system cannot find message text for message number 0x%1 in the message file for %2." == "The operation completed successfully."
  to be truthy but was false
  D:/a/ruby/ruby/src/spec/ruby/core/exception/system_call_error_spec.rb:104:in 'block (3 levels) in <top (required)>'
  D:/a/ruby/ruby/src/spec/ruby/core/exception/system_call_error_spec.rb:103:in 'block (2 levels) in <top (required)>'
  D:/a/ruby/ruby/src/spec/ruby/core/exception/system_call_error_spec.rb:22:in '<top (required)>'
  
  2)
  SystemCallError.new adds a custom error message to an 'unknown error' message when an unknown error number and a custom message specified FAILED
  Expected "The system cannot find the path specified. - custom message" == "The operation completed successfully. - custom message"
  to be truthy but was false
  D:/a/ruby/ruby/src/spec/ruby/core/exception/system_call_error_spec.rb:114:in 'block (3 levels) in <top (required)>'
  D:/a/ruby/ruby/src/spec/ruby/core/exception/system_call_error_spec.rb:113:in 'block (2 levels) in <top (required)>'
  D:/a/ruby/ruby/src/spec/ruby/core/exception/system_call_error_spec.rb:22:in '<top (required)>'

message 1) because the previous execution of SystemCallError.new(2**24) failed to retrieve the message.
MinGW(UCRT64) and VS2022 got same but VS2015 and VS2019 got different message "The resource loader cache doesn't have loaded MUI entry."
message 2) I am not sure where this message was obtained from.

Updated by nobu (Nobuyoshi Nakada) 14 days ago

I think we should drop that code (and the example in ruby/spec).
It is undocumented, non-portable, and unreliable.

Updated by YO4 (Yoshinao Muramatsu) 12 days ago

I made a patch. Includes spec changes.
The message of SystemCallError out of the errno range is "Unknown Error", same as ucrt's.

github #12622

Actions

Also available in: Atom PDF

Like0
Like0Like0