Skip to content

Commit

Permalink
Add MSVC invalid parameter handler (#14313)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil committed Feb 22, 2024
1 parent d55712b commit 677cab3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/exception/call_stack/stackwalk.cr
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ struct Exception::CallStack
# `Crystal::System::Thread.thread_proc`)
stack_size = Crystal::System::Fiber::RESERVED_STACK_SIZE
LibC.SetThreadStackGuarantee(pointerof(stack_size))

# this catches invalid argument checks inside the C runtime library
LibC._set_invalid_parameter_handler(->(expression, _function, _file, _line, _pReserved) do
message = expression ? String.from_utf16(expression)[0] : "(no message)"
Crystal::System.print_error "CRT invalid parameter handler invoked: %s\n", message
caller.each do |frame|
Crystal::System.print_error " from %s\n", frame
end
LibC._exit(1)
end)
end

{% if flag?(:interpreted) %} @[Primitive(:interpreter_call_stack_unwind)] {% end %}
Expand Down
1 change: 1 addition & 0 deletions src/lib_c/x86_64-windows-msvc/c/stdint.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
lib LibC
alias IntPtrT = Int64
alias UIntPtrT = UInt64
end
3 changes: 3 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/stdlib.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ lib LibC
fun realloc(ptr : Void*, size : SizeT) : Void*
fun strtof(nptr : Char*, endptr : Char**) : Float
fun strtod(nptr : Char*, endptr : Char**) : Double

alias InvalidParameterHandler = WCHAR*, WCHAR*, WCHAR*, UInt, UIntPtrT ->
fun _set_invalid_parameter_handler(pNew : InvalidParameterHandler) : InvalidParameterHandler
end

0 comments on commit 677cab3

Please sign in to comment.