提交 872a71fe 编写于 作者: M Mark Sliva 提交者: Mark Sliva

gpconfig: properly escape newlines when setting gucs

If we don't do this, the postgresql.conf file can easily be corrupted.
Co-authored-by: NJacob Champion <pchampion@pivotal.io>
上级 e1c7e2fe
......@@ -375,8 +375,10 @@ def do_change(options):
# the quote_literal() function in Postgres.
def quote_string(guc, value):
if value is not None and guc and guc.vartype == "string":
# Escape single quotes and backslashes
value = value.replace("'", "''").replace("\\", "\\\\")
# Escape single quotes, backslashes, and newlines
value = value.replace("'", "''") \
.replace("\\", "\\\\") \
.replace("\n", "\\n")
# Single-quote the whole string
value = "'" + value + "'"
return value
......
......@@ -516,6 +516,12 @@ class GpConfig(GpTestCase):
result = self.subject.quote_string(self.guc, value)
self.assertEqual(result, expected)
def test_quote_string_with_newline(self):
value = "test\nstring"
expected = "'test\\nstring'"
result = self.subject.quote_string(self.guc, value)
self.assertEqual(result, expected)
def setup_for_testing_quoting_string_values(self, vartype, value, additional_args=None):
sys.argv = ["gpconfig", "--change", "my_property_name", "--value", value]
if additional_args:
......
......@@ -118,6 +118,34 @@ Feature: gpconfig integration tests
| application_name | string | xxxxxx | 'bod hi' | 'bod hi' | bod hi |
| application_name | string | xxxxxx | '' | '' | |
@concourse_cluster
@demo_cluster
Scenario: write a newline using gpconfig
Given the user runs "gpstop -u"
And gpstop should return a return code of 0
And the gpconfig context is setup
# we do this to make sure all segment files contain this <guc>, both in file and live
# todo: we should instead use a custom guc here once we fix the bug that prevents us from setting custom gucs
And the user runs "gpconfig -c default_text_search_config -v xxxxxx"
And gpconfig should return a return code of 0
And the user runs "gpstop -u"
And gpstop should return a return code of 0
When the user runs "gpconfig -c default_text_search_config -v $'a\nb'"
Then verify that the last line of the file "postgresql.conf" in the master data directory contains the string "default_text_search_config='a\nb'" escaped
# now make sure the last changes took full effect as seen by gpconfig
When the user runs "gpconfig -s default_text_search_config --file"
Then gpconfig should return a return code of 0
And gpconfig should print "Master value: 'a\nb'" escaped to stdout
When the user runs "gpstop -u"
Then gpstop should return a return code of 0
When the user runs "gpconfig -s default_text_search_config"
Then gpconfig should return a return code of 0
And gpconfig should print "Master value: a\nb" to stdout
@concourse_cluster
@demo_cluster
Scenario Outline: gpconfig basic removal for type: <type>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册