Properly copy string->string

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@4347 4a71c877-e1ca-e34f-864e-861f7616d084
master
Dennis Schridde 2008-03-26 10:26:15 +00:00
parent 6fdc8dd8b1
commit 9019233d1a
2 changed files with 21 additions and 11 deletions

View File

@ -1148,7 +1148,10 @@ BOOL interpInitValue(INTERP_TYPE type, INTERP_VAL *value)
case VAL_STRING: case VAL_STRING:
value->v.sval = malloc(MAXSTRLEN); value->v.sval = malloc(MAXSTRLEN);
if (value->v.sval == NULL) if (value->v.sval == NULL)
{
debug(LOG_ERROR, "interpInitValue(string): Out of memory");
return false; return false;
}
value->v.sval[0] = '\0'; value->v.sval[0] = '\0';
break; break;
default: default:
@ -1159,7 +1162,7 @@ BOOL interpInitValue(INTERP_TYPE type, INTERP_VAL *value)
} }
BOOL interpCleanValue(INTERP_VAL *value) void interpCleanValue(INTERP_VAL *value)
{ {
switch (value->type) switch (value->type)
{ {
@ -1170,32 +1173,39 @@ BOOL interpCleanValue(INTERP_VAL *value)
default: default:
break; break;
} }
return true;
} }
BOOL interpCopyValue(INTERP_VAL *to, INTERP_VAL *from) BOOL interpCopyValue(INTERP_VAL *to, INTERP_VAL *from)
{ {
/* Check whether we can do a direct copy */
if (interpCheckEquiv(to->type, from->type)) if (interpCheckEquiv(to->type, from->type))
{ {
memcpy(&(to->v), &(from->v), sizeof(to->v)); switch (to->type)
return true; {
case VAL_STRING:
free(to->v.sval);
to->v.sval = malloc(MAXSTRLEN);
if (to->v.sval == NULL)
return false;
return (strlcpy(to->v.sval, from->v.sval, MAXSTRLEN) != 0);
default:
return (memcpy(&(to->v), &(from->v), sizeof(to->v)) != NULL);
}
} }
/* Or have to do an implicit conversion */
switch (to->type) switch (to->type)
{ {
case VAL_STRING: case VAL_STRING:
switch (from->type) switch (from->type)
{ {
case VAL_INT: case VAL_INT:
return snprintf(to->v.sval, MAXSTRLEN, "%d", from->v.ival); return (snprintf(to->v.sval, MAXSTRLEN, "%d", from->v.ival) != 0);
case VAL_BOOL: case VAL_BOOL:
return snprintf(to->v.sval, MAXSTRLEN, "%d", from->v.bval); return (snprintf(to->v.sval, MAXSTRLEN, "%d", from->v.bval) != 0);
case VAL_FLOAT: case VAL_FLOAT:
return snprintf(to->v.sval, MAXSTRLEN, "%f", from->v.fval); return (snprintf(to->v.sval, MAXSTRLEN, "%f", from->v.fval) != 0);
case VAL_STRING:
return strlcpy(to->v.sval, from->v.sval, MAXSTRLEN);
default: default:
break; break;
} }

View File

@ -288,7 +288,7 @@ extern const char *interpFunctionToString(SCRIPT_FUNC function) WZ_DECL_PURE;
extern BOOL interpCopyValue(INTERP_VAL *to, INTERP_VAL *from); extern BOOL interpCopyValue(INTERP_VAL *to, INTERP_VAL *from);
extern BOOL interpInitValue(INTERP_TYPE type, INTERP_VAL *value); extern BOOL interpInitValue(INTERP_TYPE type, INTERP_VAL *value);
extern BOOL interpCleanValue(INTERP_VAL *value); extern void interpCleanValue(INTERP_VAL *value);
#endif #endif