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:
parent
e3ee8c2432
commit
5efe69d845
@ -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);
|
fread(&t->num_keys, sizeof(size_t), 1, fp);
|
||||||
t->keys = malloc(sizeof(struct track_key) * t->num_keys);
|
t->keys = malloc(sizeof(struct track_key) * t->num_keys);
|
||||||
|
if (!t->keys)
|
||||||
|
return 1;
|
||||||
|
|
||||||
for (i = 0; i < (int)t->num_keys; ++i) {
|
for (i = 0; i < (int)t->num_keys; ++i) {
|
||||||
struct track_key *key = t->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(type < KEY_TYPE_COUNT);
|
||||||
assert(track < data->num_tracks);
|
assert(track < data->num_tracks);
|
||||||
key.type = (enum key_type)type;
|
key.type = (enum key_type)type;
|
||||||
sync_set_key(data->tracks[track], &key);
|
return !sync_set_key(data->tracks[track], &key);
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int handle_del_key_cmd(SOCKET sock, struct sync_data *data)
|
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);
|
row = ntohl(row);
|
||||||
|
|
||||||
assert(track < data->num_tracks);
|
assert(track < data->num_tracks);
|
||||||
sync_del_key(data->tracks[track], row);
|
return !sync_del_key(data->tracks[track], row);
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int purge_and_rerequest(struct sync_device *d)
|
static int purge_and_rerequest(struct sync_device *d)
|
||||||
|
|||||||
22
sync/track.c
22
sync/track.c
@ -79,29 +79,39 @@ int sync_find_key(const struct sync_track *t, int row)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifndef SYNC_PLAYER
|
#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);
|
int idx = sync_find_key(t, k->row);
|
||||||
if (idx < 0) {
|
if (idx < 0) {
|
||||||
|
void *tmp;
|
||||||
idx = -idx - 1;
|
idx = -idx - 1;
|
||||||
|
tmp = realloc(t->keys, sizeof(struct track_key) *
|
||||||
|
(t->num_keys + 1));
|
||||||
|
if (!tmp)
|
||||||
|
return -1;
|
||||||
t->num_keys++;
|
t->num_keys++;
|
||||||
t->keys = realloc(t->keys, sizeof(struct track_key) *
|
t->keys = tmp;
|
||||||
t->num_keys);
|
|
||||||
assert(t->keys);
|
|
||||||
memmove(t->keys + idx + 1, t->keys + idx,
|
memmove(t->keys + idx + 1, t->keys + idx,
|
||||||
sizeof(struct track_key) * (t->num_keys - idx - 1));
|
sizeof(struct track_key) * (t->num_keys - idx - 1));
|
||||||
}
|
}
|
||||||
t->keys[idx] = *k;
|
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);
|
int idx = sync_find_key(t, pos);
|
||||||
assert(idx >= 0);
|
assert(idx >= 0);
|
||||||
memmove(t->keys + idx, t->keys + idx + 1,
|
memmove(t->keys + idx, t->keys + idx + 1,
|
||||||
sizeof(struct track_key) * (t->num_keys - idx - 1));
|
sizeof(struct track_key) * (t->num_keys - idx - 1));
|
||||||
assert(t->keys);
|
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->num_keys--;
|
||||||
t->keys = realloc(t->keys, t->num_keys * sizeof(struct track_key));
|
t->keys = tmp;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -39,8 +39,8 @@ static inline int key_idx_floor(const struct sync_track *t, int row)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifndef SYNC_PLAYER
|
#ifndef SYNC_PLAYER
|
||||||
void sync_set_key(struct sync_track *, const struct track_key *);
|
int sync_set_key(struct sync_track *, const struct track_key *);
|
||||||
void sync_del_key(struct sync_track *, int);
|
int sync_del_key(struct sync_track *, int);
|
||||||
static inline int is_key_frame(const struct sync_track *t, size_t row)
|
static inline int is_key_frame(const struct sync_track *t, size_t row)
|
||||||
{
|
{
|
||||||
return sync_find_key(t, row) >= 0;
|
return sync_find_key(t, row) >= 0;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user