[FFmpeg-devel,v2] avcodec/h2645_parse: refine the code for better readiablity

Submitted by lance.lmwang@gmail.com on Sept. 10, 2019, 5:38 a.m.

Details

Message ID 20190910053827.30410-1-lance.lmwang@gmail.com
State New
Headers show

Commit Message

lance.lmwang@gmail.com Sept. 10, 2019, 5:38 a.m.
From: Limin Wang <lance.lmwang@gmail.com>

Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
---
 libavcodec/h2645_parse.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

Comments

Andriy Gelman Sept. 10, 2019, 9:27 a.m.
On Tue, 10. Sep 13:38, lance.lmwang@gmail.com wrote:
> From: Limin Wang <lance.lmwang@gmail.com>
> 
> Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
> ---
>  libavcodec/h2645_parse.c | 10 ++++------
>  1 file changed, 4 insertions(+), 6 deletions(-)
> 
> diff --git a/libavcodec/h2645_parse.c b/libavcodec/h2645_parse.c
> index 307e8643e6..f077900617 100644
> --- a/libavcodec/h2645_parse.c
> +++ b/libavcodec/h2645_parse.c
> @@ -453,16 +453,15 @@ int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
>              }
>          }
>  
> -        if (pkt->nals_allocated < pkt->nb_nals + 1) {
> -            int new_size = pkt->nals_allocated + 1;
> -            void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
> +        if (pkt->nb_nals >= pkt->nals_allocated) {
> +            void *tmp = av_realloc_array(pkt->nals, pkt->nals_allocated + 1, sizeof(*pkt->nals));
>  
>              if (!tmp)
>                  return AVERROR(ENOMEM);
>  
>              pkt->nals = tmp;
> -            memset(pkt->nals + pkt->nals_allocated, 0,
> -                   (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));

> +            memset(pkt->nals + pkt->nals_allocated, 0, sizeof(*pkt->nals));

if you review a patch you should send comments, not submit your own version. 

Andriy

> +            pkt->nals_allocated++;
>  
>              nal = &pkt->nals[pkt->nb_nals];
>              nal->skipped_bytes_pos_size = 1024; // initial buffer size
> @@ -470,7 +469,6 @@ int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
>              if (!nal->skipped_bytes_pos)
>                  return AVERROR(ENOMEM);
>  
> -            pkt->nals_allocated = new_size;
>          }
>          nal = &pkt->nals[pkt->nb_nals];
>  
> -- 
> 2.21.0
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
James Almer Sept. 11, 2019, 1:29 p.m.
On 9/10/2019 2:38 AM, lance.lmwang@gmail.com wrote:
> From: Limin Wang <lance.lmwang@gmail.com>
> 
> Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
> ---
>  libavcodec/h2645_parse.c | 10 ++++------
>  1 file changed, 4 insertions(+), 6 deletions(-)
> 
> diff --git a/libavcodec/h2645_parse.c b/libavcodec/h2645_parse.c
> index 307e8643e6..f077900617 100644
> --- a/libavcodec/h2645_parse.c
> +++ b/libavcodec/h2645_parse.c
> @@ -453,16 +453,15 @@ int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
>              }
>          }
>  
> -        if (pkt->nals_allocated < pkt->nb_nals + 1) {
> -            int new_size = pkt->nals_allocated + 1;
> -            void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
> +        if (pkt->nb_nals >= pkt->nals_allocated) {
> +            void *tmp = av_realloc_array(pkt->nals, pkt->nals_allocated + 1, sizeof(*pkt->nals));
>  
>              if (!tmp)
>                  return AVERROR(ENOMEM);
>  
>              pkt->nals = tmp;
> -            memset(pkt->nals + pkt->nals_allocated, 0,
> -                   (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
> +            memset(pkt->nals + pkt->nals_allocated, 0, sizeof(*pkt->nals));
> +            pkt->nals_allocated++;

The nal->skipped_bytes_pos allocation below can still fail. This counter
shouldn't increase until all allocations have succeeded.

>  
>              nal = &pkt->nals[pkt->nb_nals];
>              nal->skipped_bytes_pos_size = 1024; // initial buffer size
> @@ -470,7 +469,6 @@ int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
>              if (!nal->skipped_bytes_pos)
>                  return AVERROR(ENOMEM);
>  
> -            pkt->nals_allocated = new_size;
>          }
>          nal = &pkt->nals[pkt->nb_nals];
>  
>
lance.lmwang@gmail.com Sept. 11, 2019, 3:07 p.m.
On Wed, Sep 11, 2019 at 10:29:14AM -0300, James Almer wrote:
> On 9/10/2019 2:38 AM, lance.lmwang@gmail.com wrote:
> > From: Limin Wang <lance.lmwang@gmail.com>
> > 
> > Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
> > ---
> >  libavcodec/h2645_parse.c | 10 ++++------
> >  1 file changed, 4 insertions(+), 6 deletions(-)
> > 
> > diff --git a/libavcodec/h2645_parse.c b/libavcodec/h2645_parse.c
> > index 307e8643e6..f077900617 100644
> > --- a/libavcodec/h2645_parse.c
> > +++ b/libavcodec/h2645_parse.c
> > @@ -453,16 +453,15 @@ int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
> >              }
> >          }
> >  
> > -        if (pkt->nals_allocated < pkt->nb_nals + 1) {
> > -            int new_size = pkt->nals_allocated + 1;
> > -            void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
> > +        if (pkt->nb_nals >= pkt->nals_allocated) {
> > +            void *tmp = av_realloc_array(pkt->nals, pkt->nals_allocated + 1, sizeof(*pkt->nals));
> >  
> >              if (!tmp)
> >                  return AVERROR(ENOMEM);
> >  
> >              pkt->nals = tmp;
> > -            memset(pkt->nals + pkt->nals_allocated, 0,
> > -                   (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
> > +            memset(pkt->nals + pkt->nals_allocated, 0, sizeof(*pkt->nals));
> > +            pkt->nals_allocated++;
> 
> The nal->skipped_bytes_pos allocation below can still fail. This counter
> shouldn't increase until all allocations have succeeded.
Yes, if the next line code use pkt->nals_allocated to get nal to allocate 
by nal->skipped_bytes_pos then it's easy understand to move the counter
until all allocation are done.


> 
> >  
> >              nal = &pkt->nals[pkt->nb_nals];
> >              nal->skipped_bytes_pos_size = 1024; // initial buffer size
> > @@ -470,7 +469,6 @@ int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
> >              if (!nal->skipped_bytes_pos)
> >                  return AVERROR(ENOMEM);
> >  
> > -            pkt->nals_allocated = new_size;
> >          }
> >          nal = &pkt->nals[pkt->nb_nals];
> >  
> > 
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

Patch hide | download patch | download mbox

diff --git a/libavcodec/h2645_parse.c b/libavcodec/h2645_parse.c
index 307e8643e6..f077900617 100644
--- a/libavcodec/h2645_parse.c
+++ b/libavcodec/h2645_parse.c
@@ -453,16 +453,15 @@  int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
             }
         }
 
-        if (pkt->nals_allocated < pkt->nb_nals + 1) {
-            int new_size = pkt->nals_allocated + 1;
-            void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
+        if (pkt->nb_nals >= pkt->nals_allocated) {
+            void *tmp = av_realloc_array(pkt->nals, pkt->nals_allocated + 1, sizeof(*pkt->nals));
 
             if (!tmp)
                 return AVERROR(ENOMEM);
 
             pkt->nals = tmp;
-            memset(pkt->nals + pkt->nals_allocated, 0,
-                   (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
+            memset(pkt->nals + pkt->nals_allocated, 0, sizeof(*pkt->nals));
+            pkt->nals_allocated++;
 
             nal = &pkt->nals[pkt->nb_nals];
             nal->skipped_bytes_pos_size = 1024; // initial buffer size
@@ -470,7 +469,6 @@  int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
             if (!nal->skipped_bytes_pos)
                 return AVERROR(ENOMEM);
 
-            pkt->nals_allocated = new_size;
         }
         nal = &pkt->nals[pkt->nb_nals];