Skip to content

Commit 9a3413f

Browse files
authored
Merge pull request ElektraInitiative#3183 from ElektraInitiative/fix_memleak
Fix memleak
2 parents 7aeec3b + a63452e commit 9a3413f

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

benchmarks/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ endmacro (do_benchmark)
3333
do_benchmark (large)
3434
do_benchmark (cmp)
3535
do_benchmark (createkeys)
36+
do_benchmark (memoryleak)
3637

3738
# exclude storage and KDB benchmark from mingw
3839
if (NOT WIN32)

benchmarks/memoryleak.c

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @file
3+
*
4+
* @brief Benchmark for KDB
5+
*
6+
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7+
*/
8+
9+
#include <stdio.h>
10+
11+
#include <benchmarks.h>
12+
#include <kdb.h>
13+
#include <unistd.h>
14+
15+
#define NUM_RUNS 30
16+
17+
int main (void)
18+
{
19+
KDB * handles[NUM_RUNS];
20+
KeySet * keysets[NUM_RUNS];
21+
22+
Key * parentKey = keyNew ("user", KEY_END);
23+
24+
for (size_t i = 0; i < NUM_RUNS; ++i)
25+
{
26+
KDB * handle = kdbOpen (parentKey);
27+
28+
KeySet * ks = ksNew (0, KS_END);
29+
30+
kdbGet (handle, ks, parentKey);
31+
32+
printf ("Retrieved %d keys\n", (int) ksGetSize (ks));
33+
34+
handles[i] = handle;
35+
keysets[i] = ks;
36+
}
37+
38+
for (size_t i = 0; i < NUM_RUNS; ++i)
39+
{
40+
printf ("Freeing %d keys\n", (int) ksGetSize (keysets[i]));
41+
42+
kdbClose (handles[i], parentKey);
43+
ksDel (keysets[i]);
44+
}
45+
46+
keyDel (parentKey);
47+
}

doc/news/_preparation_next_release.md

+1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ The text below summarizes updates to the [C (and C++)-based libraries](https://w
153153
- `kdbconfig.h` is no longer included in the installed headers. This is because it could cause conflicts with other
154154
`config.h`-type headers from applications. _(Klemens Böswirth)_
155155
- `ksAppendKey`: state that it only fail on memory problems. _(Markus Raab)_
156+
- Fix memory leak in `kdbGet`. _(Markus Raab)_
156157
- Implemented `kdberrors.h` directly without generation of the `specification` file because of drastically reduced error code count _(Michael Zronek)_
157158
- `keyIsDirectBelow` was renamed to `keyIsDirectlyBelow`. _(Philipp Gackstatter)_
158159
- `keyMeta` was added to provide access to a key's underlying KeySet that holds its metadata keys. _(Philipp Gackstatter)_

src/libs/elektra/kdb.c

+1
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ KDB * kdbOpen (Key * errorKey)
333333
keySetString (errorKey, keyString (initialParent));
334334
keyDel (initialParent);
335335
errno = errnosave;
336+
ksDel (keys);
336337
return 0;
337338
case 0:
338339
ELEKTRA_ADD_INSTALLATION_WARNING (errorKey, "Initial 'kdbGet()' failed, you should either fix " KDB_DB_INIT

0 commit comments

Comments
 (0)