Fix memory issue about the value read by GDBM
[platform/core/api/system-info.git] / src / system_info.c
index 487aa28..b580ed4 100644 (file)
@@ -139,7 +139,13 @@ static int db_get_value(enum tag_type tag, const char *key,
                goto out;
        }
 
-       snprintf(value, len, "%s", d_data.dptr);
+       if (len <= d_data.dsize) {
+               _E("Buffer size is smaller than DB value size. It can be cut");
+               d_data.dsize = len - 1;
+       }
+
+       memcpy(value, d_data.dptr, d_data.dsize);
+       value[d_data.dsize] = '\0';
        free(d_data.dptr);
        ret = SYSTEM_INFO_ERROR_NONE;
 
@@ -235,7 +241,9 @@ static int system_info_get_type(enum tag_type tag, const char *key,
                        *type = info_type[i].type_e;
                        ret = SYSTEM_INFO_ERROR_NONE;
 
-                       g_hash_table_insert(hashtable, strdup(key_internal), strdup(d_data.dptr));
+                       g_hash_table_insert(hashtable, strndup(key_internal, d_key.dsize),
+                                       strndup(d_data.dptr, d_data.dsize));
+                       free(d_data.dptr);
                        goto out;
                }
        }
@@ -255,7 +263,7 @@ out:
 static int system_info_get_bool(enum tag_type tag, const char *key, bool *value)
 {
        int ret;
-       char val[8];
+       char val[16];
        char *valp;
        size_t len;
        const char *runtime_type;
@@ -272,14 +280,14 @@ static int system_info_get_bool(enum tag_type tag, const char *key, bool *value)
                                if (!runtime[rt].runtime_type)
                                        break;
                                if (!strncmp(runtime_type, runtime[rt].runtime_type, RT_PREFIX)) {
-                                       *value = val[runtime[rt].lang];
+                                       *value = val[runtime[rt].lang] == 'T' ? true : false;
                                        return SYSTEM_INFO_ERROR_NONE;
                                }
                        }
                        _I("Unknown RUNTIME_TYPE, return default value");
                }
 
-               *value = val[runtime[DEFAULT].lang];
+               *value = val[runtime[DEFAULT].lang] == 'T' ? true : false;
                return SYSTEM_INFO_ERROR_NONE;
        }
 
@@ -461,7 +469,7 @@ API int system_info_get_platform_type(const char *key, system_info_type_e *type)
 API int system_info_get_custom_type(const char *key, system_info_type_e *type)
 {
        int ret;
-       char *val;
+       char *val = NULL;
        size_t len;
 
        ret = system_info_get_type(TAG_TYPE_CUSTOM, key, type);
@@ -486,9 +494,11 @@ API int system_info_get_custom_type(const char *key, system_info_type_e *type)
                *type = SYSTEM_INFO_STRING;
        else {
                _E("Invalid type (%s)", val);
+               free(val);
                return SYSTEM_INFO_ERROR_INVALID_PARAMETER;
        }
 
+       free(val);
        return SYSTEM_INFO_ERROR_NONE;
        //LCOV_EXCL_STOP
 }