From: Steven Rostedt <rostedt@goodmis.org>
To: pavel@pavlinux.ru
Cc: LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] Repalce strncmp by memcmp
Date: Mon, 29 Nov 2010 09:58:57 -0500 [thread overview]
Message-ID: <1291042737.30543.730.camel@gandalf.stny.rr.com> (raw)
In-Reply-To: <4CF30B46.4000203@pavlinux.ru>
On Mon, 2010-11-29 at 05:09 +0300, Pavel Vasilyev wrote:
> This patch replace all strncmp(a, b, c) by memcmp(a, b, c).
But these are not the same. strncmp() will stop when a or b hit a null.
I'm not sure if memcmp() must do so, It may for some reason check
anything within the memory of a+c-1 or b+c-1. What happens if a or b are
right at the end of a vmalloc page, and is just a single character and
null?
x = vmalloc(32);
strcpy(x, "some 31 byte string + null");
call_func(x + 31);
in call_func we have:
call_func(char *a) {
strncmp(a, "this is some big string", 23);
With strncmp() when we hit a+1, it will stop comparing because a+1 is
null. With memcmp there's no such guarantee. We can then take a kernel
oops.
That will be a nice thing to try to debug.
Yes the above is contrived, but it demonstrates a possible problem with
this conversion.
-- Steve
>
> I test on x86_64 (AMD Opteron 285).
>
> #include <string.h>
> char *A = "0000";
> void test_memcmp(void) {
> memcmp(A, "TEST", 4);
> }
> void test_strn(void) {
> strncmp(A, "TEST", 4);
> }
> # gcc -c -O2 test.c
> # objdump -d test.o
> ...
>
> 0000000000000020 <test_strncmp>:
> 20: f3 c3 repz retq
> 22: 66 66 66 66 66 2e 0f data32 data32 data32 data32 nopw
> %cs:0x0(%rax,%rax,1)
> 29: 1f 84 00 00 00 00 00
>
> 0000000000000030 <test_memcmp>:
> 30: f3 c3 repz retq
>
> Wow, minus one commad :)
>
next prev parent reply other threads:[~2010-11-29 14:59 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-11-29 2:09 Pavel Vasilyev
2010-11-29 2:21 ` microcai
2010-11-29 2:29 ` Ming Lei
2010-11-29 3:11 ` Pavel Vasilyev
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 [this message]
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=1291042737.30543.730.camel@gandalf.stny.rr.com \
--to=rostedt@goodmis.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pavel@pavlinux.ru \
/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
all inboxes | Powered by JetHome®