Skip to content

Commit 06aeaaa

Browse files
committed
toml: fix silent failure on meta-key insertion
fixes ElektraInitiative#4579
1 parent d63805f commit 06aeaaa

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

doc/news/_preparation_next_release.md

+6
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ The following text lists news about the [plugins](https://www.libelektra.org/plu
9797
- Fix redirect logic to not cause loops _(@stefnotch)_
9898
- <<TODO>>
9999

100+
### toml
101+
102+
- Fix bug, where meta-keys that cannot be inserted don't report an error _(@Bujuhu)_
103+
- <<TODO>>
104+
- <<TODO>>
105+
100106
### uname
101107

102108
- Add error handling if uname call fails _(Richard Stöckl @Eiskasten)_

src/plugins/toml/testmod_toml.c

+21-5
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ static const char * prefix = NULL;
145145
if (lastKey != NULL) keySetMeta (lastKey, "binary", NULL); \
146146
}
147147

148+
static bool writeFile (const char * filename, KeySet * ksWrite, int pluginStatus);
148149
static void testRoundtrip (const char * filePath);
149150
static void testRead (void);
150151
static void testReadRoot (void);
@@ -176,6 +177,7 @@ static void testWriteReadComments (void);
176177
static void testWriteReadCommentsArray (void);
177178
static void testWriteReadOrderTableNonTable (void);
178179
static void testWriteReadNull (void);
180+
static void testWriteReadBogusMetaMustError(void);
179181
// static void testWriteReadBase64(void);
180182
static Key * addKey (KeySet * ks, const char * name, const char * value, size_t size, const char * orig, const char * type,
181183
const char * array, const char * tomltype, int order);
@@ -341,6 +343,7 @@ static void testWriteRead (const char * _prefix)
341343
testWriteReadInlineTableInArray ();
342344
testWriteReadArrayInlineTableAlternating ();
343345
testWriteReadOrderTableNonTable ();
346+
testWriteReadBogusMetaMustError();
344347
prefix = NULL;
345348
}
346349

@@ -457,6 +460,19 @@ static void testWriteReadNull (void)
457460
TEST_WR_FOOT;
458461
}
459462

463+
static void testWriteReadBogusMetaMustError (void)
464+
{
465+
TEST_WR_HEAD; \
466+
WRITE_KEY ("asdf");
467+
SET_META("asdf", "asdf");
468+
writeFile("test_write_read.toml", writeKs, ELEKTRA_PLUGIN_STATUS_ERROR);
469+
keyDel (lastKey);
470+
ksDel (expectedKs); \
471+
ksDel (writeKs); \
472+
printf ("End Test: %s\n\n", __func__);
473+
remove (filename); \
474+
}
475+
460476
/*static void testWriteReadBase64 (void)
461477
{
462478
TEST_WR_HEAD;
@@ -1423,7 +1439,7 @@ static KeySet * readFile (const char * filename)
14231439
return ks;
14241440
}
14251441

1426-
static bool writeFile (const char * filename, KeySet * ksWrite)
1442+
static bool writeFile (const char * filename, KeySet * ksWrite, int pluginStatus)
14271443
{
14281444
bool success = true;
14291445
ELEKTRA_LOG_DEBUG ("Writing '%s'\n", filename);
@@ -1433,8 +1449,8 @@ static bool writeFile (const char * filename, KeySet * ksWrite)
14331449
PLUGIN_OPEN ("toml");
14341450

14351451
int setStatus = plugin->kdbSet (plugin, ksWrite, parentKey);
1436-
succeed_if (setStatus == ELEKTRA_PLUGIN_STATUS_SUCCESS, "Could not write keys");
1437-
if (setStatus != ELEKTRA_PLUGIN_STATUS_SUCCESS)
1452+
succeed_if (setStatus == pluginStatus, "Writing keys did not return with expected Status");
1453+
if (setStatus != pluginStatus)
14381454
{
14391455
output_error (parentKey);
14401456
success = false;
@@ -1448,7 +1464,7 @@ static void testWriteReadCompare (KeySet * ksWrite, KeySet * expected)
14481464
{
14491465
const char * filename = "test_write_read.toml";
14501466

1451-
if (writeFile (filename, ksWrite))
1467+
if (writeFile (filename, ksWrite, ELEKTRA_PLUGIN_STATUS_SUCCESS))
14521468
{
14531469
{
14541470
KeySet * ksRead = readFile (filename);
@@ -1469,7 +1485,7 @@ static bool roundtripFile (const char * filenameIn, const char * filenameOut)
14691485
{
14701486
return false;
14711487
}
1472-
bool success = writeFile (filenameOut, ksRead);
1488+
bool success = writeFile (filenameOut, ksRead, ELEKTRA_PLUGIN_STATUS_SUCCESS);
14731489
ksDel (ksRead);
14741490
return success;
14751491
}

src/plugins/toml/write.c

+9
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,15 @@ static int collectComments (CommentList ** comments, Key * key, Writer * writer)
858858
subDepth++;
859859
pos += elektraStrLen (pos);
860860
}
861+
//Accept valid metakeys, throw error for everything else
862+
} else if(elektraStrCmp(pos, "order") != 0 &&
863+
elektraStrCmp(pos, "type") != 0 &&
864+
elektraStrCmp(pos, "tomltype") != 0 &&
865+
elektraStrCmp(pos, "origvalue") != 0 &&
866+
elektraStrCmp(pos, "binary") != 0 &&
867+
elektraStrCmp(pos, "array") != 0) {
868+
ELEKTRA_SET_RESOURCE_ERRORF(key, "The Metakey %s is not supported by TOML", keyString(meta));
869+
return -1;
861870
}
862871
}
863872
*comments = commentRoot;

0 commit comments

Comments
 (0)