diff --git a/doc/news/_preparation_next_release.md b/doc/news/_preparation_next_release.md index 1268059445e..5ad3491acb1 100644 --- a/doc/news/_preparation_next_release.md +++ b/doc/news/_preparation_next_release.md @@ -172,6 +172,12 @@ The following text lists news about the [plugins](https://www.libelektra.org/plu - <> - <> +### csvstorage + +- Fix a bug where writing unkown meta keys will fail silently _(Juri Schreib @Bujuhu)_ +- <> +- <> + ## Libraries The text below summarizes updates to the [C (and C++)-based libraries](https://www.libelektra.org/libraries/readme) of Elektra. diff --git a/src/plugins/csvstorage/csvstorage.c b/src/plugins/csvstorage/csvstorage.c index 61da3445a6f..a60da7b77a9 100644 --- a/src/plugins/csvstorage/csvstorage.c +++ b/src/plugins/csvstorage/csvstorage.c @@ -728,8 +728,35 @@ static int csvWrite (KeySet * returned, Key * parentKey, KeySet * exportKS, Key return 1; } +static bool elektraCheckForInvalidMetaKey (Key * parentKey, KeySet * ks) +{ + Key * cur = 0; + for (elektraCursor it = 0; it < ksGetSize (ks); ++it) + { + cur = ksAtCursor (ks, it); + const KeySet * metaKeys = keyMeta (cur); + for (elektraCursor jt = 0; jt < ksGetSize (metaKeys); ++jt) + { + const Key * meta = ksAtCursor (metaKeys, jt); + const char * pos = (const char *) keyName (meta); + if (elektraStrCmp (pos, "meta:/type") != 0 && elektraStrCmp (pos, "meta:/array") != 0 && + (elektraStrNCmp (pos, "meta:/internal/csvstorage", 25)) != 0) + { + ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "The Metakey %s is not supported by csvstorage", keyName (meta)); + return false; + } + } + } + return true; +} + int elektraCsvstorageSet (Plugin * handle, KeySet * returned, Key * parentKey) { + if (!elektraCheckForInvalidMetaKey (parentKey, returned)) + { + return ELEKTRA_PLUGIN_STATUS_ERROR; + } + KeySet * config = elektraPluginGetConfig (handle); Key * delimKey = ksLookupByName (config, "/delimiter", 0); char outputDelim; diff --git a/src/plugins/csvstorage/testmod_csvstorage.c b/src/plugins/csvstorage/testmod_csvstorage.c index 043fa11cf77..e82086a2648 100644 --- a/src/plugins/csvstorage/testmod_csvstorage.c +++ b/src/plugins/csvstorage/testmod_csvstorage.c @@ -84,6 +84,20 @@ static void testwriteinvalidheader (const char * file) PLUGIN_CLOSE (); } +static void testwriteInvalidMetaMustFail (const char * file) +{ + Key * parentKey = keyNew ("user:/tests/csvstorage", KEY_VALUE, srcdir_file (file), KEY_END); + KeySet * conf = ksNew (20, keyNew ("system:/delimiter", KEY_VALUE, ";", KEY_END), + keyNew ("system:/header", KEY_VALUE, "colname", KEY_END), KS_END); + KeySet * ks = ksNew (1, keyNew ("user:/tests/csvstorage", KEY_VALUE, "asdf", KEY_META, "asdf", "asdf", KEY_END), KS_END); + PLUGIN_OPEN ("csvstorage"); + succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR, + "kdbSet did not error on invalid meta key insertion"); + ksDel (ks); + keyDel (parentKey); + PLUGIN_CLOSE (); +} + static void testwritevalidemptycol (const char * file) { Key * parentKey = keyNew ("user:/tests/csvstorage", KEY_VALUE, srcdir_file (file), KEY_END); @@ -95,6 +109,7 @@ static void testwritevalidemptycol (const char * file) succeed_if (plugin->kdbGet (plugin, ks, parentKey) > 0, "call to kdbGet was not successful"); keySetString (parentKey, elektraFilename ()); succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 0, "error: couldn't write data"); + ksDel (conf); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE (); @@ -224,6 +239,7 @@ int main (int argc, char ** argv) testreadunescapedDQuote ("csvstorage/unescapedQuote.csv"); testexportmissing ("csvstorage/exporttest.csv"); testKeyMetaKeyIsSet ("csvstorage/metakey.csv"); + testwriteInvalidMetaMustFail ("csvstorage/invalid_meta_key.csv"); print_result ("testmod_csvstorage"); return nbError;