From 544b1db62b7b1458d09e03885ef722dd7dfd28b0 Mon Sep 17 00:00:00 2001 From: Lars Immisch Date: Thu, 23 Nov 2023 17:38:34 +0100 Subject: [PATCH] rem/aufile: test and fix aufile_set_position nread (#1010) Also fix a bug: wav files can contain data after the data chunk. If we don't update nread in aufile_set_position, we can read invalid data. --- rem/aufile/aufile.c | 4 +++ test/CMakeLists.txt | 1 + test/aupos.c | 61 ++++++++++++++++++++++++++++++++++++++++++++ test/data/beep.wav | Bin 6156 -> 6158 bytes test/test.c | 1 + test/test.h | 1 + 6 files changed, 68 insertions(+) create mode 100644 test/aupos.c diff --git a/rem/aufile/aufile.c b/rem/aufile/aufile.c index 42185e92c..f5ceb7eaa 100644 --- a/rem/aufile/aufile.c +++ b/rem/aufile/aufile.c @@ -294,8 +294,12 @@ int aufile_set_position(struct aufile *af, const struct aufile_prm *prm, off_t pos = (off_t)(prm->srate * aufmt_sample_size(prm->fmt) * prm->channels * pos_ms / 1000); + pos = min((off_t)datasize, pos); + if (fseek(af->f, pos, SEEK_CUR) < 0) return errno; + af->nread = pos; + return 0; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9412ac2f5..7e2610395 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -72,6 +72,7 @@ set(SRCS aubuf.c aulength.c aulevel.c + aupos.c auresamp.c av1.c base64.c diff --git a/test/aupos.c b/test/aupos.c new file mode 100644 index 000000000..4ff40868d --- /dev/null +++ b/test/aupos.c @@ -0,0 +1,61 @@ +/** + * @file src/aupos.c audio file setposition test + * + * Copyright (C) 2023 Lars Immisch + */ + +#include +#include +#include "test.h" + + +#define DEBUG_MODULE "auposition" +#define DEBUG_LEVEL 5 +#include + + +int test_auposition(void) +{ + struct aufile *af = NULL; + struct aufile_prm prm; + char path[256]; + uint8_t buffer[512]; + + re_snprintf(path, sizeof(path), "%s/beep.wav", test_datapath()); + + int err = aufile_open(&af, &prm, path, AUFILE_READ); + TEST_ERR(err); + + err = aufile_set_position(af, &prm, 67); + TEST_ERR(err); + + /* That file is exactly 67 ms long, so we shouldn't read anything */ + size_t size = sizeof(buffer); + err = aufile_read(af, buffer, &size); + TEST_ERR(err); + + /* It's possible we read data up to a ms */ + TEST_ASSERT(size < 16); + + af = mem_deref(af); + + err = aufile_open(&af, &prm, path, AUFILE_READ); + TEST_ERR(err); + + err = aufile_set_position(af, &prm, 37); + TEST_ERR(err); + + size = sizeof(buffer); + err = aufile_read(af, buffer, &size); + TEST_ERR(err); + + /* 30 ms should be left, at 8000Hz/s, one channels and 16 bit samples + that's 480 bytes */ + TEST_ASSERT(size - 480 < 16); + + +out: + mem_deref(af); + + return err; +} diff --git a/test/data/beep.wav b/test/data/beep.wav index a148a48aef8380792d9e36af36f0e50bace99212..2ff0b0d65c42e8c5c9d75267f91527831a442b89 100644 GIT binary patch delta 294 zcmeA%=rdpq@^o`!o5-rnXs}V!n1wYyz&B#m=4_Umtel1+h6Ywf##Y9Yy|`?Vc)eUU zDrpu5rb#Kr#=1#nsY$vfsTQWXiN+RYx`xS?X{IJA7G~xtDU;uFnJb%_C0UpnBwOev zC!1L4niv`y=vtbkB%My#JP73 Sk2ONy$;Ek?{e(5%V@DvfN~4GYBy>vNE0Q%w-E> zc5>OMSePdpnOPbd=q9Hbn(CUEr={s8r6wiorX(7enj57WTO=i!O@7H`u56TSW@%z+ zVWw-4oSLd@l9-aFYiVp`tZS5Nnq*>wpQh51gj diff --git a/test/test.c b/test/test.c index ca71e99a7..cb23ed030 100644 --- a/test/test.c +++ b/test/test.c @@ -55,6 +55,7 @@ static const struct test tests[] = { TEST(test_aubuf), TEST(test_aulength), TEST(test_aulevel), + TEST(test_auposition), TEST(test_auresamp), TEST(test_async), TEST(test_av1), diff --git a/test/test.h b/test/test.h index 306d9bf47..8b8c06e58 100644 --- a/test/test.h +++ b/test/test.h @@ -160,6 +160,7 @@ int test_aes_gcm(void); int test_aubuf(void); int test_aulevel(void); int test_aulength(void); +int test_auposition(void); int test_auresamp(void); int test_async(void); int test_av1(void);