diff --git a/lib/fluent/config/error.rb b/lib/fluent/config/error.rb index 30374611ae..1bbc199ac7 100644 --- a/lib/fluent/config/error.rb +++ b/lib/fluent/config/error.rb @@ -23,4 +23,7 @@ class ConfigParseError < ConfigError class ObsoletedParameterError < ConfigError end + + class SetNil < ConfigError + end end diff --git a/lib/fluent/config/literal_parser.rb b/lib/fluent/config/literal_parser.rb index 0d1d928d6f..4e693341a5 100644 --- a/lib/fluent/config/literal_parser.rb +++ b/lib/fluent/config/literal_parser.rb @@ -81,7 +81,11 @@ def scan_double_quoted_string string = [] while true if skip(/\"/) - return string.join + if string.include?(nil) + return nil + else + return string.join + end elsif check(/[^"]#{LINE_END_WITHOUT_SPACING_AND_COMMENT}/) if s = check(/[^\\]#{LINE_END_WITHOUT_SPACING_AND_COMMENT}/) string << s @@ -168,7 +172,11 @@ def eval_embedded_code(code) hostname = Socket.gethostname worker_id = ENV['SERVERENGINE_WORKER_ID'] || '' EOM - @eval_context.instance_eval(code) + begin + @eval_context.instance_eval(code) + rescue SetNil => e + nil + end end def eval_escape_char(c) diff --git a/lib/fluent/config/types.rb b/lib/fluent/config/types.rb index 6b8f2b384a..64bc5a82c4 100644 --- a/lib/fluent/config/types.rb +++ b/lib/fluent/config/types.rb @@ -87,9 +87,13 @@ def self.regexp_value(str, opts = {}, name = nil) end STRING_TYPE = Proc.new { |val, opts = {}, name = nil| - v = val.to_s - v = v.frozen? ? v.dup : v # config_param can't assume incoming string is mutable - v.force_encoding(Encoding::UTF_8) + if val.nil? + nil + else + v = val.to_s + v = v.frozen? ? v.dup : v # config_param can't assume incoming string is mutable + v.force_encoding(Encoding::UTF_8) + end } ENUM_TYPE = Proc.new { |val, opts = {}, name = nil| diff --git a/test/config/test_literal_parser.rb b/test/config/test_literal_parser.rb index 212d470377..707074b3b9 100644 --- a/test/config/test_literal_parser.rb +++ b/test/config/test_literal_parser.rb @@ -240,6 +240,7 @@ def test_falseX assert_text_parsed_as("foo1", '"foo#{worker_id}"') ENV.delete('SERVERENGINE_WORKER_ID') } + test('nil') { assert_text_parsed_as(nil, '"#{raise SetNil}"') } end sub_test_case 'array parsing' do diff --git a/test/config/test_types.rb b/test/config/test_types.rb index cf714cdab0..6cbca2000c 100644 --- a/test/config/test_types.rb +++ b/test/config/test_types.rb @@ -149,6 +149,10 @@ class TestConfigTypes < ::Test::Unit::TestCase assert_equal Encoding::UTF_8, Config::STRING_TYPE.call(val, {}).encoding end + test 'string nil' do + assert_equal nil, Config::STRING_TYPE.call(nil, {}) + end + data('latin' => 'Märch', 'ascii' => 'ascii', 'space' => ' ',