Commit 248dc6dd authored by Brian McGuire's avatar Brian McGuire

Safely handle escape sequences

And accompanying tests
parent 0b221135
...@@ -68,6 +68,7 @@ void strbuf_append_char(strbuf_t *buf, char c) ...@@ -68,6 +68,7 @@ void strbuf_append_char(strbuf_t *buf, char c)
strbuf_ensure_capacity(buf, 1); strbuf_ensure_capacity(buf, 1);
*(buf->string + buf->length) = c; *(buf->string + buf->length) = c;
++(buf->length); ++(buf->length);
*(buf->string + buf->length) = '\0';
} }
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
escape_seqs:
{
str = "abc";
newline = "abc\ndef\n";
cr = "abc\rdef\r";
tab = "abc\tdef\t";
feed = "abc\fdef\f";
backslash = "abc\\def\\";
dquote = "abc\"def\"";
};
...@@ -437,6 +437,57 @@ TT_TEST(RemoveSetting) ...@@ -437,6 +437,57 @@ TT_TEST(RemoveSetting)
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
TT_TEST(EscapedStrings)
{
config_t cfg;
config_setting_t* rc;
int ok;
const char* str;
config_init(&cfg);
config_set_include_dir(&cfg, "./testdata");
ok = config_read_file(&cfg, "testdata/strings.cfg");
if(!ok)
{
printf("error: %s:%d\n", config_error_text(&cfg),
config_error_line(&cfg));
}
TT_ASSERT_TRUE(ok);
ok = config_lookup_string(&cfg, "escape_seqs.str", &str);
TT_ASSERT_TRUE(ok);
TT_ASSERT_STR_EQ("abc", str);
ok = config_lookup_string(&cfg, "escape_seqs.newline", &str);
TT_ASSERT_TRUE(ok);
TT_ASSERT_STR_EQ("abc\ndef\n", str);
ok = config_lookup_string(&cfg, "escape_seqs.cr", &str);
TT_ASSERT_TRUE(ok);
TT_ASSERT_STR_EQ("abc\rdef\r", str);
ok = config_lookup_string(&cfg, "escape_seqs.tab", &str);
TT_ASSERT_TRUE(ok);
TT_ASSERT_STR_EQ("abc\tdef\t", str);
ok = config_lookup_string(&cfg, "escape_seqs.feed", &str);
TT_ASSERT_TRUE(ok);
TT_ASSERT_STR_EQ("abc\fdef\f", str);
ok = config_lookup_string(&cfg, "escape_seqs.backslash", &str);
TT_ASSERT_TRUE(ok);
TT_ASSERT_STR_EQ("abc\\def\\", str);
ok = config_lookup_string(&cfg, "escape_seqs.dquote", &str);
TT_ASSERT_TRUE(ok);
TT_ASSERT_STR_EQ("abc\"def\"", str);
config_destroy(&cfg);
}
/* ------------------------------------------------------------------------- */
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int failures; int failures;
...@@ -453,6 +504,7 @@ int main(int argc, char **argv) ...@@ -453,6 +504,7 @@ int main(int argc, char **argv)
TT_SUITE_TEST(LibConfigTests, BigInt6); TT_SUITE_TEST(LibConfigTests, BigInt6);
TT_SUITE_TEST(LibConfigTests, BigInt7); TT_SUITE_TEST(LibConfigTests, BigInt7);
TT_SUITE_TEST(LibConfigTests, RemoveSetting); TT_SUITE_TEST(LibConfigTests, RemoveSetting);
TT_SUITE_TEST(LibConfigTests, EscapedStrings);
TT_SUITE_RUN(LibConfigTests); TT_SUITE_RUN(LibConfigTests);
failures = TT_SUITE_NUM_FAILURES(LibConfigTests); failures = TT_SUITE_NUM_FAILURES(LibConfigTests);
TT_SUITE_END(LibConfigTests); TT_SUITE_END(LibConfigTests);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment