linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Anthony Truong <Anthony.Truong@mascorp.com>
To: Willy Tarreau <willy@w.ods.org>
Cc: Albert Cahalan <albert@users.sourceforge.net>,
	andersen@codepoet.org,
	linux-kernel mailing list <linux-kernel@vger.kernel.org>,
	bernd@firmix.at, alan@lxorguk.ukuu.org.uk, schwab@suse.de,
	ysato@users.sourceforge.jp, Valdis.Kletnieks@vt.edu,
	william.gallafent@virgin.net
Subject: Re: generic strncpy - off-by-one error
Date: Tue, 12 Aug 2003 20:09:00 -0700	[thread overview]
Message-ID: <1060744140.15792.17.camel@huykhoi> (raw)

On Wed, 2003-08-13 at 13:18, Willy Tarreau wrote:

On Tue, Aug 12, 2003 at 11:38:31PM -0400, Albert Cahalan wrote:
> That's excellent. On ppc I count 12 instructions,
> 4 of which would go away for typical usage if inlined.
> Annoyingly, gcc doesn't get the same assembly from my
> attempt at that general idea:
> 
> char * strncpy_5(char *dest, const char *src, size_t count){
>   char *tmp = dest;
>   while (count--){
>     if(( *tmp++ = *src )) src++;
>   }
>   return dest;
> }
> 
> I suppose that gcc could use a bug report.

I often noticed that using '++' and '--' within or just before
assignments
and/or comparisons often break the code and make it suboptimal. C
provides
enough flexibility to code what you think nearly at the instruction
level.
Since 'while' loops often start with a jump to the end, you can
sometimes help
the compiler by enclosing them within an 'if' statement such as below.
BTW, in
your case, count ends with -1.

I've absolutely not tried this one, but it could produce different code
on your
PPC, and can trivially be derived to cleaner constructs. I proceeded the
same
way when I wrote my own optimized strlcpy() implementation which is 45
bytes
long and copies 1 char per CPU cycle on i686.

char *strncpy(char *dest, const char *src, size_t count)
{
   if (count) {
      char *tmp = dest;
      while (1) {
         *tmp = *src;
         if (*src) src++;
         tmp++;
         if (!count--) break;
      }
   }
   return dest;
}

Cheers,
Willy


Hello,
This reminds me of my days in school learning how to program in C/C++. 
I first learnt that in
  while (count-- && (*dest++ = *src++));
  the first operand of && will be evaluated first, and then the second
  iff the first is evaluated to TRUE.
  count-- : count will be checked to see if it is not 0 first, and if it
  is not, it will get decremented.  If it is, the while loop is ended.
  Therefore, how could count go to -1?
If there is a bug in the gcc, then should we fix it rather than messing 
with our coding trying to please it?
We don't have to do if (count) before the loop because a count of 0 is 
already caught in while (count-- ......).

Again, maybe what I learnt in school about C/C++ programming is all
wrong.:-)

:-)
Anthony Dominic Truong.




Disclaimer: The information contained in this transmission, including any
attachments, may contain confidential information of Matsushita Avionics
Systems Corporation.  This transmission is intended only for the use of the
addressee(s) listed above.  Unauthorized review, dissemination or other use
of the information contained in this transmission is strictly prohibited.
If you have received this transmission in error or have reason to believe
you are not authorized to receive it, please notify the sender by return
email and promptly delete the transmission.



             reply	other threads:[~2003-08-13 17:57 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-08-13  3:09 Anthony Truong [this message]
  -- strict thread matches above, loose matches on Subject: below --
2003-08-20  7:43 generic strncpy - off-by-one error Peter Kjellerstedt
2003-08-16 21:10 Peter Kjellerstedt
2003-08-18 18:41 ` Timothy Miller
2003-08-16 20:08 Peter Kjellerstedt
2003-08-16  9:19 Peter Kjellerstedt
2003-08-16 10:04 ` Daniel Forrest
2003-08-18 16:40   ` Timothy Miller
2003-08-16  8:15 Peter Kjellerstedt
2003-08-16  8:41 ` Daniel Forrest
2003-08-18 16:17   ` Timothy Miller
2003-08-18 16:06 ` Timothy Miller
2003-08-15  9:54 Peter Kjellerstedt
2003-08-15 17:52 ` Timothy Miller
2003-08-15  9:53 Peter Kjellerstedt
2003-08-15 17:47 ` Timothy Miller
2003-08-14  9:34 Peter Kjellerstedt
2003-08-14 19:45 ` Willy Tarreau
2003-08-14 20:24 ` Timothy Miller
2003-08-13  2:18 Albert Cahalan
2003-08-13  2:47 ` Erik Andersen
2003-08-13  3:38   ` Albert Cahalan
2003-08-13  3:56     ` Nick Piggin
2003-08-13  5:18     ` Willy Tarreau
2003-08-13 19:03   ` Timothy Miller
2003-08-12 14:07 Yoshinori Sato
2003-08-12 14:39 ` Willy Tarreau
2003-08-12 14:50 ` Yoshinori Sato
2003-08-12 15:03   ` Valdis.Kletnieks
2003-08-12 15:54     ` William Gallafent
2003-08-12 16:19       ` Valdis.Kletnieks
2003-08-12 16:09     ` Andreas Schwab
2003-08-12  1:56 Anthony Truong
2003-08-12 17:14 ` Alan Cox
2003-08-12 21:53   ` Bernd Petrovitsch
2003-08-12  1:28 Anthony Truong
2003-08-12 16:24 ` Bernd Petrovitsch

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=1060744140.15792.17.camel@huykhoi \
    --to=anthony.truong@mascorp.com \
    --cc=Valdis.Kletnieks@vt.edu \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=albert@users.sourceforge.net \
    --cc=andersen@codepoet.org \
    --cc=bernd@firmix.at \
    --cc=linux-kernel@vger.kernel.org \
    --cc=schwab@suse.de \
    --cc=william.gallafent@virgin.net \
    --cc=willy@w.ods.org \
    --cc=ysato@users.sourceforge.jp \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).