[vlc-devel] [PATCH] ogg: fix stringop-truncation warning

Alexandre Janniaux ajanni at videolabs.io
Thu Sep 5 12:28:29 CEST 2019


Hi,

I thought it was part of C89, but it doesn't seem so:
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/posix-memccpy?view=vs-2019

strncpy will add a trailing \0 character, which means that if
you copy 4 chars, only three will be in the sub_type field.

memccpy is needed here in the situation where snprintf writes
only 3 chars or less (including null byte), because it doesn't
pad with zero, so doesn't initialize the other bytes in subtype.
I don't know if it can happen in practice (subtype is usually
3 or 4 bytes if my reading of the doc is correct) but it seems
more correct to me.

To stay closer to the c standard, it could be replaced by
strlen + memcpy, or maybe a better alternative that I don't
know.

Regards,
--
Alexandre Janniaux
Videolabs

On Thu, Sep 05, 2019 at 08:23:57AM +0200, Steve Lhomme wrote:
> Is memccpy() part of the C standard ?
> What's wrong with strncpy ?
>
> On 2019-09-05 1:50, Alexandre Janniaux wrote:
> > ---
> >   modules/mux/ogg.c | 5 +++--
> >   1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/modules/mux/ogg.c b/modules/mux/ogg.c
> > index 82e5e90aa2..4a256b4d6a 100644
> > --- a/modules/mux/ogg.c
> > +++ b/modules/mux/ogg.c
> > @@ -499,10 +499,11 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
> >
> >               memcpy( p_stream->p_oggds_header->stream_type, "audio", 5 );
> >
> > -            memset( p_stream->p_oggds_header->sub_type, 0, 4 );
> >               char buf[5];
> >               snprintf( buf, sizeof(buf), "%"PRIx16, i_tag );
> > -            strncpy( p_stream->p_oggds_header->sub_type, buf, 4 );
> > +
> > +            memset( p_stream->p_oggds_header->sub_type, 0, 4 );
> > +            memccpy( p_stream->p_oggds_header->sub_type, buf, '\0', 4 );
> >
> >               p_stream->p_oggds_header->i_time_unit = MSFTIME_FROM_SEC(1);
> >               p_stream->p_oggds_header->i_default_len = 1;
> > --
> > 2.23.0
> >
> > _______________________________________________
> > vlc-devel mailing list
> > To unsubscribe or modify your subscription options:
> > https://mailman.videolan.org/listinfo/vlc-devel
> >
> _______________________________________________
> vlc-devel mailing list
> To unsubscribe or modify your subscription options:
> https://mailman.videolan.org/listinfo/vlc-devel


More information about the vlc-devel mailing list