aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2023-02-11 23:18:51 +0100
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2023-02-11 23:18:51 +0100
commit3f6ae823cd8ff8049abeca2be495c727563c919e (patch)
tree33cdbeb162263c0c360dd1b915b7060634d0986e
parente52e1f0b9d0796412ee36de260c6bbbcde348b0a (diff)
downloadblogc-3f6ae823cd8ff8049abeca2be495c727563c919e.tar.gz
blogc-3f6ae823cd8ff8049abeca2be495c727563c919e.tar.bz2
blogc-3f6ae823cd8ff8049abeca2be495c727563c919e.zip
make: fix global variable name validation
-rw-r--r--src/blogc-make/settings.c19
-rw-r--r--tests/blogc-make/check_settings.c34
2 files changed, 51 insertions, 2 deletions
diff --git a/src/blogc-make/settings.c b/src/blogc-make/settings.c
index be976e3..1079fc0 100644
--- a/src/blogc-make/settings.c
+++ b/src/blogc-make/settings.c
@@ -119,9 +119,24 @@ bm_settings_parse(const char *content, size_t content_len, bc_error_t **err)
if (global != NULL) {
for (size_t i = 0; global[i] != NULL; i++) {
for (size_t j = 0; global[i][j] != '\0'; j++) {
- if (!((global[i][j] >= 'A' && global[i][j] <= 'Z') || global[i][j] == '_')) {
+ if (j == 0) {
+ if (!(global[i][j] >= 'A' && global[i][j] <= 'Z')) {
+ *err = bc_error_new_printf(BLOGC_MAKE_ERROR_SETTINGS,
+ "Invalid [%s] key (first character must be uppercase): %s",
+ section, global[i]);
+ bc_strv_free(global);
+ bm_settings_free(rv);
+ rv = NULL;
+ goto cleanup;
+ }
+ continue;
+ }
+ if (!((global[i][j] >= 'A' && global[i][j] <= 'Z') ||
+ (global[i][j] >= '0' && global[i][j] <= '9') ||
+ global[i][j] == '_')) {
*err = bc_error_new_printf(BLOGC_MAKE_ERROR_SETTINGS,
- "Invalid [%s] key: %s", section, global[i]);
+ "Invalid [%s] key (must be uppercase with '_' and digits after first character): %s",
+ section, global[i]);
bc_strv_free(global);
bm_settings_free(rv);
rv = NULL;
diff --git a/tests/blogc-make/check_settings.c b/tests/blogc-make/check_settings.c
index d942c45..b268570 100644
--- a/tests/blogc-make/check_settings.c
+++ b/tests/blogc-make/check_settings.c
@@ -53,6 +53,40 @@ test_settings(void **state)
assert_string_equal(err->msg,
"[global] key required but not found or empty: AUTHOR_NAME");
bc_error_free(err);
+
+ a =
+ "[settings]\n"
+ "content_dir = guda\n"
+ "main_template = foo.tmpl\n"
+ "\n"
+ "[global]\n"
+ "bOLA = asd\n"
+ "GUDA = qwe\n";
+ err = NULL;
+ s = bm_settings_parse(a, strlen(a), &err);
+ assert_non_null(err);
+ assert_null(s);
+ assert_int_equal(err->type, BLOGC_MAKE_ERROR_SETTINGS);
+ assert_string_equal(err->msg,
+ "Invalid [global] key (first character must be uppercase): bOLA");
+ bc_error_free(err);
+
+ a =
+ "[settings]\n"
+ "content_dir = guda\n"
+ "main_template = foo.tmpl\n"
+ "\n"
+ "[global]\n"
+ "BOLA = asd\n"
+ "GUDa = qwe\n";
+ err = NULL;
+ s = bm_settings_parse(a, strlen(a), &err);
+ assert_non_null(err);
+ assert_null(s);
+ assert_int_equal(err->type, BLOGC_MAKE_ERROR_SETTINGS);
+ assert_string_equal(err->msg,
+ "Invalid [global] key (must be uppercase with '_' and digits after first character): GUDa");
+ bc_error_free(err);
}