Patchwork Use strlen for const strings

login
register
about
Submitter Uwe Hermann
Date 2011-06-18 21:12:00
Message ID <20110618211200.GF5610@greenwood>
Download mbox | patch
Permalink /patch/3168/
State Accepted
Commit r1352
Headers show

Comments

Uwe Hermann - 2011-06-18 21:12:00
On Fri, Oct 15, 2010 at 02:06:16PM +0200, David Borg wrote:
> Carl-Daniel, please run 'svn up' in the source directory before committing,
> as the patch won't apply directly as-is. svn will handle the merge
> correctly.
> Thanks!

Updated patch attached. It's also
Acked-by: Uwe Hermann <uwe@hermann-uwe.de>


Uwe.
Carl-Daniel Hailfinger - 2011-06-26 11:51:42
Am 18.06.2011 23:12 schrieb Uwe Hermann:
> On 15 October 2010 01:47, Sean Nelson <audiohacked@gmail.com> wrote:
>> Acked-by: Sean Nelson <audiohacked@gmail.com>
> Acked-by: Uwe Hermann <uwe@hermann-uwe.de>

Thanks, committed in r1352.

Regards,
Carl-Daniel

Patch

Replace sizeof("string")-1 with strlen("string")

We want to avoid calls to strlen at runtime if the string is already
known at compile time.
Turns out that gcc and clang will recognize constant strings and compute
the strlen result already at compile time, so trickery with sizeof only
reduces readability but does not improve the code.

Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>

Index: processor_enable.c
===================================================================
--- processor_enable.c	(Revision 1344)
+++ processor_enable.c	(Arbeitskopie)
@@ -56,13 +56,13 @@ 
 		while (*ptr && isspace((unsigned char)*ptr))
 			ptr++;
 		/* "cpu" part appears only with some Linux versions.  */
-		if (strncmp(ptr, "cpu", sizeof("cpu") - 1) == 0)
-			ptr += sizeof("cpu") - 1;
+		if (strncmp(ptr, "cpu", strlen("cpu")) == 0)
+			ptr += strlen("cpu");
 		while (*ptr && isspace((unsigned char)*ptr))
 			ptr++;
-		if (strncmp(ptr, "model", sizeof("model") - 1) != 0)
+		if (strncmp(ptr, "model", strlen("model")) != 0)
 			continue;
-		ptr += sizeof("model") - 1;
+		ptr += strlen("model");
 		while (*ptr && isspace((unsigned char)*ptr))
 			ptr++;
 		if (*ptr != ':')
@@ -72,9 +72,9 @@ 
 			ptr++;
 		fclose(cpuinfo);
 		return (strncmp(ptr, "ICT Loongson-2 V0.3",
-				sizeof("ICT Loongson-2 V0.3") - 1) == 0)
+				strlen("ICT Loongson-2 V0.3")) == 0)
 		    || (strncmp(ptr, "Godson2 V0.3  FPU V0.1",
-				sizeof("Godson2 V0.3  FPU V0.1") - 1) == 0);
+				strlen("Godson2 V0.3  FPU V0.1")) == 0);
 	}
 	fclose(cpuinfo);
 	return 0;