Skip to content

Commit

Permalink
fix(DmPerformance): make sure messages don't go out-of-scope when ren…
Browse files Browse the repository at this point in the history
…dering
  • Loading branch information
lmichaelis committed May 2, 2024
1 parent 7b686f2 commit 72aad30
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/Message.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: MIT-Modern-Variant
#include "_Internal.h"

void DmMessage_copy(DmMessage* slf, DmMessage* cpy, uint32_t time) {
void DmMessage_copy(DmMessage* slf, DmMessage* cpy, int64_t time) {
if (slf == NULL || cpy == NULL) {
return;
}
Expand All @@ -15,7 +15,9 @@ void DmMessage_copy(DmMessage* slf, DmMessage* cpy, uint32_t time) {
cpy->style.style = DmStyle_retain(slf->style.style);
}

cpy->time = time;
if (time >= 0) {
cpy->time = time;
}
}

void DmMessage_free(DmMessage* slf) {
Expand Down
9 changes: 6 additions & 3 deletions src/Performance.c
Original file line number Diff line number Diff line change
Expand Up @@ -803,8 +803,10 @@ DmResult DmPerformance_renderPcm(DmPerformance* slf, void* buf, size_t len, DmRe
(void) mtx_unlock(&slf->mod_lock);
}

DmMessage* msg = ok_ctrl ? &msg_ctrl : &msg_midi;
uint32_t time_offset = (uint32_t) max_s32((int) msg->time - (int) slf->time, 0);
DmMessage msg;
DmMessage_copy(ok_ctrl ? &msg_ctrl : &msg_midi, &msg, -1);

uint32_t time_offset = (uint32_t) max_s32((int) msg.time - (int) slf->time, 0);
uint32_t offset_samples = DmPerformance_getSampleCountFromDuration(slf, time_offset, slf->sample_rate, channels);

if (offset_samples > len - sample) {
Expand Down Expand Up @@ -842,7 +844,8 @@ DmResult DmPerformance_renderPcm(DmPerformance* slf, void* buf, size_t len, DmRe
DmMessageQueue_pop(&slf->music_queue);
}

DmPerformance_handleMessage(slf, msg);
DmPerformance_handleMessage(slf, &msg);
DmMessage_free(&msg);
}

// Render the remaining samples
Expand Down
4 changes: 2 additions & 2 deletions src/_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef enum DmResolveFlags {
DmResolve_AFTER_LATENCY_TIME = 1 << 22,
DmResolve_GRID = 1 << 11,
DmResolve_BEAT = 1 << 12,
DmResolve_MEASURE =1 << 13,
DmResolve_MEASURE = 1 << 13,
DmResolve_MARKER = 1 << 24,
DmResolve_SEGMENT_END = 1 << 23,
} DmResolveFlags;
Expand Down Expand Up @@ -583,7 +583,7 @@ DMINT DmResult DmLoader_getDownloadableSound(DmLoader* slf, DmReference const* r
DMINT DmResult DmSegment_create(DmSegment** slf);
DMINT DmResult DmSegment_parse(DmSegment* slf, void* buf, size_t len);

DMINT void DmMessage_copy(DmMessage* slf, DmMessage* cpy, uint32_t time);
DMINT void DmMessage_copy(DmMessage* slf, DmMessage* cpy, int64_t time);
DMINT void DmMessage_free(DmMessage* slf);
DMINT DmResult DmMessageQueue_init(DmMessageQueue* slf);
DMINT void DmMessageQueue_free(DmMessageQueue* slf);
Expand Down

0 comments on commit 72aad30

Please sign in to comment.