Re: [PATCH] media: dvb_ringbuffer : Fix a bug in dvb_ringbuffer.c
From: Hans Petter Selasky
Date: Mon Sep 12 2022 - 08:36:24 EST
Hi Mauro and YongSu,
Answering my own question: The reason nobody has triggered this yet, is
because the buffer size used is power of two. Because unsigned modulus
is used, the result becomes correct. See below. But if non-power of two
ring-buffer is used, then the result becomes incorrect. There is no
block for non-power of two sized buffers. See:
https://github.com/search?q=dvb_set_pesfilter&type=code
cat << EOF > testX.c
#include <stdio.h>
int
main()
{
int consumed_old;
int consumed_fix;
size_t idx = 3;
ssize_t pread = 15;
ssize_t size = 256;
consumed_old = (idx - pread) % size;
consumed_fix = (idx - pread);
if (consumed_fix < 0)
consumed_fix += size;
printf("old=%d new=%d size=%zd\n", consumed_old, consumed_fix, size);
size = 254;
consumed_old = (idx - pread) % size;
consumed_fix = (idx - pread);
if (consumed_fix < 0)
consumed_fix += size;
printf("old=%d new=%d size=%zd\n", consumed_old, consumed_fix, size);
return (0);
}
EOF
cc testX.c && ./a.out
old=244 new=244 size=256
old=244 new=242 size=254
So either push the suggested fix, or block non-power of two buffer sizes!
Best regards,
--HPS