[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Xen-devel] [PATCH] Cleanup use of strlen() to check for empty string

  • To: "Petersson, Mats" <mats.petersson@xxxxxxx>, xen-devel@xxxxxxxxxxxxxxxxxxx
  • From: Jacob Gorm Hansen <jacobg@xxxxxxx>
  • Date: Wed, 5 Oct 2005 13:13:21 +0200
  • Delivery-date: Wed, 05 Oct 2005 11:10:58 +0000
  • Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:sender:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=LZYex1oE+494pS9gAlS6fbGK5geUtIJldeFPYrNF2J3UbSOnYYYryia+XE7w9nOadQZDz2/nc7PlWNxxl1yBtnpVmyqNVuDCBK+OWtm9DJIspamDvybyXZb275bRMJLfS3kn04VsM+cGm+cjj1OX7NkMuEmta67C2mjn875KSys=
  • List-id: Xen developer discussion <xen-devel.lists.xensource.com>

On 10/5/05, Petersson, Mats <mats.petersson@xxxxxxx> wrote:

> I created a function:
> int foo(char *s)
> {
>         if (strlen(s)) return 1; else return 0;
> }
> Compiled with gcc 4.0.0 as:
> gcc -O3 -s x.c (or gcc -O2 ...)
> Gives the following code:
> foo:
>         xorl  %eax, %eax
>         cmpb  $(0), (%rdi)
>         sete  %al
>         ret

Cool, I am surprised gcc4 is able to fix this. gcc 3.3.4 which I am
using is not, even with -O3. How does gcc4 behave without -O3?

push   %ebp
mov    %esp,%ebp
mov    0x8(%ebp),%edx
pop    %ebp
cmpb   $0x0,(%edx)
setne  %dl
movzbl %dl,%eax

I guess I only reacted to this because I have seen worse examples
resulting from the "strlen is constant-time" assumption (people
iterating over a string in a while(strlen(s)) loop), and because I was
bored with trying to get block devices running with xenbus. :-)


Xen-devel mailing list



Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.