aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2016-12-23 17:13:03 +0100
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2016-12-23 17:13:03 +0100
commitccb429435e162915917f2492217c4e206b9b2a96 (patch)
tree5aca18649cd45f2583593ff5799e9512c1960eb2
parentb9ec274bed571db9c705b65bb8c0bb6409c34fc4 (diff)
downloadblogc-ccb429435e162915917f2492217c4e206b9b2a96.tar.gz
blogc-ccb429435e162915917f2492217c4e206b9b2a96.tar.bz2
blogc-ccb429435e162915917f2492217c4e206b9b2a96.zip
blogc: common: git-receiver: improved error handling
-rw-r--r--src/blogc/loader.c5
-rw-r--r--src/common/config-parser.c30
-rw-r--r--tests/blogc-git-receiver/check_post_receive.c11
3 files changed, 26 insertions, 20 deletions
diff --git a/src/blogc/loader.c b/src/blogc/loader.c
index 57db98f..165bb8e 100644
--- a/src/blogc/loader.c
+++ b/src/blogc/loader.c
@@ -62,6 +62,7 @@ blogc_template_parse_from_file(const char *f, bc_error_t **err)
{
if (err == NULL || *err != NULL)
return NULL;
+
size_t len;
char *s = bc_file_get_contents(f, true, &len, err);
if (s == NULL)
@@ -77,6 +78,7 @@ blogc_source_parse_from_file(const char *f, bc_error_t **err)
{
if (err == NULL || *err != NULL)
return NULL;
+
size_t len;
char *s = bc_file_get_contents(f, true, &len, err);
if (s == NULL)
@@ -98,6 +100,9 @@ blogc_source_parse_from_file(const char *f, bc_error_t **err)
bc_slist_t*
blogc_source_parse_from_files(bc_trie_t *conf, bc_slist_t *l, bc_error_t **err)
{
+ if (err == NULL || *err != NULL)
+ return NULL;
+
bc_error_t *tmp_err = NULL;
bc_slist_t *rv = NULL;
unsigned int with_date = 0;
diff --git a/src/common/config-parser.c b/src/common/config-parser.c
index ff05f2e..bfea2d4 100644
--- a/src/common/config-parser.c
+++ b/src/common/config-parser.c
@@ -26,7 +26,7 @@ typedef enum {
bc_config_t*
bc_config_parse(const char *src, size_t src_len, bc_error_t **err)
{
- if (err != NULL && *err != NULL)
+ if (err == NULL || *err != NULL)
return NULL;
size_t current = 0;
@@ -69,9 +69,8 @@ bc_config_parse(const char *src, size_t src_len, bc_error_t **err)
state = CONFIG_SECTION_KEY;
continue;
}
- if (err != NULL)
- *err = bc_error_parser(BC_ERROR_CONFIG_PARSER, src, src_len,
- current, "File must start with section.");
+ *err = bc_error_parser(BC_ERROR_CONFIG_PARSER, src, src_len,
+ current, "File must start with section.");
break;
case CONFIG_SECTION_START:
@@ -91,9 +90,8 @@ bc_config_parse(const char *src, size_t src_len, bc_error_t **err)
}
if (c != '\r' && c != '\n')
break;
- if (err != NULL)
- *err = bc_error_parser(BC_ERROR_CONFIG_PARSER, src, src_len,
- current, "Section names can't have new lines.");
+ *err = bc_error_parser(BC_ERROR_CONFIG_PARSER, src, src_len,
+ current, "Section names can't have new lines.");
break;
case CONFIG_SECTION_KEY:
@@ -105,15 +103,13 @@ bc_config_parse(const char *src, size_t src_len, bc_error_t **err)
if (c != '\r' && c != '\n' && !is_last)
break;
// key without value, should we support it?
- if (err != NULL) {
- size_t end = is_last && c != '\n' && c != '\r' ? src_len :
- current;
- key = bc_strndup(src + start, end - start);
- *err = bc_error_parser(BC_ERROR_CONFIG_PARSER, src, src_len,
- current, "Key without value: %s.", key);
- free(key);
- key = NULL;
- }
+ size_t end = is_last && c != '\n' && c != '\r' ? src_len :
+ current;
+ key = bc_strndup(src + start, end - start);
+ *err = bc_error_parser(BC_ERROR_CONFIG_PARSER, src, src_len,
+ current, "Key without value: %s.", key);
+ free(key);
+ key = NULL;
break;
case CONFIG_SECTION_VALUE_START:
@@ -139,7 +135,7 @@ bc_config_parse(const char *src, size_t src_len, bc_error_t **err)
}
- if (err != NULL && *err != NULL) {
+ if (*err != NULL) {
bc_config_free(rv);
rv = NULL;
break;
diff --git a/tests/blogc-git-receiver/check_post_receive.c b/tests/blogc-git-receiver/check_post_receive.c
index 74a9a29..8e56f98 100644
--- a/tests/blogc-git-receiver/check_post_receive.c
+++ b/tests/blogc-git-receiver/check_post_receive.c
@@ -31,7 +31,10 @@ __wrap_realpath(const char *path, char *resolved_path)
static void
test_post_receive_get_config_section(void **state)
{
- bc_config_t *config = bc_config_parse("", 0, NULL);
+ bc_error_t *err = NULL;
+
+ bc_config_t *config = bc_config_parse("", 0, &err);
+ assert_null(err);
assert_null(bgr_post_receive_get_config_section(config,
"/home/blogc/repos/foo.git", "/home/blogc"));
bc_config_free(config);
@@ -48,7 +51,8 @@ test_post_receive_get_config_section(void **state)
"[repo:baz.git]\n"
"mirror = baz\n"
"\n";
- config = bc_config_parse(conf, strlen(conf), NULL);
+ config = bc_config_parse(conf, strlen(conf), &err);
+ assert_null(err);
char *s = bgr_post_receive_get_config_section(config,
"/home/blogc/repos/bar.git", "/home/blogc");
assert_string_equal(s, "repo:bar.git");
@@ -67,7 +71,8 @@ test_post_receive_get_config_section(void **state)
"[repo:asd/baz.git]\n"
"mirror = baz\n"
"\n";
- config = bc_config_parse(conf, strlen(conf), NULL);
+ config = bc_config_parse(conf, strlen(conf), &err);
+ assert_null(err);
s = bgr_post_receive_get_config_section(config,
"/home/blogc/repos/asd/bar.git", "/home/blogc");
assert_string_equal(s, "repo:asd/bar.git");