ruby/ext/psych
Peter Zhu 7b7dde37f5 [ruby/psych] Guard from memory leak in Psych::Emitter#start_document
When an exception is raised, it can leak memory in `head`. There are two
places that can leak memory:

1. `Check_Type(tuple, T_ARRAY)` can leak memory if `tuple` is not an
   array.
2. `StringValue(name)` and `StringValue(value)` if they are not strings
   and the call to `to_str` does not return a string.

This commit fixes these memory leaks by wrapping the code around a
rb_ensure so that the memory is freed in all cases.

The following code demonstrates the memory leak:

    emitter = Psych::Emitter.new(StringIO.new)
    nil_to_string_tags = [[nil, "tag:TALOS"]] + ([1] * 1000)
    expected_array_tags = [1] * 1000

    10.times do
      1_000.times do
        # Raises `no implicit conversion of nil into String`
        emitter.start_document([], nil_to_string_tags, 0)
      rescue TypeError
      end

      1_000.times do
        # Raises `wrong argument type Integer (expected Array)`
        emitter.start_document([], expected_array_tags, 0)
      rescue TypeError
      end

      puts `ps -o rss= -p #{$$}`
    end

Before:

    47248
    79728
    111968
    144224
    176480
    208896
    241104
    273280
    305472
    337664

After:

    14832
    15088
    15344
    15344
    15360
    15632
    15632
    15632
    15648
    15648

https://github.com/ruby/psych/commit/053af73818
2024-08-09 20:28:53 +00:00
..
lib Don't call `Kernel#require` in hot loop 2024-07-19 20:37:20 +00:00
.gitignore Ignore yaml source 2022-04-05 14:31:54 +09:00
depend ruby tool/update-deps --fix 2024-04-27 21:55:28 +09:00
extconf.rb [ruby/psych] configure of libyaml couldn't detect "arm64-apple-darwin22" for build host. 2023-01-17 08:26:22 +00:00
psych.c [ruby/psych] Convert tabs to spaces in C files 2024-08-09 18:29:46 +00:00
psych.gemspec [ruby/psych] Provide a 'Changelog' link on rubygems.org/gems/psych 2024-01-17 00:48:52 +00:00
psych.h Update psych-2.2.1 2016-11-30 13:55:02 +00:00
psych_emitter.c [ruby/psych] Guard from memory leak in Psych::Emitter#start_document 2024-08-09 20:28:53 +00:00
psych_emitter.h ext: protoize no-arguments functions 2014-10-04 23:29:24 +00:00
psych_parser.c [ruby/psych] Convert missed tabs to spaces in C files 2024-08-09 19:35:28 +00:00
psych_parser.h ext: protoize no-arguments functions 2014-10-04 23:29:24 +00:00
psych_to_ruby.c Update psych-2.2.1 2016-11-30 13:55:02 +00:00
psych_to_ruby.h * ext/psych/extconf.rb: use embedded libyaml if no system libyaml is 2012-11-28 04:34:41 +00:00
psych_yaml_tree.c Remove private_iv_get 2020-09-25 13:11:32 +09:00
psych_yaml_tree.h * ext/psych/extconf.rb: use embedded libyaml if no system libyaml is 2012-11-28 04:34:41 +00:00