Skip to content

Commit fbde916

Browse files
Bujuhunprvulovic
authored and
nprvulovic
committed
csvstorage: fix silent failure on meta-key insertion
fixes ElektraInitiative#4586
1 parent e89f68c commit fbde916

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

doc/news/_preparation_next_release.md

+4
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ The following text lists news about the [plugins](https://www.libelektra.org/plu
184184

185185
- Fix an Issue with validationg RFC 822 date-times _(Juri Schreib @Bujuhu)_ _(Nikola Prvulovic @Dynamichost96)_
186186
- Improve Code Coverage _(Juri Schreib @Bujuhu)_ _(Nikola Prvulovic @Dynamichost96)_
187+
### csvstorage
188+
189+
- Fix a bug where writing unkown meta keys will fail silently _(Juri Schreib @Bujuhu)_
190+
- <<TODO>>
187191
- <<TODO>>
188192

189193
## Libraries

src/plugins/csvstorage/csvstorage.c

+27
Original file line numberDiff line numberDiff line change
@@ -728,8 +728,35 @@ static int csvWrite (KeySet * returned, Key * parentKey, KeySet * exportKS, Key
728728
return 1;
729729
}
730730

731+
static bool elektraCheckForInvalidMetaKey (Key * parentKey, KeySet * ks)
732+
{
733+
Key * cur = 0;
734+
for (elektraCursor it = 0; it < ksGetSize (ks); ++it)
735+
{
736+
cur = ksAtCursor (ks, it);
737+
const KeySet * metaKeys = keyMeta (cur);
738+
for (elektraCursor jt = 0; jt < ksGetSize (metaKeys); ++jt)
739+
{
740+
const Key * meta = ksAtCursor (metaKeys, jt);
741+
const char * pos = (const char *) keyName (meta);
742+
if (elektraStrCmp (pos, "meta:/type") != 0 && elektraStrCmp (pos, "meta:/array") != 0 &&
743+
(elektraStrNCmp (pos, "meta:/internal/csvstorage", 25)) != 0)
744+
{
745+
ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "The Metakey %s is not supported by csvstorage", keyName (meta));
746+
return false;
747+
}
748+
}
749+
}
750+
return true;
751+
}
752+
731753
int elektraCsvstorageSet (Plugin * handle, KeySet * returned, Key * parentKey)
732754
{
755+
if (!elektraCheckForInvalidMetaKey (parentKey, returned))
756+
{
757+
return ELEKTRA_PLUGIN_STATUS_ERROR;
758+
}
759+
733760
KeySet * config = elektraPluginGetConfig (handle);
734761
Key * delimKey = ksLookupByName (config, "/delimiter", 0);
735762
char outputDelim;

src/plugins/csvstorage/testmod_csvstorage.c

+16
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ static void testwriteinvalidheader (const char * file)
8484
PLUGIN_CLOSE ();
8585
}
8686

87+
static void testwriteInvalidMetaMustFail (const char * file)
88+
{
89+
Key * parentKey = keyNew ("user:/tests/csvstorage", KEY_VALUE, srcdir_file (file), KEY_END);
90+
KeySet * conf = ksNew (20, keyNew ("system:/delimiter", KEY_VALUE, ";", KEY_END),
91+
keyNew ("system:/header", KEY_VALUE, "colname", KEY_END), KS_END);
92+
KeySet * ks = ksNew (1, keyNew ("user:/tests/csvstorage", KEY_VALUE, "asdf", KEY_META, "asdf", "asdf", KEY_END), KS_END);
93+
PLUGIN_OPEN ("csvstorage");
94+
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR,
95+
"kdbSet did not error on invalid meta key insertion");
96+
ksDel (conf);
97+
ksDel (ks);
98+
keyDel (parentKey);
99+
PLUGIN_CLOSE ();
100+
}
101+
87102
static void testwritevalidemptycol (const char * file)
88103
{
89104
Key * parentKey = keyNew ("user:/tests/csvstorage", KEY_VALUE, srcdir_file (file), KEY_END);
@@ -224,6 +239,7 @@ int main (int argc, char ** argv)
224239
testreadunescapedDQuote ("csvstorage/unescapedQuote.csv");
225240
testexportmissing ("csvstorage/exporttest.csv");
226241
testKeyMetaKeyIsSet ("csvstorage/metakey.csv");
242+
testwriteInvalidMetaMustFail ("csvstorage/invalid_meta_key.csv");
227243
print_result ("testmod_csvstorage");
228244

229245
return nbError;

0 commit comments

Comments
 (0)