Fix behavior of some XPC object functions

In particular:
  * `xpc_string_create` needs to copy its input
  * `xpc_data_create` needs to do the same
  * `xpc_object_destroy` needs to free the memory copied into `xpc_string`s and `xpc_data`s
  * `xpc_dictionary_set_value` needs to copy the key given to it
  * `xpc_dictionary_destroy` needs to free the keys it copied

There's probably a few more, but these are the ones that popped up when I was fixing another library
This commit is contained in:
Ariel Abreu 2020-04-07 08:44:15 -04:00
parent b9301ffde6
commit 7d8c664778
No known key found for this signature in database
GPG Key ID: 6EA25D8348CAA137
3 changed files with 21 additions and 4 deletions

View File

@ -337,7 +337,11 @@ xpc_dictionary_set_value(xpc_object_t xdict, const char *key, xpc_object_t value
xo->xo_size++;
pair = malloc(sizeof(struct xpc_dict_pair));
pair->key = key;
size_t len = strlen(key);
char* str = malloc(len + 1);
strncpy(str, key, len);
str[len] = '\0';
pair->key = str;
pair->value = value;
TAILQ_INSERT_TAIL(&xo->xo_dict, pair, xo_link);
xpc_retain(value);

View File

@ -94,6 +94,7 @@ xpc_dictionary_destroy(struct xpc_object *dict)
TAILQ_FOREACH_SAFE(p, head, xo_link, ptmp) {
TAILQ_REMOVE(head, p, xo_link);
free(p->key);
xpc_object_destroy(p->value);
free(p);
}
@ -160,6 +161,12 @@ xpc_object_destroy(struct xpc_object *xo)
if (xo->xo_xpc_type == _XPC_TYPE_CONNECTION)
xpc_connection_destroy(xo);
if (xo->xo_xpc_type == _XPC_TYPE_STRING)
free(xo->xo_u.str);
if (xo->xo_xpc_type == _XPC_TYPE_DATA)
free(xo->xo_u.ptr);
free(xo);
}

View File

@ -273,7 +273,9 @@ xpc_data_create(const void *bytes, size_t length)
{
xpc_u val;
val.ptr = (uintptr_t)bytes;
void* copy = malloc(length);
memcpy(copy, bytes, length);
val.ptr = copy;
return _xpc_prim_create(_XPC_TYPE_DATA, val, length);
}
@ -340,8 +342,12 @@ xpc_string_create(const char *string)
{
xpc_u val;
val.str = string;
return _xpc_prim_create(_XPC_TYPE_STRING, val, strlen(string));
size_t len = strlen(string);
char* str = malloc(len + 1);
strncpy(str, string, len);
str[len] = '\0';
val.str = str;
return _xpc_prim_create(_XPC_TYPE_STRING, val, len);
}
xpc_object_t