diff options
author | Angelo Haller <[email protected]> | 2020-09-03 17:41:41 -0500 |
---|---|---|
committer | Paul Adenot <[email protected]> | 2020-09-14 11:06:11 +0200 |
commit | f4c31978941d16414eefb8eadfed463d01c2478d (patch) | |
tree | 12e398de2453365059b6d3b7238aaeda7d29441b | |
parent | f39ce8a726448d6dd4b08949b49ed6937bc080d7 (diff) | |
download | cubeb-f4c31978941d16414eefb8eadfed463d01c2478d.tar.gz cubeb-f4c31978941d16414eefb8eadfed463d01c2478d.zip |
Fix integer underflow/unsigned wrapping.
The number of xrun fragments to skip is added once to ctx->jack_xruns but
then subtracted multiple times (once for each stream) from the same variable.
This causes the unsigned int to "underflow" and wrap around.
- Use unsigned int in all calculations instead of mixing un/signed.
- Remove erroneous and useless subtraction logic
- Replace for loop by simple multiplication
- Remove unneeded casts to float
-rw-r--r-- | src/cubeb_jack.cpp | 15 |
1 files changed, 7 insertions, 8 deletions
diff --git a/src/cubeb_jack.cpp b/src/cubeb_jack.cpp index 2320b73..d913b15 100644 --- a/src/cubeb_jack.cpp +++ b/src/cubeb_jack.cpp @@ -290,9 +290,9 @@ cbjack_xrun_callback(void * arg) cubeb * ctx = (cubeb *)arg; float delay = api_jack_get_xrun_delayed_usecs(ctx->jack_client); - int fragments = (int)ceilf( ((delay / 1000000.0) * ctx->jack_sample_rate ) - / (float)(ctx->jack_buffer_size) ); - ctx->jack_xruns += fragments; + float fragments = ceilf(((delay / 1000000.0) * ctx->jack_sample_rate) / ctx->jack_buffer_size); + + ctx->jack_xruns += (unsigned int)fragments; return 0; } @@ -332,9 +332,11 @@ static int cbjack_process(jack_nframes_t nframes, void * arg) { cubeb * ctx = (cubeb *)arg; - int t_jack_xruns = ctx->jack_xruns; + unsigned int t_jack_xruns = ctx->jack_xruns; int i; + ctx->jack_xruns = 0; + for (int j = 0; j < MAX_STREAMS; j++) { cubeb_stream *stm = &ctx->streams[j]; float *bufs_out[stm->out_params.channels]; @@ -344,10 +346,7 @@ cbjack_process(jack_nframes_t nframes, void * arg) continue; // handle xruns by skipping audio that should have been played - for (i = 0; i < t_jack_xruns; i++) { - stm->position += ctx->fragment_size * stm->ratio; - } - ctx->jack_xruns -= t_jack_xruns; + stm->position += t_jack_xruns * ctx->fragment_size * stm->ratio; if (!stm->ports_ready) continue; |