Skip to content

Commit 50fc293

Browse files
committed
Merge branch 'zrrto-add_null_check'
2 parents ac87cc7 + 7edd7d1 commit 50fc293

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/dictionary.c

+3
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ const char * dictionary_get(const dictionary * d, const char * key, const char *
204204
unsigned hash ;
205205
size_t i ;
206206

207+
if(d == NULL || key == NULL)
208+
return def ;
209+
207210
hash = dictionary_hash(key);
208211
for (i=0 ; i<d->size ; i++) {
209212
if (d->key[i]==NULL)

test/test_dictionary.c

+49
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,52 @@ void Test_dictionary_dump(CuTest *tc)
239239

240240
dictionary_del(dic);
241241
}
242+
243+
void Test_dictionary_get(CuTest *tc)
244+
{
245+
dictionary *dic;
246+
int i, j;
247+
char sec_name[32];
248+
char key_name[64];
249+
250+
/*NULL test*/
251+
CuAssertPtrEquals(tc, NULL, dictionary_get(NULL, NULL, NULL));
252+
CuAssertPtrEquals(tc, NULL, dictionary_get(NULL, "string", NULL));
253+
254+
/*Check the def return element*/
255+
dic = dictionary_new(DICTMINSZ);
256+
CuAssertPtrNotNull(tc, dic);
257+
CuAssertPtrEquals(tc, NULL, dictionary_get(dic, "dummy", NULL));
258+
CuAssertStrEquals(tc, "def", dictionary_get(dic, NULL, "def"));
259+
CuAssertStrEquals(tc, "def", dictionary_get(dic, "dummy", "def"));
260+
261+
/*Populate the dictionary*/
262+
for (i = 1; i < 3; ++i)
263+
{
264+
sprintf(sec_name, "sec%d", i);
265+
dictionary_set(dic, sec_name, "");
266+
for (j = 1; j < 5; ++j)
267+
{
268+
sprintf(key_name, "%s:key%d", sec_name, j);
269+
dictionary_set(dic, key_name, "dummy_value");
270+
CuAssertStrEquals(tc, "dummy_value",
271+
dictionary_get(dic, key_name, "string"));
272+
}
273+
}
274+
275+
/*Test get dictionary section value*/
276+
CuAssertStrEquals(tc, "",
277+
dictionary_get(dic, "sec1", NULL));
278+
CuAssertStrEquals(tc, "",
279+
dictionary_get(dic, "sec1", "def"));
280+
281+
/*delete and set a key in a dictionary*/
282+
dictionary_unset(dic, "sec1:key4");
283+
CuAssertStrEquals(tc, "def",
284+
dictionary_get(dic, "sec1:key4", "def"));
285+
dictionary_set(dic, "sec1:key4", "dummy_value");
286+
CuAssertStrEquals(tc, "dummy_value",
287+
dictionary_get(dic, "sec1:key4", "def"));
288+
289+
dictionary_del(dic);
290+
}

0 commit comments

Comments
 (0)