All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paul Moore <paul.moore@hp.com>
To: James Antill <james.antill@redhat.com>
Cc: Stephen Smalley <sds@tycho.nsa.gov>, selinux@tycho.nsa.gov
Subject: Re: [RFC PATCH] SELinux: Cleanup the secid/secctx conversion functions
Date: Fri, 29 Feb 2008 12:03:40 -0500	[thread overview]
Message-ID: <200802291203.40204.paul.moore@hp.com> (raw)
In-Reply-To: <1204303255.11268.326.camel@code.and.org>

On Friday 29 February 2008 11:40:55 am James Antill wrote:
> On Fri, 2008-02-29 at 09:13 -0500, Stephen Smalley wrote:
> > On Fri, 2008-02-29 at 08:54 -0500, Stephen Smalley wrote:
> > > The snippet below looks like a step backward rather than an
> > > improvement - single sprintf replaced by series of strcat calls. 
> > > That can't be more efficient.
> >
> > Hmm...well, maybe I'm wrong (after looking at the implementations).
> > Pity that Linux doesn't have stpcpy (as in glibc) - that is much
> > nicer than a series of strcat's since it returns the end pointer
> > and doesn't require finding the end of string each time.
>
>  Note that you can do (only slightly abusing the interface):
>
> 	ctx = *scontext;
>
> 	ctx += strlcpy(ctx, policydb.p_user_val_to_name[context->user - 1],
> -1); ctx += strlcpy(ctx, ":", -1);
> 	ctx += strlcpy(ctx, policydb.p_role_val_to_name[context->role - 1],
> -1); ctx += strlcpy(ctx, ":", -1);
> 	ctx += strlcpy(ctx, policydb.p_type_val_to_name[context->type - 1],
> -1);
>
> ...which is basically a memcpy() with a simple if test.

... except for that strlen() call right at the top, there are also two 
comparisons (you only accounted for one) and a add/subtract 
operation :)

[NOTE: I'm comparing the architecture independent versions of these 
functions]

The strlcpy() implementation does a strlen() and a memcpy() call along 
with two comparisons and some trivial math.  The strcat() 
implementation does pretty much the same thing but with less 
comparisons and no additional math.  The strcpy() implementation only 
does a byte-by-byte copy which should be faster then strlcpy() even 
when one factors in an optimized memcpy() implementation due to the 
strlen() call.

My conclusion is that the strcpy()/strcat() approach should be at least 
as fast as the strlcpy() approach if not a smidge faster.

> > > > -	*scontextp = 0;
> > > > +	ctx = *scontext;
> > > > +	strcpy(ctx, policydb.p_user_val_to_name[context->user - 1]);
> > > > +	strcat(ctx, ":");
> > > > +	strcat(ctx, policydb.p_role_val_to_name[context->role - 1]);
> > > > +	strcat(ctx, ":");
> > > > +	strcat(ctx, policydb.p_type_val_to_name[context->type - 1]);

-- 
paul moore
linux security @ hp

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

  reply	other threads:[~2008-02-29 17:03 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-28 21:41 [RFC PATCH] SELinux: Cleanup the secid/secctx conversion functions Paul Moore
2008-02-29  9:49 ` James Morris
2008-02-29 13:27   ` Paul Moore
2008-02-29 13:54 ` Stephen Smalley
2008-02-29 14:13   ` Stephen Smalley
2008-02-29 14:23     ` Paul Moore
2008-02-29 16:40     ` James Antill
2008-02-29 17:03       ` Paul Moore [this message]
2008-02-29 18:41         ` Todd Miller
2008-02-29 18:58           ` Paul Moore

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200802291203.40204.paul.moore@hp.com \
    --to=paul.moore@hp.com \
    --cc=james.antill@redhat.com \
    --cc=sds@tycho.nsa.gov \
    --cc=selinux@tycho.nsa.gov \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.