diff options
author | Taylor Yu <catalyst@torproject.org> | 2019-07-03 15:13:51 -0500 |
---|---|---|
committer | Taylor Yu <catalyst@torproject.org> | 2019-12-08 17:09:42 -0600 |
commit | 4b22c739fe8b04b05e7bdfdf18ec42ec4bf1c436 (patch) | |
tree | 7580363b1638c03d6c84f3e84c197cf71d5331b7 /src/lib/encoding | |
parent | 089466eff356454b0e0eb4e1d432057504a9a4d6 (diff) | |
download | tor-4b22c739fe8b04b05e7bdfdf18ec42ec4bf1c436.tar.gz tor-4b22c739fe8b04b05e7bdfdf18ec42ec4bf1c436.zip |
clean up kvline_can_encode_lines()
Add a check for '=' characters in needs_escape(). This simplifies the
logic in kvline_can_encode_lines().
Part of #30984.
Diffstat (limited to 'src/lib/encoding')
-rw-r--r-- | src/lib/encoding/kvline.c | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/src/lib/encoding/kvline.c b/src/lib/encoding/kvline.c index d4a8f510ba..e56b57b4cf 100644 --- a/src/lib/encoding/kvline.c +++ b/src/lib/encoding/kvline.c @@ -29,12 +29,20 @@ #include <string.h> /** Return true iff we need to quote and escape the string <b>s</b> to encode - * it. */ + * it. + * + * kvline_can_encode_lines() also uses this (with + * <b>as_keyless_val</b> true) to check whether a key would require + * quoting. + */ static bool needs_escape(const char *s, bool as_keyless_val) { if (as_keyless_val && *s == 0) return true; + /* Keyless values containing '=' need to be escaped. */ + if (as_keyless_val && strchr(s, '=')) + return true; for (; *s; ++s) { if (*s >= 127 || TOR_ISSPACE(*s) || ! TOR_ISPRINT(*s) || @@ -72,23 +80,16 @@ kvline_can_encode_lines(const config_line_t *line, unsigned flags) { for ( ; line; line = line->next) { const bool keyless = line_has_no_key(line); - if (keyless) { - if (! (flags & KV_OMIT_KEYS)) { - /* If KV_OMIT_KEYS is not set, we can't encode a line with no key. */ - return false; - } - if (strchr(line->value, '=') && !( flags & KV_QUOTED)) { - /* We can't have a keyless value with = without quoting it. */ - return false; - } + if (keyless && ! (flags & KV_OMIT_KEYS)) { + /* If KV_OMIT_KEYS is not set, we can't encode a line with no key. */ + return false; } if (needs_escape(line->value, keyless) && ! (flags & KV_QUOTED)) { /* If KV_QUOTED is false, we can't encode a value that needs quotes. */ return false; } - if (line->key && strlen(line->key) && - (needs_escape(line->key, false) || strchr(line->key, '='))) { + if (!keyless && needs_escape(line->key, true)) { /* We can't handle keys that need quoting. */ return false; } @@ -142,9 +143,6 @@ kvline_encode(const config_line_t *line, k = line->key; } else { eq = ""; - if (strchr(line->value, '=')) { - esc = true; - } } if ((flags & KV_OMIT_VALS) && line_has_no_val(line)) { |