Code Optimization avoid strdup of strings in the GPV, GPN and GPA

Code review
This commit is contained in:
Anis Ellouze 2015-08-25 18:20:09 +01:00 committed by MOHAMED Kallel
parent 48293abde3
commit 152bf287e3
5 changed files with 424 additions and 500 deletions

View file

@ -38,15 +38,14 @@ char *get_pid(char *pname)
fgets(str, TAILLE_MAX, f);
if (str[0] == '\0') {
pclose(f);
return dmstrdup("");
return "";
}
pid_t pid = strtoul(str, NULL, 10);
pclose(f);
dmasprintf(&v, "%d", pid);
dmasprintf(&v, "%d", pid); // MEM WILL BE FREED IN DMMEMCLEAN
return v;
}
v = dmstrdup("");
return v;
return "";
}
int check_file(char *path)
@ -57,4 +56,4 @@ int check_file(char *path)
return 1;
}
return 0;
}
}

View file

@ -92,16 +92,14 @@ char *update_instance(struct uci_section *s, char *last_inst, char *inst_opt)
sprintf(buf, "%d", 1);
else
sprintf(buf, "%d", atoi(last_inst)+1);
dmfree(instance); //TODO MEM
instance = dmuci_set_value_by_section(s, inst_opt, buf);
instance = dmstrdup(instance);
}
return instance;
}
int get_empty(char *refparam, struct dmctx *args, char **value)
{
*value = dmstrdup("");
*value = "";
return 0;
}
@ -121,7 +119,7 @@ void add_list_paramameter(struct dmctx *ctx, char *param_name, char *param_data,
dm_parameter = dmcalloc(1, sizeof(struct dm_parameter));
_list_add(&dm_parameter->list, ilist->prev, ilist);
dm_parameter->name = param_name;
dm_parameter->data = param_data ? param_data : dmstrdup(""); //allocate memory in function
dm_parameter->data = param_data ? param_data : ""; //allocate memory in function
dm_parameter->type = param_type;
}
@ -129,7 +127,6 @@ void del_list_parameter(struct dm_parameter *dm_parameter)
{
list_del(&dm_parameter->list);
dmfree(dm_parameter->name);
dmfree(dm_parameter->data);
dmfree(dm_parameter);
}
@ -261,11 +258,9 @@ static int set_parameter_notification(char *param, char *value)
dmuci_get_section_type("cwmp", "@notifications[0]", &tmp);
if (!tmp || tmp[0] == '\0') {
dmuci_add_section("cwmp", "notifications", &s, &buf);
dmfree(buf);
} else {
remove_parameter_notification(param);
}
dmfree(tmp);
notification = get_parameter_notification(param);
if (strcmp(notification, value) == 0) {
@ -483,7 +478,7 @@ int dm_entry_get_name(struct dmctx *ctx)
static int get_name_obj(DMOBJECT_API_ARGS)
{
char *obj = dmstrdup(ctx->current_obj);
char *p = dmstrdup(permission);
char *p = permission;
add_list_paramameter(ctx, obj, p, NULL);
return 0;
}
@ -499,7 +494,7 @@ static int get_name_inparam_isobj_check_obj(DMOBJECT_API_ARGS)
ctx->faultcode = 0;
if (ctx->nextlevel == 0 || check_obj_is_nl1(ctx->current_obj, ctx->in_param, 2) == 0 ) {
char *obj = dmstrdup(ctx->current_obj);
char *p = dmstrdup(permission);
char *p = permission;
add_list_paramameter(ctx, obj, p, NULL);
return 0;
}
@ -511,7 +506,7 @@ static int get_name_inparam_isobj_check_obj(DMOBJECT_API_ARGS)
static int get_name_emptyin_nl1_obj(DMOBJECT_API_ARGS)
{
char *obj = dmstrdup(ctx->current_obj);
char *p = dmstrdup(permission);
char *p = permission;
add_list_paramameter(ctx, obj, p, NULL);
return 0;
}
@ -519,7 +514,7 @@ static int get_name_emptyin_nl1_obj(DMOBJECT_API_ARGS)
static int get_name_param(DMPARAM_API_ARGS)
{
char *full_param;
char *p = dmstrdup(permission);
char *p = permission;
dmastrcat(&full_param, ctx->current_obj, lastname);
add_list_paramameter(ctx, full_param, p, NULL);
return 0;
@ -538,7 +533,7 @@ static int get_name_inparam_isparam_check_param(DMPARAM_API_ARGS)
ctx->stop = 1;
return FAULT_9003;
}
char *p = dmstrdup(permission);
char *p = permission;
add_list_paramameter(ctx, full_param, p, NULL);
ctx->stop = 1;
return 0;
@ -551,12 +546,12 @@ static int get_name_inparam_isobj_check_param(DMPARAM_API_ARGS)
if (strstr(full_param, ctx->in_param)) {
ctx->faultcode = 0;
if (ctx->nextlevel == 0 || check_obj_is_nl1(full_param, ctx->in_param, 1) == 0 ) {
char *p = dmstrdup(permission);
char *p = permission;
add_list_paramameter(ctx, full_param, p, NULL);
return 0;
}
dmfree(full_param);
return 0;
return 0; //TODO check the return value here!
}
dmfree(full_param);
return FAULT_9005;
@ -620,9 +615,8 @@ static int get_notification_param(DMPARAM_API_ARGS)
dmastrcat(&full_param, ctx->current_obj, lastname);
if (forced_notify == UNDEF) {
notification = get_parameter_notification(full_param);
notification = dmstrdup(notification);
} else {
notification = dmstrdup(notifications[forced_notify].value);
notification = notifications[forced_notify].value;
}
add_list_paramameter(ctx, full_param, notification, NULL);
return 0;
@ -639,9 +633,8 @@ static int get_notification_inparam_isparam_check_param(DMPARAM_API_ARGS)
}
if (forced_notify == UNDEF) {
notification = get_parameter_notification(full_param);
notification = dmstrdup(notification);
} else {
notification = dmstrdup(notifications[forced_notify].value);
notification = notifications[forced_notify].value;
}
add_list_paramameter(ctx, full_param, notification, NULL);
ctx->stop = true;
@ -656,9 +649,8 @@ static int get_notification_inparam_isobj_check_param(DMPARAM_API_ARGS)
if (strstr(full_param, ctx->in_param)) {
if (forced_notify == UNDEF) {
notification = get_parameter_notification(full_param);
notification = dmstrdup(notification);
} else {
notification = dmstrdup(notifications[forced_notify].value);
notification = notifications[forced_notify].value;
}
add_list_paramameter(ctx, full_param, notification, NULL);
ctx->faultcode = 0;
@ -873,7 +865,8 @@ static int set_notification_check_obj(DMOBJECT_API_ARGS)
return FAULT_9009;
add_set_list_tmp(ctx, ctx->in_param, ctx->in_notification);
} else if (ctx->setaction == VALUESET) {
}
else if (ctx->setaction == VALUESET) {
set_parameter_notification(ctx->in_param, ctx->in_notification);
}
return 0;

View file

@ -105,7 +105,7 @@ int get_device_hardwareversion(char *refparam, struct dmctx *ctx, char **value)
{
db_get_value_string("hw", "board", "hardwareVersion", value);
return 0;
}
}
int get_device_routermodel(char *refparam, struct dmctx *ctx, char **value)
{

File diff suppressed because it is too large Load diff

View file

@ -38,11 +38,11 @@ char *dmuci_list_to_string(struct uci_list *list, char *delimitor)
else
strcpy(val, e->name);
}
return (dmstrdup(val));
return (dmstrdup(val)); // MEM WILL BE FREED IN DMMEMCLEAN
}
else
{
return dmstrdup("");
return "";
}
}
@ -129,13 +129,13 @@ int dmuci_get_section_type(char *package, char *section,char **value)
struct uci_element *e;
struct uci_ptr ptr = {0};
if (dmuci_lookup_ptr(uci_ctx, &ptr, package, section, NULL, NULL)) {
*value = dmstrdup("");
*value = "";
return -1;
}
if (ptr.s) {
*value = dmstrdup(ptr.s->type);
*value = ptr.s->type;
} else {
*value = dmstrdup("");
*value = "";
return -1;
}
return 0;
@ -146,13 +146,13 @@ int dmuci_get_option_value_string(char *package, char *section, char *option, ch
struct uci_element *e;
struct uci_ptr ptr = {0};
if (dmuci_lookup_ptr(uci_ctx, &ptr, package, section, option, NULL)) {
*value = dmstrdup("");
*value = "";
return -1;
}
if (ptr.o) {
*value = dmstrdup(ptr.o->v.string);
*value = ptr.o->v.string;
} else {
*value = dmstrdup("");
*value = "";
return -1;
}
return 0;
@ -205,9 +205,9 @@ int db_get_value_string(char *package, char *section, char *option, char **value
struct uci_option *o;
o = dmuci_get_option_ptr(DB_CONFIG, package, section, option);
if (o) {
*value = dmstrdup(o->v.string);
*value = o->v.string;
} else {
*value = dmstrdup("");
*value = "";
return -1;
}
return 0;
@ -359,15 +359,15 @@ int dmuci_add_section(char *package, char *stype, struct uci_section **s, char *
*s = NULL;
if (dmuci_lookup_ptr(uci_ctx, &ptr, package, NULL, NULL, NULL)) {
*value = dmstrdup("");
*value = "";
return -1;
}
if (uci_add_section(uci_ctx, ptr.p, stype, s) != UCI_OK) {
*value = dmstrdup("");
*value = "";
return -1;
}
*value = dmstrdup((*s)->e.name);
*value = (*s)->e.name;
return 0;
}
@ -437,11 +437,11 @@ int dmuci_get_value_by_section_string(struct uci_section *s, char *option, char
uci_foreach_element(&s->options, e) {
o = (uci_to_option(e));
if (!strcmp(o->e.name, option)) {
*value = dmstrdup(o->v.string);
*value = o->v.string;
return 0;
}
}
*value = dmstrdup("");
*value = "";
return -1;
}