Skip to content

Commit

Permalink
mini: fix silent failure on meta-key insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
Bujuhu committed Nov 29, 2022
1 parent 59d56a8 commit bc53d55
Show file tree
Hide file tree
Showing 5 changed files with 50 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 @@ -112,6 +112,12 @@ The following text lists news about the [plugins](https://www.libelektra.org/plu
- <<TODO>>
- <<TODO>>

### mini

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

### mmapstorage

- Remove code duplication in the data block calculation _(Richard Stöckl @Eiskasten)_
Expand Down
28 changes: 28 additions & 0 deletions src/plugins/mini/mini.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,37 @@ int elektraMiniGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * par
return parseFile (returned, parentKey);
}


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)
{
// We reach her iff we try to set a metakey. Therefore we should t
const Key * meta = ksAtCursor (metaKeys, jt);
const char * pos = (const char *) keyName (meta);
if (elektraStrNCmp (pos, "meta:/internal/mini", 19) != 0 && elektraStrCmp (pos, "meta:/origname") && elektraStrNCmp (pos, "meta:/rename", 12) != 0 && elektraStrCmp (pos, "meta:/binary") != 0)
{
ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "The mini storage Plugin doesn't support the met key %s", pos);
return false;
}
}
}
return true;
}

/** @see elektraDocSet */
int elektraMiniSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
if (!elektraCheckForInvalidMetaKey (parentKey, returned))
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}

ELEKTRA_LOG ("Write configuration data");
int errorNumber = errno;
FILE * destination = fopen (keyString (parentKey), "w");
Expand Down
16 changes: 16 additions & 0 deletions src/plugins/mini/testmod_mini.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,21 @@ static void test_set (void)
PLUGIN_CLOSE ();
}

static void test_setMetaMustFail (void)
{
char const * const prefix = "user:/mini/tests/writeMeta";

Key * parentKey = keyNew (prefix, KEY_VALUE, elektraFilename (), KEY_END);
KeySet * conf = ksNew (0, KS_END);
KeySet * ks = ksNew (1, keyNew ("user:/mini/tests/writeMeta", KEY_VALUE, "asdf", KEY_META, "asdf", "asdf", KEY_END), KS_END);

PLUGIN_OPEN ("mini");
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR, "Attempting to write a Meta Key did not fail");
keyDel (parentKey);
ksDel (ks);
PLUGIN_CLOSE ();
}

/* -- Main ------------------------------------------------------------------------------------------------------------------------------ */

int main (int argc, char ** argv)
Expand All @@ -125,6 +140,7 @@ int main (int argc, char ** argv)
test_basics ();
test_get ();
test_set ();
test_setMetaMustFail ();

print_result ("testmod_mini");

Expand Down
Empty file added stderr
Empty file.
Empty file added stdout
Empty file.

0 comments on commit bc53d55

Please sign in to comment.