From 45d9dec31c7af129eded87785b53ad78d3556b51 Mon Sep 17 00:00:00 2001 From: "pr.jung" Date: Thu, 24 Jan 2019 16:21:48 +0900 Subject: [PATCH] Change strtol to strtoul Change-Id: I65a94fcaf0fc4a737ffc64e498a9b2f9c3cc7379 Signed-off-by: pr.jung --- src/haptic/standard-vibcore.c | 132 ++++++++++++++++++++++++------------------ 1 file changed, 75 insertions(+), 57 deletions(-) diff --git a/src/haptic/standard-vibcore.c b/src/haptic/standard-vibcore.c index eb2403d..09ae445 100644 --- a/src/haptic/standard-vibcore.c +++ b/src/haptic/standard-vibcore.c @@ -68,9 +68,6 @@ static int insert_conf_data(dd_list **conf_data, struct duration_data *update) { struct duration_data *data; - if (update->duration < 0 || update->intensity < 0) - return 0; - data = (struct duration_data *)calloc(1, sizeof(struct duration_data)); if (!data) { _E("not enough memory"); @@ -86,25 +83,29 @@ static int insert_conf_data(dd_list **conf_data, struct duration_data *update) static void get_pattern_property(char **iter, char property, int *value) { - char *check = strchr(*iter, property); - long int val; + char *check; + unsigned long int val; + check = strchr(*iter, property); if (!check) return; *check = '\0'; - val = strtol(*iter, NULL, 10); - if (val < 0) + val = strtoul(*iter, NULL, 10); + if (errno == EINVAL || errno == ERANGE) { val = 0; - else if (val > VIB_LOCK_TIMEOUT_MAX) + _E("Failed to get value of %s: %d", *iter, errno); + } + if (val > VIB_LOCK_TIMEOUT_MAX) val = VIB_LOCK_TIMEOUT_MAX; *value = (int)val; *iter = check + 1; } + /* [A]xxxDxxxIxxxFxxxOxxxW format */ -static int insert_raw_data_format(dd_list **conf_data, const char *value) +static int insert_raw_data_format(dd_list **conf_data, char *value) { struct duration_data update = {0, }; char *iter; @@ -113,24 +114,26 @@ static int insert_raw_data_format(dd_list **conf_data, const char *value) if (!value) return insert_conf_data(conf_data, &update); + if (!conf_data) { + _E("Invalid parameter: Configuration list is null"); + return -EINVAL; + } - iter = strdup(value); + iter = value; end = iter + strlen(iter); while (iter < end) { memset(&update, 0, sizeof(struct duration_data)); get_pattern_property(&iter, 'D', &update.duration); - if (update.duration > VIB_LOCK_TIMEOUT_MAX) - update.duration = VIB_LOCK_TIMEOUT_MAX; get_pattern_property(&iter, 'I', &update.intensity); if (update.intensity > INTENSITY_BASE_RATE) update.intensity = INTENSITY_BASE_RATE; get_pattern_property(&iter, 'W', &update.wait); - if (update.wait > VIB_LOCK_TIMEOUT_MAX) - update.wait = VIB_LOCK_TIMEOUT_MAX; - if (update.duration == 0 && update.wait == 0) + if (update.duration == 0 && update.wait == 0) { + _D("Pattern duration is zero."); break; + } pattern_duration += (update.duration + update.wait); if (pattern_duration > VIB_LOCK_TIMEOUT_MAX) { @@ -145,6 +148,52 @@ static int insert_raw_data_format(dd_list **conf_data, const char *value) return pattern_duration; } +static int get_config_data(int count, bool *packed, char *val, unsigned int *pattern_duration, struct duration_data *update) +{ + static int duration = 0; + int value; + + if (count > 2 || count <= 0) { + _E("Invalid parameter: count is invalid"); + return -EINVAL; + } + + if (!packed || !val || !pattern_duration || !update) { + _E("Invalid paramger"); + return -EINVAL; + } + + get_pattern_property(&val, 'C', &value); + + if (count == 1) { + duration = value; + *pattern_duration += duration; + if (*pattern_duration > VIB_LOCK_TIMEOUT_MAX) { + _D("Max pattern duration"); + *pattern_duration = VIB_LOCK_TIMEOUT_MAX; + + } + return count; + } + + if (value > INTENSITY_BASE_RATE) + value = INTENSITY_BASE_RATE; + if (*packed == false) { + update->duration = duration; + update->intensity = value; + *packed = true; + return 0; + } + if (value == 0) + update->wait = duration; + else + update->wait = 0; + *packed = false; + duration = 0; + + return -1; +} + /* duration, intensity, frequency, overdriving waiting duration, intensity=0, frequency, overdriving @@ -155,10 +204,9 @@ static int insert_raw_data_format(dd_list **conf_data, const char *value) */ static int load_standard_format(const char *pattern) { - struct vibration_config *conf; struct duration_data update = {0, }; bool packed = false; - long int duration = 0, intensity = 0; + struct vibration_config *conf; int ret = 0, count = 0, end = 2; int index = 0; int fd; @@ -200,51 +248,23 @@ static int load_standard_format(const char *pattern) } if (elem == ',') { count++; - val[index] = '\0'; + val[index] = 'C'; index = 0; - switch (count) { - case 1: /* D */ - duration = strtol(val, NULL, 10); - if (duration < 0) - duration = 0; - else if (duration > VIB_LOCK_TIMEOUT_MAX) - duration = VIB_LOCK_TIMEOUT_MAX; - conf->pattern_duration += (int)duration; - if (conf->pattern_duration > VIB_LOCK_TIMEOUT_MAX) { - _D("Reached max pattern duration"); - conf->pattern_duration = VIB_LOCK_TIMEOUT_MAX; - } - break; - case 2: /* I or W */ - count = 0; - intensity = strtol(val, NULL, 10); - if (intensity < 0) - intensity = 0; - else if (intensity > INTENSITY_BASE_RATE) - intensity = INTENSITY_BASE_RATE; - if (!packed) { - update.duration = (int)duration; - update.intensity = (int)intensity; - packed = true; - break; - } - /* Intensity should be 0 for wait(off) time */ - if (intensity == 0) - update.wait = duration; - else - update.wait = 0; - packed = false; + ret = get_config_data(count, &packed, val, &(conf->pattern_duration), &update); + if (ret < 0) { + if (ret == -EINVAL) + break; if (insert_conf_data(&conf->data, &update) < 0) goto error_out; memset(&update, 0, sizeof(struct duration_data)); - break; - default: - break; - } + } else + count = ret; } else { if (index < (VALUE_MAX_LEN - 2)) /* Temporal limit */ val[index++] = elem; + else + _E("Pattern %s is out of bound: %s", pattern, val); } } close(fd); @@ -273,9 +293,7 @@ static int vibration_load_config(struct parse_result *result, void *user_data) if (!result) return 0; - if (!MATCH(result->section, "vibration") && - !MATCH(result->section, "Vibration") && - !MATCH(result->section, "VIBRATION")) + if (!MATCH(result->section, "Vibration")) return 0; -- 2.7.4