diff mbox series

[FFmpeg-devel] lavu/common: fix PUT_UTF8() length

Message ID 20220506135751.112173-1-george@nsup.org
State New
Headers show
Series [FFmpeg-devel] lavu/common: fix PUT_UTF8() length | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Nicolas George May 6, 2022, 1:57 p.m. UTC
Ensure the input is <= 31 bits because otherwise it would produce
very invalid UTF-8.

Document that the length can be up to 6 bytes, because that is what
the code does. The FLAC encoder abuses UTF-8 to store 32-bits
integers, limiting PUT_UTF8() to 21 bits might affect some real
cases negatively.

Signed-off-by: Nicolas George <george@nsup.org>
---
 libavutil/common.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/libavutil/common.h b/libavutil/common.h
index fd1404be6c..7a71be3c81 100644
--- a/libavutil/common.h
+++ b/libavutil/common.h
@@ -505,7 +505,9 @@  static av_always_inline av_const int av_parity_c(uint32_t v)
 
 /**
  * @def PUT_UTF8(val, tmp, PUT_BYTE)
- * Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
+ * Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 6
+ * bytes long, but codes more than 4 bytes long, i.e. codepoints above
+ * 0x001FFFFF, are deprecated).
  * @param val is an input-only argument and should be of type uint32_t. It holds
  * a UCS-4 encoded Unicode character that is to be converted to UTF-8. If
  * val is given as a function it is executed only once.
@@ -522,7 +524,7 @@  static av_always_inline av_const int av_parity_c(uint32_t v)
 #define PUT_UTF8(val, tmp, PUT_BYTE)\
     {\
         int bytes, shift;\
-        uint32_t in = val;\
+        uint32_t in = val & 0x7FFFFFFF;\
         if (in < 0x80) {\
             tmp = in;\
             PUT_BYTE\