Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

[FLOSS H3] mozprefs: Fixing silent failing for mozprefs plugin #4772

6 changes: 5 additions & 1 deletion doc/news/_preparation_next_release.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,14 @@ 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)_
- Fix a bug where writing unknown meta keys will fail silently _(Juri Schreib @Bujuhu)_
- <<TODO>>
- <<TODO>>

### mozprefs

-Fixed bug when inserting Metakeys it would fail silently _(Nikola Prvulovic @Dynamichost96)_
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
-Fixed bug when inserting Metakeys it would fail silently _(Nikola Prvulovic @Dynamichost96)_
-Fixed bug: When inserting unknown meta keys it failed silently _(Nikola Prvulovic @Dynamichost96)_


## Libraries

The text below summarizes updates to the [C (and C++)-based libraries](https://www.libelektra.org/libraries/readme) of Elektra.
Expand Down
26 changes: 25 additions & 1 deletion src/plugins/mozprefs/mozprefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,36 @@ static void writeKey (FILE * fp, const Key * parentKey, const Key * key)
if (realPrefName) elektraFree (realPrefName);
if (argString) elektraFree (argString);
}

static bool elektraCheckForInvalidMetaKey (Key * parentKey, KeySet * keySet)
{
Key * cur = 0;
for (elektraCursor it = 0; it < ksGetSize (keySet); ++it)
{
cur = ksAtCursor (keySet, 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 (elektraStrNCmp (pos, "meta:/internal/mozprefs", 19) != 0 && elektraStrCmp (pos, "meta:/origname") && elektraStrNCmp (pos, "meta:/rename", 12) != 0 && elektraStrCmp (pos, "meta:/binary") != 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you only compare the first 19 characters in elektraStrNCmp (pos, "meta:/internal/mozprefs", 19)?

Please use a consistent coding style (elektraStrCmp (pos, "meta:/origname") was not explicitly compared against 0).

Suggested change
if (elektraStrNCmp (pos, "meta:/internal/mozprefs", 19) != 0 && elektraStrCmp (pos, "meta:/origname") && elektraStrNCmp (pos, "meta:/rename", 12) != 0 && elektraStrCmp (pos, "meta:/binary") != 0)
if (elektraStrNCmp (pos, "meta:/internal/mozprefs", 23) != 0 && elektraStrCmp (pos, "meta:/origname") != 0 && elektraStrNCmp (pos, "meta:/rename", 12) != 0 && elektraStrCmp (pos, "meta:/binary") != 0)

{
ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "The mozprefs storage Plugin doesn't support the meta key %s", pos);
return false;
}
}
}
return true;
}
int elektraMozprefsSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
// get all keys
// this function is optional

if (!elektraCheckForInvalidMetaKey (parentKey, returned))
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}

FILE * fp = fopen (keyString (parentKey), "w");
if (!fp) return -1;

Expand Down
17 changes: 16 additions & 1 deletion src/plugins/mozprefs/testmod_mozprefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ static void test_writePref (char * fileName)
PLUGIN_CLOSE ();
}

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

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

PLUGIN_OPEN ("mozprefs");
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 ();
}

int main (int argc, char ** argv)
{
printf ("PREFS TESTS\n");
Expand All @@ -74,7 +89,7 @@ int main (int argc, char ** argv)

test_readPref ("mozprefs/prefs.js");
test_writePref ("mozprefs/prefs.js");

test_setMetaMustFail()
print_result ("testmod_mozprefs");

return nbError;
Expand Down