summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYaniv Kaul <ykaul@redhat.com>2018-08-24 17:24:15 +0300
committerAmar Tumballi <amarts@redhat.com>2018-08-30 05:37:48 +0000
commitd27e8870c1882b1abfffefaf7b40bc7f2a1ec05f (patch)
tree1cc0c618ac506383ed7494e10b9d6682f3db1a4d
parentf29e46f97737d9e2d154ba0e99c5ffb1d03eaa57 (diff)
libglusterfs/src/dict.c: allocate memory accurately.
I suspect we've allocated and then set to '\0' an additional byte for no reason: gf_asprintf() allocates enough inc. the terminating null; data->len took that additional byte into account; memcpy() it back took care of that terminating null byte as well. Also, unrelated, implemented str_to_data() via strn_to_data(). Compile-tested only! updates: bz#1193929 Signed-off-by: Yaniv Kaul <ykaul@redhat.com> Change-Id: I32c90e12974f51a0fbc0cfaebf9bd0fd722adc11
-rw-r--r--libglusterfs/src/dict.c31
1 files changed, 8 insertions, 23 deletions
diff --git a/libglusterfs/src/dict.c b/libglusterfs/src/dict.c
index 57be4e76d63..b69f0e79a47 100644
--- a/libglusterfs/src/dict.c
+++ b/libglusterfs/src/dict.c
@@ -1018,18 +1018,8 @@ str_to_data (char *value)
LG_MSG_INVALID_ARG, "value is NULL");
return NULL;
}
- data_t *data = get_new_data ();
-
- if (!data) {
- return NULL;
- }
- data->len = strlen (value) + 1;
- data->data_type = GF_DATA_TYPE_STR;
-
- data->data = value;
- data->is_static = 1;
- return data;
+ return strn_to_data(value, strlen (value));
}
data_t *
@@ -1127,12 +1117,12 @@ data_to_int64 (data_t *data)
{
VALIDATE_DATA_AND_LOG(data, GF_DATA_TYPE_INT, "null", -1);
- char *str = alloca (data->len + 1);
+ char *str = alloca (data->len);
if (!str)
return -1;
memcpy (str, data->data, data->len);
- str[data->len] = '\0';
+
return (int64_t) strtoull (str, NULL, 0);
}
@@ -1141,12 +1131,11 @@ data_to_int32 (data_t *data)
{
VALIDATE_DATA_AND_LOG(data, GF_DATA_TYPE_INT, "null", -1);
- char *str = alloca (data->len + 1);
+ char *str = alloca (data->len);
if (!str)
return -1;
memcpy (str, data->data, data->len);
- str[data->len] = '\0';
return strtoul (str, NULL, 0);
}
@@ -1157,12 +1146,11 @@ data_to_int16 (data_t *data)
VALIDATE_DATA_AND_LOG(data, GF_DATA_TYPE_INT, "null", -1);
int16_t value = 0;
- char *str = alloca (data->len + 1);
+ char *str = alloca (data->len);
if (!str)
return -1;
memcpy (str, data->data, data->len);
- str[data->len] = '\0';
errno = 0;
value = strtol (str, NULL, 0);
@@ -1212,12 +1200,11 @@ data_to_uint64 (data_t *data)
{
VALIDATE_DATA_AND_LOG(data, GF_DATA_TYPE_UINT, "null", -1);
- char *str = alloca (data->len + 1);
+ char *str = alloca (data->len);
if (!str)
return -1;
memcpy (str, data->data, data->len);
- str[data->len] = '\0';
return strtoll (str, NULL, 0);
}
@@ -1227,12 +1214,11 @@ data_to_uint32 (data_t *data)
{
VALIDATE_DATA_AND_LOG(data, GF_DATA_TYPE_UINT, "null", -1);
- char *str = alloca (data->len + 1);
+ char *str = alloca (data->len);
if (!str)
return -1;
memcpy (str, data->data, data->len);
- str[data->len] = '\0';
return strtol (str, NULL, 0);
}
@@ -1243,12 +1229,11 @@ data_to_uint16 (data_t *data)
VALIDATE_DATA_AND_LOG(data, GF_DATA_TYPE_UINT, "null", -1);
uint16_t value = 0;
- char *str = alloca (data->len + 1);
+ char *str = alloca (data->len);
if (!str)
return -1;
memcpy (str, data->data, data->len);
- str[data->len] = '\0';
errno = 0;
value = strtol (str, NULL, 0);