[pacman-dev] [PATCH 4/4] Change strcat function using for strncat

Laszlo Papp djszapi at archlinux.us
Sun Oct 18 06:32:38 EDT 2009


On Sun, Oct 18, 2009 at 12:03 PM, Xavier <shiningxc at gmail.com> wrote:

> On Sun, Oct 18, 2009 at 6:04 AM, Laszlo Papp <djszapi2 at gmail.com> wrote:
> >        * Size examined str* function usage is a common coding practice,
> >        because it's more safer to avoid breakage while using str*
> functions.
> >
> > Signed-off-by: Laszlo Papp <djszapi at archlinux.us>
> > ---
> >  lib/libalpm/util.c |    2 +-
> >  1 files changed, 1 insertions(+), 1 deletions(-)
> >
> > diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
> > index 553591d..41a5783 100644
> > --- a/lib/libalpm/util.c
> > +++ b/lib/libalpm/util.c
> > @@ -105,7 +105,7 @@ int _alpm_makepath_mode(const char *path, mode_t
> mode)
> >                        /* we have another path component- append the
> newest component to
> >                         * existing string and create one more level of
> dir structure */
> >                        strcat(incr, "/");
> > -                       strcat(incr, ptr);
> > +                       strncat(incr, ptr, strlen(ptr));
> >                        if(access(incr, F_OK)) {
> >                                if(mkdir(incr, mode)) {
> >                                        ret = 1;
>
> This is useless too.
> These two are equivalent, so we might as well go for the simpler one.
>
>

Xavier, you're right!

But in this mode, it's more safer/better to handle strncat.

sdst = sizeof(dst)-strlen(dst)-1;
ssrc = sizeof(src)

n = (sdst < ssrc ? sdst : ssrc) ; strncat (dst, src , n); dst[strlen(dst)+n]
= '\0';

Best Regards,
Laszlo Papp


More information about the pacman-dev mailing list