service: metadata: Add check to avoid null pointer dereference

add check to avoid null pointer dereference and
reset num_kvs incase memory allocation failed

Change-Id: I0d691480d95b9dbd899da393f1e82c47a14dcafd
diff --git a/service/src/metadata.c b/service/src/metadata.c
index bcf0d51..d49ab18 100644
--- a/service/src/metadata.c
+++ b/service/src/metadata.c
@@ -165,6 +165,22 @@
 {
     int i, j;
 
+    if (!meta_data || !ckv) {
+        AGM_LOGE("Invalid params\n");
+        return;
+    }
+
+    if (!(meta_data->ckv.kv)) {
+        AGM_LOGE("metadata->ckv.kv is NULL, num_kvs=%d\n",
+                                    meta_data->ckv.num_kvs);
+        return;
+    }
+    if (!(ckv->kv)) {
+        AGM_LOGE("ckv->kv is NULL, num_kvs=%d\n",
+                                    ckv->num_kvs);
+        return;
+    }
+
     for (i = 0; i < meta_data->ckv.num_kvs; i++) {
         for (j = 0; j < ckv->num_kvs; j++) {
             if (meta_data->ckv.kv[i].key == ckv->kv[j].key) {