puppetlabs/puppet · error · Puppet::Error

FormatMessageW failed to allocate buffer for code %{code}

Error message

FormatMessageW failed to allocate buffer for code %{code}

What it means

Companion to the format failure: FormatMessageW reported success but the output buffer Windows allocated (via FORMAT_MESSAGE_ALLOCATE_BUFFER, read through read_win32_local_pointer) came back null, so Puppet has no memory to read the message from and raises plain Puppet::Error with the code.

Source

Thrown at lib/puppet/util/windows/error.rb:51

            FORMAT_MESSAGE_ARGUMENT_ARRAY |
            FORMAT_MESSAGE_IGNORE_INSERTS |
            FORMAT_MESSAGE_MAX_WIDTH_MASK
    error_string = ''.dup

    # this pointer actually points to a :lpwstr (pointer) since we're letting Windows allocate for us
    FFI::MemoryPointer.new(:pointer, 1) do |buffer_ptr|
      length = FormatMessageW(flags, FFI::Pointer::NULL, code, dwLanguageId,
                              buffer_ptr, 0, FFI::Pointer::NULL)

      if length == FFI::WIN32_FALSE
        # can't raise same error type here or potentially recurse infinitely
        raise Puppet::Error, _("FormatMessageW could not format code %{code}") % { code: code }
      end

      # returns an FFI::Pointer with autorelease set to false, which is what we want
      buffer_ptr.read_win32_local_pointer do |wide_string_ptr|
        if wide_string_ptr.null?
          raise Puppet::Error, _("FormatMessageW failed to allocate buffer for code %{code}") % { code: code }
        end

        error_string = wide_string_ptr.read_wide_string(length)
      end
    end

    error_string
  end

  ERROR_FILE_NOT_FOUND      = 2
  ERROR_ACCESS_DENIED       = 5

  FORMAT_MESSAGE_ALLOCATE_BUFFER   = 0x00000100
  FORMAT_MESSAGE_IGNORE_INSERTS    = 0x00000200
  FORMAT_MESSAGE_FROM_SYSTEM       = 0x00001000
  FORMAT_MESSAGE_ARGUMENT_ARRAY    = 0x00002000
  FORMAT_MESSAGE_MAX_WIDTH_MASK    = 0x000000FF

View on GitHub (pinned to e227c27540)

Solutions

  1. Treat as environmental: free memory or reboot, then retry.
  2. In your own wrappers, catch Puppet::Error from formatting and degrade to a hex rendering of the code.
Defensive patterns

Strategy: fallback

Try / catch

def safe_win32_message(code)
  Puppet::Util::Windows::Error.format_error_code(code)
rescue Puppet::Error
  "unformatted Windows error 0x#{code.to_s(16).upcase}"
end

Prevention

When it happens

Trigger: LocalAlloc failure or a broken API contract inside the FORMAT_MESSAGE_ALLOCATE_BUFFER path while formatting any Windows error code — essentially only under memory exhaustion or a corrupted system state.

Common situations: Severe memory pressure on a Windows agent; otherwise theoretical. Treat as an environmental signal, not an application bug.

Related errors


AI-assisted analysis of puppetlabs/puppet@e227c27540 (2026-08-21). Data as JSON: /api/errors/261aadbb9bcafc64. Report an issue: GitHub.