Unsafe code in ruby helpers.rb get_redhatish_platform, needs null-check and maybe null-coalesce
In file /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/libraries/helpers.rb
I noticed this unsafe code:
def get_redhatish_platform(contents)
contents[/^Red Hat/i] ? "redhat" : contents[/(\w+)/i, 1].downcase
end
Suppose I call with empty string: get_redhatish_platform("")
Expected return value for an empty string input could be maybe empty string?
Actual result: nil gets asked to downcase itself which it takes a dim view of.
To make it safe, insert an ampersand. I'd submit a PR if that's wanted.
A one character fix is:
def get_redhatish_platform(contents)
contents[/^Red Hat/i] ? "redhat" : contents[/(\w+)/i, 1]&.downcase
end
That's necessary but perhaps not sufficient, perhaps appending a null-coalesce would be safer, and perhaps parens would be good.
def get_redhatish_platform(contents)
contents[/^Red Hat/i] ? "redhat" : contents[/(\w+)/i, 1]&.downcase || "?"
end
This actually happened and was reported in the forums, here:
https://forum.gitlab.com/t/gitlab-installation-undefined-method-downcase/4700