{"record":{"id":"261aadbb9bcafc64","repo":"puppetlabs/puppet","slug":"formatmessagew-failed-to-allocate-buffer-for-code","errorCode":null,"errorMessage":"FormatMessageW failed to allocate buffer for code %{code}","messagePattern":"FormatMessageW failed to allocate buffer for code %(.+?)","errorType":"exception","errorClass":"Puppet::Error","httpStatus":null,"severity":"error","filePath":"lib/puppet/util/windows/error.rb","lineNumber":51,"sourceCode":"            FORMAT_MESSAGE_ARGUMENT_ARRAY |\n            FORMAT_MESSAGE_IGNORE_INSERTS |\n            FORMAT_MESSAGE_MAX_WIDTH_MASK\n    error_string = ''.dup\n\n    # this pointer actually points to a :lpwstr (pointer) since we're letting Windows allocate for us\n    FFI::MemoryPointer.new(:pointer, 1) do |buffer_ptr|\n      length = FormatMessageW(flags, FFI::Pointer::NULL, code, dwLanguageId,\n                              buffer_ptr, 0, FFI::Pointer::NULL)\n\n      if length == FFI::WIN32_FALSE\n        # can't raise same error type here or potentially recurse infinitely\n        raise Puppet::Error, _(\"FormatMessageW could not format code %{code}\") % { code: code }\n      end\n\n      # returns an FFI::Pointer with autorelease set to false, which is what we want\n      buffer_ptr.read_win32_local_pointer do |wide_string_ptr|\n        if wide_string_ptr.null?\n          raise Puppet::Error, _(\"FormatMessageW failed to allocate buffer for code %{code}\") % { code: code }\n        end\n\n        error_string = wide_string_ptr.read_wide_string(length)\n      end\n    end\n\n    error_string\n  end\n\n  ERROR_FILE_NOT_FOUND      = 2\n  ERROR_ACCESS_DENIED       = 5\n\n  FORMAT_MESSAGE_ALLOCATE_BUFFER   = 0x00000100\n  FORMAT_MESSAGE_IGNORE_INSERTS    = 0x00000200\n  FORMAT_MESSAGE_FROM_SYSTEM       = 0x00001000\n  FORMAT_MESSAGE_ARGUMENT_ARRAY    = 0x00002000\n  FORMAT_MESSAGE_MAX_WIDTH_MASK    = 0x000000FF\n","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/util/windows/error.rb#L33-L69","documentation":"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.","triggerScenarios":"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.","commonSituations":"Severe memory pressure on a Windows agent; otherwise theoretical. Treat as an environmental signal, not an application bug.","solutions":["Treat as environmental: free memory or reboot, then retry.","In your own wrappers, catch Puppet::Error from formatting and degrade to a hex rendering of the code."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"def safe_win32_message(code)\n  Puppet::Util::Windows::Error.format_error_code(code)\nrescue Puppet::Error\n  \"unformatted Windows error 0x#{code.to_s(16).upcase}\"\nend","preventionTips":["Degrade to hex codes when formatting fails.","Treat repeated hits as a memory/system health problem, not a code bug."],"tags":["puppet","windows","win32","formatmessage","memory-allocation"],"backgroundTag":"win32-error-formatting-failed","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}