Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure strings that may be confused as YAML1.2 numbers are quoted #628

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/psych/visitors/yaml_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def visit_String o
style = Nodes::Scalar::FOLDED
elsif o =~ /^[^[:word:]][^"]*$/
style = Nodes::Scalar::DOUBLE_QUOTED
elsif not String === @ss.tokenize(o) or /\A0[0-7]*[89]/ =~ o
elsif not String === @ss.tokenize(o) or may_be_confused_as_yaml12?(o)
style = Nodes::Scalar::SINGLE_QUOTED
end

Expand Down Expand Up @@ -392,6 +392,19 @@ def binary? string
string.encoding == Encoding::ASCII_8BIT && !string.ascii_only?
end

def may_be_confused_as_yaml12? string
case string
when /\A0[0-7]*[89]\z/ # YAML 1.1 int (Base 8)
true
when /\A0o[0-7]+\z/ # YAML 1.2 int (Base 8)
true
when /\A[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?\z/ # YAML 1.2 float (Number)
true
else
false
end
end

def visit_array_subclass o
tag = "!ruby/array:#{o.class}"
ivars = o.instance_variables
Expand Down
10 changes: 10 additions & 0 deletions test/psych/visitors/test_yaml_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,19 @@ def test_float
end

def test_string
# YAML 1.1 int Base 8
assert_match(/'017'/, Psych.dump({'a' => '017'}))
assert_match(/'019'/, Psych.dump({'a' => '019'}))
assert_match(/'01818'/, Psych.dump({'a' => '01818'}))

# YAML 1.2 int Base 8
assert_match(/'0o17'/, Psych.dump({'a' => '0o17'}))
assert_match(/'0o111'/, Psych.dump({'a' => '0o111'}))

# YAML 1.2 float Number
assert_match(/'12e03'/, Psych.dump({'a' => '12e03'}))
assert_match(/'1.2e3'/, Psych.dump({'a' => '1.2e3'}))
assert_match(/".2e3"/, Psych.dump({'a' => '.2e3'}))
Comment on lines +176 to +182
Copy link
Member

@nobu nobu Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

. (any character) does not seem what you want here.

Suggested change
assert_match(/'0o17'/, Psych.dump({'a' => '0o17'}))
assert_match(/'0o111'/, Psych.dump({'a' => '0o111'}))
# YAML 1.2 float Number
assert_match(/'12e03'/, Psych.dump({'a' => '12e03'}))
assert_match(/'1.2e3'/, Psych.dump({'a' => '1.2e3'}))
assert_match(/".2e3"/, Psych.dump({'a' => '.2e3'}))
assert_include(Psych.dump({'a' => '0o17'}), "'0o17'")
assert_include(Psych.dump({'a' => '0o111'}), "'0o111'")
# YAML 1.2 float Number
assert_include(Psych.dump({'a' => '12e03'}), "'12e03'")
assert_include(Psych.dump({'a' => '1.2e3'}), "'1.2e3'")
assert_include(Psych.dump({'a' => '.2e3'}), '".2e3"')

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the last ".2e3" seems from YAML 1.1, right?

end

# http://yaml.org/type/null.html
Expand Down