diff mbox series

[FFmpeg-devel,v3,3/5] avcodec/golomb: Document return value of get_ue_golomb_31 on error

Message ID 20200727090810.23794-3-andreas.rheinhardt@gmail.com
State Accepted
Commit 2d5407d390728d4d4a4d8f36a8fe69ff490f54e4
Headers show
Series [FFmpeg-devel,v3,1/5] avcodec/golomb: Don't emit error message in get_ue_golomb | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Andreas Rheinhardt July 27, 2020, 9:08 a.m. UTC
get_ue_golomb_31() reads nine bits and an array with 512 entries to
parse golomb codes. The longest golomb codes that fit into 9 bits use
four leading zeroes and five value bits and can encode numbers in the
0..30 range. 31 meanwhile is encoded on 11 bits and if the nine bits
read coincide with the first nine bits of the encoding of 31,
get_ue_golomb_31() returns 31 (and skips 11 bits).

But looking at the first nine bits only makes it impossible to distinguish
31 from 32..34. Therefore the documentation of get_ue_golomb_31() simply
states that the return value is undefined if the value of the encountered
exp golomb code was outside the 0..31 range.

But actually get_ue_golomb_31() does not behave that bad: If the returned
value is in the range of 0..30, then this is the actually encountered value,
so that this function can be used without any problems to parse and validate
parameters whose legal values are a subset of the 0..30 range.

This commit documents this fact.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/golomb.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Michael Niedermayer July 28, 2020, 7:44 p.m. UTC | #1
On Mon, Jul 27, 2020 at 11:08:08AM +0200, Andreas Rheinhardt wrote:
> get_ue_golomb_31() reads nine bits and an array with 512 entries to
> parse golomb codes. The longest golomb codes that fit into 9 bits use
> four leading zeroes and five value bits and can encode numbers in the
> 0..30 range. 31 meanwhile is encoded on 11 bits and if the nine bits
> read coincide with the first nine bits of the encoding of 31,
> get_ue_golomb_31() returns 31 (and skips 11 bits).
> 
> But looking at the first nine bits only makes it impossible to distinguish
> 31 from 32..34. Therefore the documentation of get_ue_golomb_31() simply
> states that the return value is undefined if the value of the encountered
> exp golomb code was outside the 0..31 range.
> 
> But actually get_ue_golomb_31() does not behave that bad: If the returned
> value is in the range of 0..30, then this is the actually encountered value,
> so that this function can be used without any problems to parse and validate
> parameters whose legal values are a subset of the 0..30 range.
> 
> This commit documents this fact.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavcodec/golomb.h | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)

LGTM

thx

[...]
diff mbox series

Patch

diff --git a/libavcodec/golomb.h b/libavcodec/golomb.h
index aed9b22471..4d531cf805 100644
--- a/libavcodec/golomb.h
+++ b/libavcodec/golomb.h
@@ -116,7 +116,8 @@  static inline unsigned get_ue_golomb_long(GetBitContext *gb)
 
 /**
  * read unsigned exp golomb code, constraint to a max of 31.
- * the return value is undefined if the stored value exceeds 31.
+ * If the value encountered is not in 0..31, the return value
+ * is outside the range 0..30.
  */
 static inline int get_ue_golomb_31(GetBitContext *gb)
 {