Skip to content

Commit febfff5

Browse files
author
mpg123 GitHub bot
committed
Merge remote-tracking branch 'mpg123/master' into master-with-github-ci
2 parents 4080a89 + c9923b1 commit febfff5

5 files changed

Lines changed: 119 additions & 3 deletions

File tree

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
------
33
- ports/cmake: now versioned library files (github PR 24 by madebr)
44
This raises the minimum CMake version to 3.27.
5+
- fmt123.h: Check for obviously invalid encoding in MPG123_ENC_SIZE()
6+
via the MPG123_ENC_ANY mask.
57

68
1.33.6
79
------

src/include/fmt123.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,17 @@ enum mpg123_enc_enum
9595
* encoding to appear. But who knows? Perhaps the encoding type
9696
* will be abused for funny things in future, not even plain PCM.
9797
* And, by the way: Thomas really likes the ?: operator.
98+
*
99+
* This does a cheap check if any unknown bits are set, no deep
100+
* testing if the bit combination really is a valid encoding. It
101+
* shall be a quick macro. You can use out123_enc_name() to check
102+
* if your value is really a known encoding.
103+
*
98104
* \param enc the encoding (mpg123_enc_enum value)
99105
* \return size of one sample in bytes
100106
*/
101107
#define MPG123_SAMPLESIZE(enc) ( \
102-
(enc) < 1 \
108+
((enc) < 1 || ((enc) & ~MPG123_ENC_ANY)) \
103109
? 0 \
104110
: ( (enc) & MPG123_ENC_8 \
105111
? 1 \

src/include/syn123.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ size_t syn123_resample_out(syn123_handle *sh, size_t ins, int *err);
981981
*
982982
* This give you the minimal number of input samples needed right
983983
* now to yield at least the specified amount of output samples.
984-
* Since one input sample can result in several output sampels in one
984+
* Since one input sample can result in several output samples in one
985985
* go, you have to check using syn123_resample_out() how many
986986
* output samples to really expect.
987987
*

src/tests/Makemodule.am

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ endif
1010
TESTS += \
1111
src/tests/seek_whence.sh \
1212
src/tests/seek_accuracy.sh \
13+
src/tests/resample_buffer \
1314
src/tests/resample_total \
1415
src/tests/text \
1516
src/tests/textprint \
@@ -41,7 +42,6 @@ check_PROGRAMS += \
4142
src/tests/decode_fixed \
4243
src/tests/seek_whence \
4344
src/tests/seek_accuracy \
44-
src/tests/resample_total \
4545
src/tests/text \
4646
src/tests/textprint \
4747
src/tests/plain_id3
@@ -55,6 +55,12 @@ EXTRA_PROGRAMS += \
5555
src/tests/sweeper
5656
endif
5757

58+
if BUILD_LIBSYN123
59+
check_PROGRAMS += \
60+
src/tests/resample_total \
61+
src/tests/resample_buffer
62+
endif
63+
5864
src_tests_volume_SOURCES = \
5965
src/tests/volume.c
6066
src_tests_volume_LDADD = \
@@ -92,6 +98,11 @@ src_tests_sweeper_LDADD = \
9298
$(LIBSYN123) \
9399
$(LIBOUT123)
94100

101+
src_tests_resample_buffer_SOURCES = \
102+
src/tests/resample_buffer.c
103+
src_tests_resample_buffer_LDADD = \
104+
$(LIBSYN123)
105+
95106
src_tests_resample_total_SOURCES = \
96107
src/tests/resample_total.c
97108
src_tests_resample_total_LDADD = \

src/tests/resample_buffer.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Test the various sample count computations and against output
2+
// buffer overflow of libsyn123.
3+
4+
#include "mpg123config.h"
5+
#include <syn123.h>
6+
#include <inttypes.h>
7+
8+
#include <stdlib.h>
9+
#include <stdio.h>
10+
11+
#include "common/debug.h"
12+
13+
const long arate = 22050;
14+
const long brate = 48000;
15+
const size_t outs = 0x10000;
16+
const int buffer_runs = 100;
17+
const float canary = 2317.;
18+
19+
int main()
20+
{
21+
size_t ins = syn123_resample_incount(arate, brate, outs);
22+
size_t outs2 = syn123_resample_count(arate, brate, ins);
23+
size_t fillins = syn123_resample_fillcount(arate, brate, outs);
24+
size_t fillouts = syn123_resample_count(arate, brate, fillins);
25+
printf("%zu output buffer size\n", outs);
26+
printf("%zu minimal input samples to ensure we can fill that buffer\n", ins);
27+
printf("%zu maximum output samples to result from the above\n", outs2);
28+
printf("%zu maximum input samples to not overflow that buffer\n", fillins);
29+
printf("%zu maximum output samples to result from the above\n", fillouts);
30+
int err = 0;
31+
32+
syn123_handle *sh = syn123_new( brate, 1, MPG123_ENC_FLOAT_32, 1024, &err);
33+
if(!sh)
34+
mereturn(1, "handle alloc failure: %s", syn123_strerror(err));
35+
36+
// A dirty resampler.
37+
if(SYN123_OK != (err =syn123_setup_resample(sh, arate, brate, 1, 1, 0)))
38+
mereturn(1, "resample setup failure: %s", syn123_strerror(err));
39+
40+
// Default sine wave as source signal.
41+
if( SYN123_OK !=
42+
(err =syn123_setup_waves( sh, 0, NULL, NULL, NULL, NULL, NULL)) )
43+
mereturn(1, "wave setup failure: %s", syn123_strerror(err));
44+
45+
float *inbuf = malloc(sizeof(float)*ins);
46+
float *outbuf = malloc(sizeof(float)*(outs2+10));
47+
if(!inbuf || !outbuf)
48+
ereturn(1, "buffer alloc failure");
49+
50+
for(int r=0; r<buffer_runs; ++r)
51+
{
52+
// Prepare input,
53+
size_t got = syn123_read(sh, inbuf, sizeof(float)*ins);
54+
if(got != sizeof(float)*ins)
55+
mereturn( 1
56+
, "unexpected byte count from generator in run %d: %zu != %zu"
57+
, r, got, sizeof(float)*ins);
58+
// Prime output buffer with some weird large number.
59+
for(int o=0; o<(outs2+10); ++o) outbuf[o] = canary;
60+
size_t exp = syn123_resample_out(sh, ins, NULL);
61+
// Resample, check if anything is overwritten beyond the desired end.
62+
got = syn123_resample(sh, outbuf, inbuf, ins);
63+
if(!got || exp != got)
64+
mereturn( 1
65+
, "got nothing or at least not what was expected"
66+
" in run %d: %zu != %zu"
67+
, r, got, exp );
68+
if(got < outs)
69+
mereturn( 1
70+
, "got less than promised minimum output sample count"
71+
" in run %d: %zu < %zu"
72+
, r, got, outs );
73+
if(got > outs2)
74+
mereturn( 1
75+
, "got more than promised maximum output sample count"
76+
" in run %d: %zu > %zu"
77+
, r, got, outs2 );
78+
size_t over = 0;
79+
for(int o=outs2; o<(outs2+10); ++o)
80+
if(outbuf[o] != canary)
81+
{
82+
merror( "resample output overflow at %zu+%zu in run %d: %f != %f"
83+
, outs2, o-outs2, r, outbuf[o], canary );
84+
++over;
85+
}
86+
if(over)
87+
mereturn(1, "%zu samples overflow in run %d", over, r);
88+
}
89+
printf("%d resampler runs without issue\n", buffer_runs);
90+
printf("PASS\n");
91+
92+
free(outbuf);
93+
free(inbuf);
94+
syn123_del(sh);
95+
96+
return 0;
97+
}

0 commit comments

Comments
 (0)