Skip to content

Commit

Permalink
mark keyNew with __attribute__ ((sentinel)) and fix resulting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kodebach committed Mar 3, 2021
1 parent 355b6a0 commit 4612324
Show file tree
Hide file tree
Showing 18 changed files with 683 additions and 669 deletions.
4 changes: 3 additions & 1 deletion doc/news/_preparation_next_release.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ The text below summarizes updates to the [C (and C++)-based libraries](https://w
- <<TODO>>
- We added `keyReplacePrefix`, a function that allows you to easily move a key from one parent to another. _(Klemens Böswirth)_
- `kdbEnsure` was removed and replaced by similar functionality added to `kdbOpen`. _[see above](#hl-1)_ _(Klemens Böswirth)_
- <<TODO>>
- `KEY_END` is now defined as `(void *) 0` instead of `0`. This allows us to mark `keyNew` with the GCC attribute
`__attribute__ ((sentinel))`, which causes a compiler warning, if `keyNew` calls don't use `KEY_END` as their last argument.
_(Klemens Böswirth)_
### Io
Expand Down
6 changes: 3 additions & 3 deletions examples/keyNew.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static void shortExamples(void)
{
{
//! [Simple]
Key *k = keyNew(0);
Key *k = keyNew("/", KEY_END);
// work with it
keyDel (k);
//! [Simple]
Expand Down Expand Up @@ -131,7 +131,7 @@ ksDel(ks2); // k is now deleted
}{

//! [Ref]
Key *k = keyNew(0); // ref counter = 0
Key *k = keyNew("/", KEY_END); // ref counter = 0
keyIncRef(k); // ref counter = 1
keyDel(k); // key will not be deleted
keyDecRef(k);
Expand All @@ -141,7 +141,7 @@ keyDel(k);
}{

//! [Multi Ref]
Key *k = keyNew(0); // ref counter 0
Key *k = keyNew("/", KEY_END); // ref counter 0
keyIncRef(k); // ref counter of key 1
keyDel (k); // has no effect
keyIncRef(k); // ref counter of key 2
Expand Down
2 changes: 1 addition & 1 deletion examples/ksIterate.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void iterate_while (KeySet * ks)

int main (void)
{
KeySet * ks = ksNew (20, keyNew ("user:/name1", 0), keyNew ("user:/name2", 0), keyNew ("user:/name3", 0), KS_END);
KeySet * ks = ksNew (20, keyNew ("user:/name1", KEY_END), keyNew ("user:/name2", KEY_END), keyNew ("user:/name3", KEY_END), KS_END);
iterate_while (ks);
iterate_for (ks);
}
16 changes: 8 additions & 8 deletions examples/ksNew.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,28 @@ ksDel (keys);
// Create KeySet without allocating memory for keys
KeySet * keys = ksNew (0, KS_END);
// The first allocation will happen in ksAppendKey
ksAppendKey(keys, keyNew ("user:/sw/org/app/#0/current/fixedConfiguration/key02", KEY_VALUE, "value02", 0));
ksAppendKey(keys, keyNew ("user:/sw/org/app/#0/current/fixedConfiguration/key02", KEY_VALUE, "value02", KEY_END));
// work with the KeySet
ksDel (keys);
//! [No Allocation]
}

{
//! [Length 15]
KeySet * keys = ksNew (15, keyNew ("user:/sw/org/app/#0/current/fixedConfiguration/key01", KEY_VALUE, "value01", 0),
keyNew ("user:/sw/org/app/#0/current/fixedConfiguration/key02", KEY_VALUE, "value02", 0),
keyNew ("user:/sw/org/app/#0/current/fixedConfiguration/key03", KEY_VALUE, "value03", 0),
KeySet * keys = ksNew (15, keyNew ("user:/sw/org/app/#0/current/fixedConfiguration/key01", KEY_VALUE, "value01", KEY_END),
keyNew ("user:/sw/org/app/#0/current/fixedConfiguration/key02", KEY_VALUE, "value02", KEY_END),
keyNew ("user:/sw/org/app/#0/current/fixedConfiguration/key03", KEY_VALUE, "value03", KEY_END),
// ...
keyNew ("user:/sw/org/app/#0/current/fixedConfiguration/key15", KEY_VALUE, "value15", 0), KS_END);
keyNew ("user:/sw/org/app/#0/current/fixedConfiguration/key15", KEY_VALUE, "value15", KEY_END), KS_END);
// work with it
ksDel (keys);
//! [Length 15]
}
{
//! [Hint 500]
KeySet * config = ksNew (500, keyNew ("user:/sw/org/app/#0/current/fixedConfiguration/key1", KEY_VALUE, "value1", 0),
keyNew ("user:/sw/org/app/#0/current/fixedConfiguration/key2", KEY_VALUE, "value2", 0),
keyNew ("user:/sw/org/app/#0/current/fixedConfiguration/key3", KEY_VALUE, "value3", 0),
KeySet * config = ksNew (500, keyNew ("user:/sw/org/app/#0/current/fixedConfiguration/key1", KEY_VALUE, "value1", KEY_END),
keyNew ("user:/sw/org/app/#0/current/fixedConfiguration/key2", KEY_VALUE, "value2", KEY_END),
keyNew ("user:/sw/org/app/#0/current/fixedConfiguration/key3", KEY_VALUE, "value3", KEY_END),
KS_END); // don't forget the KS_END at the end!
// work with it
ksDel (config);
Expand Down
4 changes: 2 additions & 2 deletions examples/ksNewExample.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ int main (void)
//! [Full Example]
// create a new keyset with 3 keys
// with a hint that about 20 keys will be inside
KeySet * myConfig = ksNew (20, keyNew ("user:/name1", 0), keyNew ("user:/name2", 0), keyNew ("user:/name3", 0), KS_END);
KeySet * myConfig = ksNew (20, keyNew ("user:/name1", KEY_END), keyNew ("user:/name2", KEY_END), keyNew ("user:/name3", KEY_END), KS_END);
// append a key in the keyset
ksAppendKey (myConfig, keyNew ("user:/name4", 0));
ksAppendKey (myConfig, keyNew ("user:/name4", KEY_END));

Key * current;
ksRewind (myConfig);
Expand Down
2 changes: 1 addition & 1 deletion examples/set_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ int main (void)
KeySet * ks = ksNew (0, KS_END);
kdbGet (kdb_handle, ks, top);

Key * key = keyNew (0);
Key * key = keyNew ("/", KEY_END);
keySetName (key, "user:/sw/MyApp/Tests/TestKey1"); // == 31
keySetString (key, "NULLTestValue"); // == 14
keySetMeta (key, "comment", "NULLTestComment"); // == 16
Expand Down
4 changes: 3 additions & 1 deletion src/bindings/glib/gelektra-key.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ typedef struct _GElektraKeyClass GElektraKeyClass;
// clang-format off
typedef enum {
GELEKTRA_KEY_FLAGS = KEY_FLAGS,
GELEKTRA_KEY_END = KEY_END,
GELEKTRA_KEY_NAME = KEY_NAME,
GELEKTRA_KEY_VALUE = KEY_VALUE,
GELEKTRA_KEY_COMMENT = KEY_COMMENT,
Expand All @@ -32,6 +31,9 @@ typedef enum {
GELEKTRA_KEY_META = KEY_META,
GELEKTRA_KEY_NULL = KEY_NULL
} GElektraKeySwitch;

#define GELEKTRA_KEY_END KEY_END

// clang-format on

struct _GElektraKey
Expand Down
10 changes: 8 additions & 2 deletions src/include/kdb.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,14 @@ enum {
KEY_LOCK_NAME=1<<17,
KEY_LOCK_VALUE=1<<18,
KEY_LOCK_META=1<<19,
KEY_END=0,
};

#ifdef __cplusplus
#define KEY_END (static_cast<void *> (0))
#else
#define KEY_END ((void *) 0)
#endif

typedef int elektraLockFlags;
typedef int elektraKeyFlags;

Expand Down Expand Up @@ -134,7 +140,7 @@ int elektraGOptsContractFromStrings (KeySet * contract, size_t argsSize, const c
**************************************/

/* Basic Methods */
Key *keyNew(const char *keyname, ...);
Key *keyNew(const char *keyname, ...) ELEKTRA_SENTINEL;
Key *keyVNew(const char *keyname, va_list ap);

Key * keyCopy(Key *dest, const Key *source, elektraCopyFlags flags);
Expand Down
8 changes: 4 additions & 4 deletions src/libs/elektra/contracts.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ int elektraGOptsContract (KeySet * contract, int argc, const char * const * argv
ksAppendKey (contract, keyNew ("system:/elektra/contract/mountglobal/gopts", KEY_END));
if (goptsConfig != NULL)
{
Key * configRoot = keyNew ("user:/");
Key * contractRoot = keyNew ("system:/elektra/contract/mountglobal/gopts");
Key * configRoot = keyNew ("user:/", KEY_END);
Key * contractRoot = keyNew ("system:/elektra/contract/mountglobal/gopts", KEY_END);

elektraCursor end;
for (elektraCursor it = ksFindHierarchy (goptsConfig, configRoot, &end); it < end; it++)
Expand Down Expand Up @@ -100,8 +100,8 @@ int elektraGOptsContractFromStrings (KeySet * contract, size_t argsSize, const c
ksAppendKey (contract, keyNew ("system:/elektra/contract/mountglobal/gopts", KEY_END));
if (goptsConfig != NULL)
{
Key * configRoot = keyNew ("user:/");
Key * contractRoot = keyNew ("system:/elektra/contract/mountglobal/gopts");
Key * configRoot = keyNew ("user:/", KEY_END);
Key * contractRoot = keyNew ("system:/elektra/contract/mountglobal/gopts", KEY_END);

elektraCursor end;
for (elektraCursor it = ksFindHierarchy (goptsConfig, configRoot, &end); it < end; it++)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/crypto/testmod_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static KeySet * newTestdataKeySet (void)

static inline void setPluginShutdown (KeySet * config)
{
ksAppendKey (config, keyNew (ELEKTRA_CRYPTO_PARAM_SHUTDOWN, KEY_VALUE, "1", 0));
ksAppendKey (config, keyNew (ELEKTRA_CRYPTO_PARAM_SHUTDOWN, KEY_VALUE, "1", KEY_END));
}

static KeySet * newPluginConfiguration (void)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/csvstorage/csvstorage.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ static int csvRead (KeySet * returned, Key * parentKey, char delim, Key * colAsP
Key * lookupKey = keyNew (keyName (dirKey), KEY_END);
keyAddName (lookupKey, keyString (colAsParent));
Key * indexKey = ksLookupByName (tmpKs, keyName (lookupKey), 0);
Key * renameKey = keyNew (keyName (dirKey), 0);
Key * renameKey = keyNew (keyName (dirKey), KEY_END);
keySetBaseName (renameKey, keyString (indexKey));
ksRewind (tmpKs);
KeySet * renamedKs = ksRenameKeys (tmpKs, keyName (renameKey));
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/list/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ static int runPlugins (KeySet * pluginKS, KeySet * modules, KeySet * plugins, Ke
}
else
{
Key * userCutPoint = keyNew ("user:/", 0);
Key * globalConfCutPoint = keyNew ("/config", 0);
Key * userCutPoint = keyNew ("user:/", KEY_END);
Key * globalConfCutPoint = keyNew ("/config", KEY_END);
KeySet * config = ksDup (configOrig);
KeySet * globalConfigAll = ksCut (config, globalConfCutPoint);
KeySet * userConfigAll = ksCut (config, userCutPoint);
Expand All @@ -339,7 +339,7 @@ static int runPlugins (KeySet * pluginKS, KeySet * modules, KeySet * plugins, Ke
ksAppend (pluginConfigWithConfigPrefix, globalPluginConfig);
ksDel (globalPluginConfig);
// remove "placements" from plugin config
Key * toRemove = keyNew ("user:/placements", 0);
Key * toRemove = keyNew ("user:/placements", KEY_END);
ksDel (ksCut (pluginConfigWithConfigPrefix, toRemove));
ksRewind (pluginConfigWithConfigPrefix);
ksDel (globalConfigAll);
Expand Down
25 changes: 14 additions & 11 deletions src/plugins/xmltool/testmod_xmltool.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,44 +209,47 @@ void test_keyset (void)
static void test_ksCommonParentName (void)
{
char ret[MAX_SIZE + 1];
KeySet * ks = ksNew (
10, keyNew ("system:/sw/xorg/Monitors/Monitor1/vrefresh", 0), keyNew ("system:/sw/xorg/Monitors/Monitor1/hrefresh", 0),
keyNew ("system:/sw/xorg/Monitors/Monitor2/vrefresh", 0), keyNew ("system:/sw/xorg/Monitors/Monitor2/hrefresh", 0),
keyNew ("system:/sw/xorg/Devices/Device1/driver", 0), keyNew ("system:/sw/xorg/Devices/Device1/mode", 0), KS_END);
KeySet * ks = ksNew (10, keyNew ("system:/sw/xorg/Monitors/Monitor1/vrefresh", KEY_END),
keyNew ("system:/sw/xorg/Monitors/Monitor1/hrefresh", KEY_END),
keyNew ("system:/sw/xorg/Monitors/Monitor2/vrefresh", KEY_END),
keyNew ("system:/sw/xorg/Monitors/Monitor2/hrefresh", KEY_END),
keyNew ("system:/sw/xorg/Devices/Device1/driver", KEY_END),
keyNew ("system:/sw/xorg/Devices/Device1/mode", KEY_END), KS_END);

printf ("Test common parentname\n");

succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) > 0, "could not find correct parentname");
succeed_if_same_string (ret, "system:/sw/xorg");
ksDel (ks);

ks = ksNew (10, keyNew ("system:/", 0), keyNew ("user:/", 0), KS_END);
ks = ksNew (10, keyNew ("system:/", KEY_END), keyNew ("user:/", KEY_END), KS_END);
succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) == 0, "could find correct parentname");
succeed_if_same_string (ret, "");
ksDel (ks);

ks = ksNew (10, keyNew ("system:/some/thing", 0), keyNew ("system:/other/thing", 0), KS_END);
ks = ksNew (10, keyNew ("system:/some/thing", KEY_END), keyNew ("system:/other/thing", KEY_END), KS_END);
succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) == 9, "could find correct parentname");
succeed_if_same_string (ret, "system:/");
ksDel (ks);

ks = ksNew (10, keyNew ("system:/some/thing", 0), keyNew ("system:/something", 0), KS_END);
ks = ksNew (10, keyNew ("system:/some/thing", KEY_END), keyNew ("system:/something", KEY_END), KS_END);
succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) == 9, "could find correct parentname");
succeed_if_same_string (ret, "system:/");
ksDel (ks);

ks = ksNew (10, keyNew ("system:/here/in/deep/goes/ok/thing", 0), keyNew ("system:/here/in/deep/goes/ok/other/thing", 0), KS_END);
ks = ksNew (10, keyNew ("system:/here/in/deep/goes/ok/thing", KEY_END),
keyNew ("system:/here/in/deep/goes/ok/other/thing", KEY_END), KS_END);
succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) > 0, "could find correct parentname");
succeed_if_same_string (ret, "system:/here/in/deep/goes/ok");
ksDel (ks);

ks = ksNew (10, keyNew ("system:/here/in/deep/goes/ok/thing", 0), keyNew ("system:/here/in/deep/goes/ok/other/thing", 0),
keyNew ("user:/unique/thing", 0), KS_END);
ks = ksNew (10, keyNew ("system:/here/in/deep/goes/ok/thing", KEY_END),
keyNew ("system:/here/in/deep/goes/ok/other/thing", KEY_END), keyNew ("user:/unique/thing", KEY_END), KS_END);
succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) == 0, "could find correct parentname");
succeed_if_same_string (ret, "");
ksDel (ks);

ks = ksNew (10, keyNew ("user:/unique/thing", 0), KS_END);
ks = ksNew (10, keyNew ("user:/unique/thing", KEY_END), KS_END);
succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) > 0, "could find correct parentname");
succeed_if_same_string (ret, "user:/unique/thing");
ksDel (ks);
Expand Down
16 changes: 8 additions & 8 deletions tests/abi/testabi_ks.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ static void test_ksNew (void)
// succeed_if(ksGetAlloc(keys) == 15, "allocation size wrong");
succeed_if (ksDel (keys) == 0, "could not delete keyset");

config = ksNew (100, keyNew ("user:/sw/app/fixedConfiguration/key1", KEY_VALUE, "value1", 0),
keyNew ("user:/sw/app/fixedConfiguration/key2", KEY_VALUE, "value2", 0),
keyNew ("user:/sw/app/fixedConfiguration/key3", KEY_VALUE, "value3", 0), KS_END);
config = ksNew (100, keyNew ("user:/sw/app/fixedConfiguration/key1", KEY_VALUE, "value1", KEY_END),
keyNew ("user:/sw/app/fixedConfiguration/key2", KEY_VALUE, "value2", KEY_END),
keyNew ("user:/sw/app/fixedConfiguration/key3", KEY_VALUE, "value3", KEY_END), KS_END);
succeed_if (ksGetSize (config) == 3, "could not append 3 keys in keyNew");
// this behaviour might change, do not build on it,
// and there is no compatible way to get the alloc info
Expand All @@ -64,14 +64,14 @@ static void test_ksNew (void)
// succeed_if(ksGetAlloc(config) == 15, "allocation size wrong");
succeed_if (ksDel (config) == 0, "could not delete keyset");

config = ksNew (10, keyNew ("user:/sw/app/fixedConfiguration/key1", KEY_VALUE, "value1", 0),
keyNew ("user:/sw/app/fixedConfiguration/key2", KEY_VALUE, "value2", 0),
keyNew ("user:/sw/app/fixedConfiguration/key3", KEY_VALUE, "value1", 0),
keyNew ("user:/sw/app/fixedConfiguration/key4", KEY_VALUE, "value3", 0), KS_END);
config = ksNew (10, keyNew ("user:/sw/app/fixedConfiguration/key1", KEY_VALUE, "value1", KEY_END),
keyNew ("user:/sw/app/fixedConfiguration/key2", KEY_VALUE, "value2", KEY_END),
keyNew ("user:/sw/app/fixedConfiguration/key3", KEY_VALUE, "value1", KEY_END),
keyNew ("user:/sw/app/fixedConfiguration/key4", KEY_VALUE, "value3", KEY_END), KS_END);

succeed_if (ksGetSize (config) == 4, "could not append 5 keys in keyNew");
// succeed_if(ksGetAlloc(config) == 15, "allocation size wrong");
ksAppendKey (config, keyNew ("user:/sw/app/fixedConfiguration/key6", KEY_VALUE, "value4", 0));
ksAppendKey (config, keyNew ("user:/sw/app/fixedConfiguration/key6", KEY_VALUE, "value4", KEY_END));

ksClear (ks2);
ksCopy (ks2, config);
Expand Down
5 changes: 3 additions & 2 deletions tests/ctest/test_ks.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ static void test_cascadingLookup (void)
Key * k1;
Key * k2;
Key * k3;
KeySet * ks = ksNew (10, k0 = keyNew ("system:/benchmark/override/#0", 0), k1 = keyNew ("system:/benchmark/override/#1", 0),
k2 = keyNew ("user:/benchmark/override/#2", 0), k3 = keyNew ("user:/benchmark/override/#3", 0), KS_END);
KeySet * ks =
ksNew (10, k0 = keyNew ("system:/benchmark/override/#0", KEY_END), k1 = keyNew ("system:/benchmark/override/#1", KEY_END),
k2 = keyNew ("user:/benchmark/override/#2", KEY_END), k3 = keyNew ("user:/benchmark/override/#3", KEY_END), KS_END);
Key * search = keyNew ("/benchmark/override/#0", KEY_END);
Key * found = ksLookup (ks, search, 0);
succeed_if (found == k0, "found wrong key");
Expand Down
16 changes: 8 additions & 8 deletions tests/ctest/test_order.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ static void test_ksNew (void)
succeed_if (ksGetAlloc (keys) == 15, "allocation size wrong");
succeed_if (ksDel (keys) == 0, "could not delete keyset");

config = ksNew (100, keyNew ("user:/sw/app/fixedConfiguration/key1", KEY_VALUE, "value1", 0),
keyNew ("user:/sw/app/fixedConfiguration/key2", KEY_VALUE, "value2", 0),
keyNew ("user:/sw/app/fixedConfiguration/key3", KEY_VALUE, "value3", 0), KS_END);
config = ksNew (100, keyNew ("user:/sw/app/fixedConfiguration/key1", KEY_VALUE, "value1", KEY_END),
keyNew ("user:/sw/app/fixedConfiguration/key2", KEY_VALUE, "value2", KEY_END),
keyNew ("user:/sw/app/fixedConfiguration/key3", KEY_VALUE, "value3", KEY_END), KS_END);
succeed_if (ksGetSize (config) == 3, "could not append 3 keys in keyNew");
succeed_if (ksGetAlloc (config) == 100, "allocation size wrong");
keyDel (ksPop (config));
Expand All @@ -45,14 +45,14 @@ static void test_ksNew (void)
succeed_if (ksGetAlloc (config) == 15, "allocation size wrong");
succeed_if (ksDel (config) == 0, "could not delete keyset");

config = ksNew (17, keyNew ("user:/sw/app/fixedConfiguration/key1", KEY_VALUE, "value1", 0),
keyNew ("user:/sw/app/fixedConfiguration/key2", KEY_VALUE, "value2", 0),
keyNew ("user:/sw/app/fixedConfiguration/key3", KEY_VALUE, "value1", 0),
keyNew ("user:/sw/app/fixedConfiguration/key4", KEY_VALUE, "value3", 0), KS_END);
config = ksNew (17, keyNew ("user:/sw/app/fixedConfiguration/key1", KEY_VALUE, "value1", KEY_END),
keyNew ("user:/sw/app/fixedConfiguration/key2", KEY_VALUE, "value2", KEY_END),
keyNew ("user:/sw/app/fixedConfiguration/key3", KEY_VALUE, "value1", KEY_END),
keyNew ("user:/sw/app/fixedConfiguration/key4", KEY_VALUE, "value3", KEY_END), KS_END);

succeed_if (ksGetSize (config) == 4, "could not append 5 keys in keyNew");
succeed_if (ksGetAlloc (config) == 17, "allocation size wrong");
ksAppendKey (config, keyNew ("user:/sw/app/fixedConfiguration/key6", KEY_VALUE, "value4", 0));
ksAppendKey (config, keyNew ("user:/sw/app/fixedConfiguration/key6", KEY_VALUE, "value4", KEY_END));

ksClear (ks2);
ksCopy (ks2, config);
Expand Down
Loading

0 comments on commit 4612324

Please sign in to comment.