[vlc-commits] [Git][videolan/vlc][master] 2 commits: codec: telx: fix truncation warning

Steve Lhomme (@robUx4) gitlab at videolan.org
Thu Aug 24 08:49:58 UTC 2023



Steve Lhomme pushed to branch master at VideoLAN / VLC


Commits:
6a798b46 by Alexandre Janniaux at 2023-08-24T08:29:33+00:00
codec: telx: fix truncation warning

Fix the -Wstringop-truncation warning by setting a correct size to the
line being read:

    ../../modules/codec/telx.c: In function ‘Decode’:
    ../../modules/codec/telx.c:583:9: warning: ‘__builtin_strncpy’ output may be truncated copying 127 bytes from a string of length 255 [-Wstringop-truncation]
      583 |         strncpy( p_sys->ppsz_lines[0], psz_line,
          |         ^
    ../../modules/codec/telx.c:535:9: warning: ‘__builtin_strncpy’ output may be truncated copying 127 bytes from a string of length 255 [-Wstringop-truncation]
      535 |         strncpy( p_sys->ppsz_lines[row], t,
          |         ^
    ../../modules/codec/telx.c:501:9: warning: ‘__builtin_strncpy’ output may be truncated copying 127 bytes from a string of length 255 [-Wstringop-truncation]
      501 |         strncpy( p_sys->ppsz_lines[0], psz_line,
          |         ^

Since at most `len` characters are read from the input, and an utf-8
caracter is written in the output for each character from the input,
and since the to_utf8 function only supports utf-8 characters with at
most 3 characters, then it's not possible to write more than 3 times len
into the output buffer, without accounting the terminating character.

Additional related changes are made to:

 - Move the creation of psz_line close to decode_string, since that's
   the main first user and it needs the size of the buffer, helping
   readability.

 - Add assertion checking that we don't extend utf-8 to 4 bytes, which
   is mainly for development, since the function will not outreach the
   bounds of psz_line anyway.

 - Add some documentation for `decode_string` so it's clearer how the
   length of the buffer should be assigned.

- - - - -
b63962e2 by Alexandre Janniaux at 2023-08-24T08:29:33+00:00
codec: telx: strncpy -> strlcpy

The destination string must be zero-terminated, so strlcpy is the
correct choice to ensure that.

- - - - -


1 changed file:

- modules/codec/telx.c


Changes:

=====================================
modules/codec/telx.c
=====================================
@@ -325,6 +325,11 @@ static void to_utf8( char * res, uint16_t ch )
     }
 }
 
+/**
+ * Decode a packet, potentially decoding strings from utc-2 to utf-8.
+ *
+ * Decoding a packet of size \p len will write at most `len * 3 + 1`.
+ */
 static void decode_string( char * res, int res_len,
                            decoder_sys_t *p_sys, int magazine,
                            const uint8_t * packet, int len )
@@ -410,6 +415,7 @@ static void decode_string( char * res, int res_len,
         /* convert to utf-8 */
         to_utf8( utf8, out );
         l = strlen( utf8 );
+        assert(l < 4);
         if ( pt + l < res + res_len - 1 )
         {
             strcpy(pt, utf8);
@@ -428,7 +434,6 @@ static bool DecodePageHeaderPacket( decoder_t *p_dec, const uint8_t *packet,
     decoder_sys_t *p_sys = p_dec->p_sys;
 
     int flag = 0;
-    char psz_line[256];
 
     for ( int a = 0; a < 6; a++ )
     {
@@ -442,6 +447,7 @@ static bool DecodePageHeaderPacket( decoder_t *p_dec, const uint8_t *packet,
     p_sys->i_page[magazine] = (0xF0 & bytereverse( hamming_8_4(packet[7]) )) | /* tens */
                               (0x0F & (bytereverse( hamming_8_4(packet[6]) ) >> 4) ); /* units */
 
+    char psz_line[(40 - 14) * 3 + 1];
     decode_string( psz_line, sizeof(psz_line), p_sys, magazine,
                    packet + 14, 40 - 14 );
 
@@ -498,8 +504,8 @@ static bool DecodePageHeaderPacket( decoder_t *p_dec, const uint8_t *packet,
     /* replace the row if it's different */
     if ( strcmp(psz_line, p_sys->ppsz_lines[0]) )
     {
-        strncpy( p_sys->ppsz_lines[0], psz_line,
-                 sizeof(p_sys->ppsz_lines[0]) - 1);
+        strlcpy( p_sys->ppsz_lines[0], psz_line,
+                 sizeof(p_sys->ppsz_lines[0]) );
     }
 
     return true;
@@ -511,13 +517,13 @@ static bool DecodePacketX1_X23( decoder_t *p_dec, const uint8_t *packet,
     decoder_sys_t *p_sys = p_dec->p_sys;
 
     bool b_update = false;
-    char psz_line[256];
     char * t;
     int i;
 
     if ( p_sys->i_wanted_page == -1 && p_sys->i_page[magazine] > 0x99)
         return false;
 
+    char psz_line[40 * 3 + 1];
     decode_string( psz_line, sizeof(psz_line), p_sys, magazine,
                    packet + 6, 40 );
     t = psz_line;
@@ -532,8 +538,8 @@ static bool DecodePacketX1_X23( decoder_t *p_dec, const uint8_t *packet,
     /* replace the row if it's different */
     if ( strcmp( t, p_sys->ppsz_lines[row] ) )
     {
-        strncpy( p_sys->ppsz_lines[row], t,
-                 sizeof(p_sys->ppsz_lines[row]) - 1 );
+        strlcpy( p_sys->ppsz_lines[row], t,
+                 sizeof(p_sys->ppsz_lines[row]) );
         b_update = true;
     }
 
@@ -573,15 +579,15 @@ static bool DecodePacketX25( decoder_t *p_dec, const uint8_t *packet,
     decoder_sys_t *p_sys = p_dec->p_sys;
 
     /* row 25 : alternate header line */
-    char psz_line[256];
+    char psz_line[40 * 3 + 1];
     decode_string( psz_line, sizeof(psz_line), p_sys, magazine,
                    packet + 6, 40 );
 
     /* replace the row if it's different */
     if ( strcmp( psz_line, p_sys->ppsz_lines[0] ) )
     {
-        strncpy( p_sys->ppsz_lines[0], psz_line,
-                 sizeof(p_sys->ppsz_lines[0]) - 1 );
+        strlcpy( p_sys->ppsz_lines[0], psz_line,
+                 sizeof(p_sys->ppsz_lines[0]) );
         /* return true; */
     }
 



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/d1b3bbf3ed7a8b0de1a6fec21762e1cddcc22e50...b63962e2927e42afb636ae3a93f2ccb4af37db17

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/d1b3bbf3ed7a8b0de1a6fec21762e1cddcc22e50...b63962e2927e42afb636ae3a93f2ccb4af37db17
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list