Skip to content

Commit

Permalink
csvstorage: fix silent failure on meta-key insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
Bujuhu committed Dec 11, 2022
1 parent 6a6d1bf commit 63fabe1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions doc/news/_preparation_next_release.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ The following text lists news about the [plugins](https://www.libelektra.org/plu
- <<TODO>>
- <<TODO>>

### csvstorage

- Fix a bug where writing unkown meta keys will fail silently _(Juri Schreib @Bujuhu)_
- <<TODO>>
- <<TODO>>

## Libraries

The text below summarizes updates to the [C (and C++)-based libraries](https://www.libelektra.org/libraries/readme) of Elektra.
Expand Down
27 changes: 27 additions & 0 deletions src/plugins/csvstorage/csvstorage.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions src/plugins/csvstorage/testmod_csvstorage.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 ();
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 63fabe1

Please sign in to comment.