diff --git a/sync/device.c b/sync/device.c index 8738835..cf4e4a5 100644 --- a/sync/device.c +++ b/sync/device.c @@ -114,6 +114,8 @@ static int get_track_data(const struct sync_device *d, struct sync_track *t) fread(&t->num_keys, sizeof(size_t), 1, fp); t->keys = malloc(sizeof(struct track_key) * t->num_keys); + if (!t->keys) + return 1; for (i = 0; i < (int)t->num_keys; ++i) { struct track_key *key = t->keys + i; @@ -194,8 +196,7 @@ static int handle_set_key_cmd(SOCKET sock, struct sync_data *data) assert(type < KEY_TYPE_COUNT); assert(track < data->num_tracks); key.type = (enum key_type)type; - sync_set_key(data->tracks[track], &key); - return 1; + return !sync_set_key(data->tracks[track], &key); } static int handle_del_key_cmd(SOCKET sock, struct sync_data *data) @@ -210,8 +211,7 @@ static int handle_del_key_cmd(SOCKET sock, struct sync_data *data) row = ntohl(row); assert(track < data->num_tracks); - sync_del_key(data->tracks[track], row); - return 1; + return !sync_del_key(data->tracks[track], row); } static int purge_and_rerequest(struct sync_device *d) diff --git a/sync/track.c b/sync/track.c index 6f069ce..8160b46 100644 --- a/sync/track.c +++ b/sync/track.c @@ -79,29 +79,39 @@ int sync_find_key(const struct sync_track *t, int row) } #ifndef SYNC_PLAYER -void sync_set_key(struct sync_track *t, const struct track_key *k) +int sync_set_key(struct sync_track *t, const struct track_key *k) { int idx = sync_find_key(t, k->row); if (idx < 0) { + void *tmp; idx = -idx - 1; + tmp = realloc(t->keys, sizeof(struct track_key) * + (t->num_keys + 1)); + if (!tmp) + return -1; t->num_keys++; - t->keys = realloc(t->keys, sizeof(struct track_key) * - t->num_keys); - assert(t->keys); + t->keys = tmp; memmove(t->keys + idx + 1, t->keys + idx, sizeof(struct track_key) * (t->num_keys - idx - 1)); } t->keys[idx] = *k; + return 0; } -void sync_del_key(struct sync_track *t, int pos) +int sync_del_key(struct sync_track *t, int pos) { + void *tmp; int idx = sync_find_key(t, pos); assert(idx >= 0); memmove(t->keys + idx, t->keys + idx + 1, sizeof(struct track_key) * (t->num_keys - idx - 1)); assert(t->keys); + tmp = realloc(t->keys, sizeof(struct track_key) * + (t->num_keys - 1)); + if (t->num_keys != 1 && !tmp) + return -1; t->num_keys--; - t->keys = realloc(t->keys, t->num_keys * sizeof(struct track_key)); + t->keys = tmp; + return 0; } #endif diff --git a/sync/track.h b/sync/track.h index 1946f6d..3611a32 100644 --- a/sync/track.h +++ b/sync/track.h @@ -39,8 +39,8 @@ static inline int key_idx_floor(const struct sync_track *t, int row) } #ifndef SYNC_PLAYER -void sync_set_key(struct sync_track *, const struct track_key *); -void sync_del_key(struct sync_track *, int); +int sync_set_key(struct sync_track *, const struct track_key *); +int sync_del_key(struct sync_track *, int); static inline int is_key_frame(const struct sync_track *t, size_t row) { return sync_find_key(t, row) >= 0;