[vlc-commits] [Git][videolan/vlc][master] ts: arib: use early return

Felix Paul Kühne (@fkuehne) gitlab at videolan.org
Thu Jun 30 05:53:14 UTC 2022



Felix Paul Kühne pushed to branch master at VideoLAN / VLC


Commits:
10a6fc63 by Alexandre Janniaux at 2022-06-30T05:41:22+00:00
ts: arib: use early return

It simplifies the handling of the different error cases, and ensure
every case is ending with a return. The 3: case was not ending with a
return before, leading to potential (harmless currently) fallthrough to
the default case.

In addition, between one to two indentation level is saved, and the
further the reader go through the function, the less guarantee there is
to check before usage. For instance, after the beginning, p_dr is set to
be valid without needing to check if we use it in the correct condition
or not. Thus, even the error handling where breaks fall-through is
simplified to just clean up existing objects.

- - - - -


1 changed file:

- modules/demux/mpeg/ts_arib.c


Changes:

=====================================
modules/demux/mpeg/ts_arib.c
=====================================
@@ -172,42 +172,39 @@ ts_arib_logo_dr_t * ts_arib_logo_dr_Decode( const uint8_t *p_data, size_t i_data
         return NULL;
 
     ts_arib_logo_dr_t *p_dr = calloc( 1, sizeof(*p_dr) );
-    if( p_dr )
+    if( unlikely( p_dr == NULL ) )
+        return NULL;
+
+    p_dr->i_logo_version = p_data[0];
+    switch( p_data[0] )
     {
-        p_dr->i_logo_version = p_data[0];
-        switch( p_data[0] )
-        {
-            case 1:
-                if( i_data == 7 )
-                {
-                    p_dr->i_logo_id = ((p_data[1] & 0x01) << 8) | p_data[2];
-                    p_dr->i_logo_version = ((p_data[3] & 0x0F) << 8) | p_data[4];
-                    p_dr->i_download_data_id = (p_data[5] << 8) | p_data[6];
-                    return p_dr;
-                }
+        case 1:
+            if( i_data != 7 )
                 break;
-            case 2:
-                if( i_data == 3 )
-                {
-                    p_dr->i_logo_id = ((p_data[1] & 0x01) << 8) | p_data[2];
-                    return p_dr;
-                }
+            p_dr->i_logo_id = ((p_data[1] & 0x01) << 8) | p_data[2];
+            p_dr->i_logo_version = ((p_data[3] & 0x0F) << 8) | p_data[4];
+            p_dr->i_download_data_id = (p_data[5] << 8) | p_data[6];
+            return p_dr;
+        case 2:
+            if( i_data != 3 )
                 break;
-            case 3:
-                if( i_data > 2 )
-                {
-                    p_dr->p_logo_char = malloc( i_data - 1 );
-                    if( p_dr->p_logo_char )
-                    {
-                        p_dr->i_logo_char = i_data - 1;
-                        memcpy( p_dr->p_logo_char, &p_data[1], p_dr->i_logo_char );
-                        return p_dr;
-                    }
-                }
-            default:
+            p_dr->i_logo_id = ((p_data[1] & 0x01) << 8) | p_data[2];
+            return p_dr;
+        case 3:
+            if( i_data <= 2 )
                 break;
-        }
-        ts_arib_logo_dr_Delete( p_dr );
+
+            p_dr->p_logo_char = malloc( i_data - 1 );
+            if( unlikely( p_dr->p_logo_char == NULL ) )
+                break;
+
+            p_dr->i_logo_char = i_data - 1;
+            memcpy( p_dr->p_logo_char, &p_data[1], p_dr->i_logo_char );
+            return p_dr;
+        default:
+            break;
     }
+
+    ts_arib_logo_dr_Delete( p_dr );
     return NULL;
 }



View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/10a6fc63815db36d5fedd7acb6077888b262551c

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/10a6fc63815db36d5fedd7acb6077888b262551c
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