public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Pavel Vasilyev <pavel@pavlinux.ru>
To: Ming Lei <tom.leiming@gmail.com>
Cc: LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] Repalce strncmp by memcmp
Date: Mon, 29 Nov 2010 06:11:21 +0300	[thread overview]
Message-ID: <4CF319D9.9060102@pavlinux.ru> (raw)
In-Reply-To: <AANLkTikjDfyWY=Gy7VkdvEGNnYB9kb_iCqNDP7H9W5z4@mail.gmail.com>

On 29.11.2010 05:29, Ming Lei wrote:
> Hi,
>
> 2010/11/29 Pavel Vasilyev <pavel@pavlinux.ru>:
>>    This patch replace all strncmp(a, b, c) by  memcmp(a, b, c).
>>
>> I test on x86_64 (AMD Opteron 285).
> In fact, memcmp doesn't handle case of tail of string, so
> it is not safe to replace strncmp with memcmp
>
#include <stdio.h>
#include <errno.h>

int main() {

   char *STR = "XXXX\0";
   char *XXX = "XXXX";
   int a, b;

     errno = 0; a = memcmp(STR, XXX, 5);  a += errno;
     errno = 0; b = strncmp(STR, XXX, 5); b += errno;
     printf("5 chars: %d %d \n", a, b);
     errno = 0; a = memcmp(STR, XXX, 4);  a += errno;
     errno = 0; b = strncmp(STR, XXX, 4); b += errno;
     printf("4 chars: %d %d \n", a, b);

     printf("SWAP STRINGS\n");

     errno = 0; a = memcmp(XXX, STR, 5);  a += errno;
     errno = 0; b = strncmp(XXX, STR, 5); b += errno;
     printf("5 chars: %d %d \n", a, b);
     errno = 0; a = memcmp(XXX, STR, 4);  a += errno;
     errno = 0; b = strncmp(XXX, STR, 4); b += errno;
     printf("4 chars: %d %d \n", a, b);

return 0;
}
----
# ./a.out
5 chars: 0 0
4 chars: 0 0
SWAP STRINGS
5 chars: 0 0
4 chars: 0 0

But I think the same thing  ;)

-- 

                                                         Pavel.


  reply	other threads:[~2010-11-29  3:11 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-29  2:09 [PATCH] Repalce strncmp by memcmp Pavel Vasilyev
2010-11-29  2:21 ` microcai
2010-11-29  2:29 ` Ming Lei
2010-11-29  3:11   ` Pavel Vasilyev [this message]
2010-11-29  4:13     ` Dmitry Torokhov
2010-11-29  5:26       ` YOSHIFUJI Hideaki
2010-11-29 12:41         ` Pavel Vasilyev
2010-11-29  3:10 ` Américo Wang
2010-11-29 10:18 ` Andi Kleen
2010-11-29 14:58 ` Steven Rostedt
2010-11-29 19:41   ` Pavel Vasilyev
2010-11-29 22:18     ` Steven Rostedt
2010-11-29 22:26       ` Steven Rostedt
2010-11-29 22:49         ` Pavel Vasilyev
2010-11-30  9:24           ` Américo Wang
2010-11-29 22:51         ` Ryan Mallon
2010-11-30 10:27           ` Bernd Petrovitsch
2010-11-29 23:32 ` Arnaud Lacombe
2010-11-30 10:34 ` 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=4CF319D9.9060102@pavlinux.ru \
    --to=pavel@pavlinux.ru \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tom.leiming@gmail.com \
    /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