clean up some poorly constructed code (issue #5399, with mod to include header file for ast_copy_string())

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@6733 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.2-netsec
Kevin P. Fleming 20 years ago
parent 8496d5fe92
commit b00e8e509b

@ -31,43 +31,31 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/chanvars.h" #include "asterisk/chanvars.h"
#include "asterisk/logger.h" #include "asterisk/logger.h"
#include "asterisk/strings.h"
struct ast_var_t *ast_var_assign(const char *name, const char *value) struct ast_var_t *ast_var_assign(const char *name, const char *value)
{ {
int i; int i;
struct ast_var_t *var; struct ast_var_t *var;
int len;
len = sizeof(struct ast_var_t); var = calloc(sizeof(struct ast_var_t) + strlen(name) + 1 + strlen(value) + 1, sizeof(char));
len += strlen(name) + 1; if (var == NULL) {
len += strlen(value) + 1;
var = malloc(len);
if (var == NULL)
{
ast_log(LOG_WARNING, "Out of memory\n"); ast_log(LOG_WARNING, "Out of memory\n");
return NULL; return NULL;
} }
memset(var, 0, len); i = strlen(name) + 1;
i = strlen(name); ast_copy_string(var->name, name, i);
strncpy(var->name, name, i); var->value = var->name + i;
var->name[i] = '\0'; ast_copy_string(var->value, value, strlen(value) + 1);
var->value = var->name + i + 1;
i = strlen(value);
strncpy(var->value, value, i);
var->value[i] = '\0';
return var; return var;
} }
void ast_var_delete(struct ast_var_t *var) void ast_var_delete(struct ast_var_t *var)
{ {
if (var == NULL) return; if (var)
free(var); free(var);
} }
@ -80,8 +68,8 @@ char *ast_var_name(struct ast_var_t *var)
if (var->name == NULL) if (var->name == NULL)
return NULL; return NULL;
/* Return the name without the initial underscores */ /* Return the name without the initial underscores */
if ((strlen(var->name) > 0) && (var->name[0] == '_')) { if (var->name[0] == '_') {
if ((strlen(var->name) > 1) && (var->name[1] == '_')) if (var->name[1] == '_')
name = (char*)&(var->name[2]); name = (char*)&(var->name[2]);
else else
name = (char*)&(var->name[1]); name = (char*)&(var->name[1]);
@ -92,12 +80,12 @@ char *ast_var_name(struct ast_var_t *var)
char *ast_var_full_name(struct ast_var_t *var) char *ast_var_full_name(struct ast_var_t *var)
{ {
return (var != NULL ? var->name : NULL); return (var ? var->name : NULL);
} }
char *ast_var_value(struct ast_var_t *var) char *ast_var_value(struct ast_var_t *var)
{ {
return (var != NULL ? var->value : NULL); return (var ? var->value : NULL);
} }

Loading…
Cancel
Save