diff options
author | Nick Mathewson <nickm@torproject.org> | 2008-01-02 06:59:12 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2008-01-02 06:59:12 +0000 |
commit | 86f5180853e4054c6a14dda69253bccf6ea50a4c (patch) | |
tree | 7e271734bc756086974a0b4047d94034087e22bb /src/or | |
parent | c0ec2815652842bdb2b533c2a8ddd125f26730fb (diff) | |
download | tor-86f5180853e4054c6a14dda69253bccf6ea50a4c.tar tor-86f5180853e4054c6a14dda69253bccf6ea50a4c.tar.gz |
r15786@tombo: nickm | 2008-01-02 01:11:51 -0500
Push the strdups used for parsing configuration lines into parse_line_from_string(). This will make it easier to parse more complex value formats, which in turn will help fix bug 557
svn:r13020
Diffstat (limited to 'src/or')
-rw-r--r-- | src/or/config.c | 13 | ||||
-rw-r--r-- | src/or/or.h | 2 | ||||
-rw-r--r-- | src/or/test.c | 45 |
3 files changed, 36 insertions, 24 deletions
diff --git a/src/or/config.c b/src/or/config.c index ab69d0a6d..4fabe9ecf 100644 --- a/src/or/config.c +++ b/src/or/config.c @@ -1366,16 +1366,16 @@ config_line_append(config_line_t **lst, /** Helper: parse the config string and strdup into key/value * strings. Set *result to the list, or NULL if parsing the string * failed. Return 0 on success, -1 on failure. Warn and ignore any - * misformatted lines. Modifies the contents of <b>string</b>. */ + * misformatted lines. */ int -config_get_lines(char *string, config_line_t **result) +config_get_lines(const char *string, config_line_t **result) { config_line_t *list = NULL, **next; char *k, *v; next = &list; do { - string = parse_line_from_str(string, &k, &v); + string = parse_config_line_from_str(string, &k, &v); if (!string) { config_free_lines(list); return -1; @@ -1385,10 +1385,13 @@ config_get_lines(char *string, config_line_t **result) * rather than using config_line_append over and over and getting * n^2 performance. */ *next = tor_malloc(sizeof(config_line_t)); - (*next)->key = tor_strdup(k); - (*next)->value = tor_strdup(v); + (*next)->key = k; + (*next)->value = v; (*next)->next = NULL; next = &((*next)->next); + } else { + tor_free(k); + tor_free(v); } } while (*string); diff --git a/src/or/or.h b/src/or/or.h index 5058ce562..dcb89bb54 100644 --- a/src/or/or.h +++ b/src/or/or.h @@ -2634,7 +2634,7 @@ const char *safe_str(const char *address); const char *escaped_safe_str(const char *address); const char *get_version(void); -int config_get_lines(char *string, config_line_t **result); +int config_get_lines(const char *string, config_line_t **result); void config_free_lines(config_line_t *front); int options_trial_assign(config_line_t *list, int use_defaults, int clear_first, char **msg); diff --git a/src/or/test.c b/src/or/test.c index 23bae4820..20bbc4910 100644 --- a/src/or/test.c +++ b/src/or/test.c @@ -682,6 +682,7 @@ test_util(void) uint32_t u32; uint16_t u16; char *cp, *k, *v; + const char *str; start.tv_sec = 5; start.tv_usec = 5000; @@ -821,52 +822,60 @@ test_util(void) U64_PRINTF_ARG(U64_LITERAL(12345678901))); test_streq(buf, "x!12345678901!x"); - /* Test parse_line_from_str */ + /* Test parse_config_line_from_str */ strlcpy(buf, "k v\n" " key value with spaces \n" "keykey val\n" "k2\n" "k3 \n" "\n" " \n" "#comment\n" "k4#a\n" "k5#abc\n" "k6 val #with comment\n", sizeof(buf)); - cp = buf; + str = buf; - cp = parse_line_from_str(cp, &k, &v); + str = parse_config_line_from_str(str, &k, &v); test_streq(k, "k"); test_streq(v, "v"); - test_assert(!strcmpstart(cp, " key value with")); + tor_free(k); tor_free(v); + test_assert(!strcmpstart(str, " key value with")); - cp = parse_line_from_str(cp, &k, &v); + str = parse_config_line_from_str(str, &k, &v); test_streq(k, "key"); test_streq(v, "value with spaces"); - test_assert(!strcmpstart(cp, "keykey")); + tor_free(k); tor_free(v); + test_assert(!strcmpstart(str, "keykey")); - cp = parse_line_from_str(cp, &k, &v); + str = parse_config_line_from_str(str, &k, &v); test_streq(k, "keykey"); test_streq(v, "val"); - test_assert(!strcmpstart(cp, "k2\n")); + tor_free(k); tor_free(v); + test_assert(!strcmpstart(str, "k2\n")); - cp = parse_line_from_str(cp, &k, &v); + str = parse_config_line_from_str(str, &k, &v); test_streq(k, "k2"); test_streq(v, ""); - test_assert(!strcmpstart(cp, "k3 \n")); + tor_free(k); tor_free(v); + test_assert(!strcmpstart(str, "k3 \n")); - cp = parse_line_from_str(cp, &k, &v); + str = parse_config_line_from_str(str, &k, &v); test_streq(k, "k3"); test_streq(v, ""); - test_assert(!strcmpstart(cp, "\n \n")); + tor_free(k); tor_free(v); + test_assert(!strcmpstart(str, "\n \n")); - cp = parse_line_from_str(cp, &k, &v); + str = parse_config_line_from_str(str, &k, &v); test_streq(k, "k4"); test_streq(v, ""); - test_assert(!strcmpstart(cp, "k5#abc")); + tor_free(k); tor_free(v); + test_assert(!strcmpstart(str, "k5#abc")); - cp = parse_line_from_str(cp, &k, &v); + str = parse_config_line_from_str(str, &k, &v); test_streq(k, "k5"); test_streq(v, ""); - test_assert(!strcmpstart(cp, "k6")); + tor_free(k); tor_free(v); + test_assert(!strcmpstart(str, "k6")); - cp = parse_line_from_str(cp, &k, &v); + str = parse_config_line_from_str(str, &k, &v); test_streq(k, "k6"); test_streq(v, "val"); - test_streq(cp, ""); + tor_free(k); tor_free(v); + test_streq(str, ""); /* Test for strcmpstart and strcmpend. */ test_assert(strcmpstart("abcdef", "abcdef")==0); |