modules: use safer pod parsing for control sequence

This commit is contained in:
Wim Taymans 2025-07-31 11:50:11 +02:00
parent 28510f3117
commit 8495bffee5
5 changed files with 73 additions and 80 deletions

View file

@ -307,9 +307,11 @@ static inline void n2j_midi_buffer_append(struct nj2_midi_buffer *buf,
static void midi_to_netjack2(struct netjack2_peer *peer,
struct nj2_midi_buffer *buf, float *src, uint32_t n_samples)
{
struct spa_pod *pod;
struct spa_pod_sequence *seq;
struct spa_pod_control *c;
struct spa_pod_parser parser;
struct spa_pod_frame frame;
struct spa_pod_sequence seq;
struct spa_pod_control c;
const void *seq_body, *c_body;
bool in_sysex = false;
buf->magic = MIDI_BUFFER_MAGIC;
@ -322,28 +324,24 @@ static void midi_to_netjack2(struct netjack2_peer *peer,
if (src == NULL)
return;
if ((pod = spa_pod_from_data(src, n_samples * sizeof(float),
0, n_samples * sizeof(float))) == NULL)
return;
if (!spa_pod_is_sequence(pod))
spa_pod_parser_init_from_data(&parser, src, n_samples * sizeof(float),
0, n_samples * sizeof(float));
if (spa_pod_parser_push_sequence_body(&parser, &frame, &seq, &seq_body) < 0)
return;
seq = (struct spa_pod_sequence*)pod;
SPA_POD_SEQUENCE_FOREACH(seq, c) {
while (spa_pod_parser_get_control_body(&parser, &c, &c_body) >= 0) {
int size;
uint8_t data[16];
bool was_sysex = in_sysex;
if (c->type != SPA_CONTROL_UMP)
if (c.type != SPA_CONTROL_UMP)
continue;
size = spa_ump_to_midi(SPA_POD_BODY(&c->value),
SPA_POD_BODY_SIZE(&c->value), data, sizeof(data));
size = spa_ump_to_midi(c_body, c.value.size, data, sizeof(data));
if (size <= 0)
continue;
if (c->offset >= n_samples) {
if (c.offset >= n_samples) {
buf->lost_events++;
continue;
}
@ -360,7 +358,7 @@ static void midi_to_netjack2(struct netjack2_peer *peer,
if (was_sysex)
n2j_midi_buffer_append(buf, data, size);
else
n2j_midi_buffer_write(buf, c->offset, data, size);
n2j_midi_buffer_write(buf, c.offset, data, size);
}
if (buf->write_pos > 0)
memmove(SPA_PTROFF(buf, sizeof(*buf) + buf->event_count * sizeof(struct nj2_midi_event), void),