player: catch malloc-failures

There were a couple of places where malloc-failures (out-of-memory,
really) weren't caught. This isn't a big deal on modern operating
systems, but is worth dealing with on low-end platforms.

Failures to add/delete keys should count as socket-errors, because
they cause the editor and client to become out of sync.
This commit is contained in:
Erik Faye-Lund 2011-01-26 20:51:52 +01:00
parent e3ee8c2432
commit 5efe69d845
3 changed files with 22 additions and 12 deletions

View File

@ -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)

View File

@ -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

View File

@ -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;