All of lore.kernel.org
 help / color / mirror / Atom feed
From: "André Goddard Rosa" <andre.goddard@gmail.com>
To: jengelh@medozas.de, linux-kernel@vger.kernel.org
Cc: "André Goddard Rosa" <andre.goddard@gmail.com>
Subject: [PATCH v4 08/12] vsprintf: reuse almost identical simple_strtoulX() functions
Date: Sat,  7 Nov 2009 14:33:13 -0200	[thread overview]
Message-ID: <57d62b178a5bbd0393e83a2293ba933e60ddc658.1257602781.git.andre.goddard@gmail.com> (raw)
In-Reply-To: <cover.1257602781.git.andre.goddard@gmail.com>

The difference between simple_strtoul() and simple_strtoull() is just
the size of the variable used to keep track of the sum of characters
converted to numbers:

unsigned long simple_strtoul() {...}
unsigned long long simple_strtoull(){...}

Both are same size (8 bytes) on my Core 2/gcc 4.4.1.
Overflow condition is not checked on both functions, so an extremely large
string can break these functions so that they don't even notice it.

As we do not care for overflowing on these functions, always keep the sum
using the larger variable around (unsigned long long) on simple_strtoull()
and cast it to (unsigned long) on simple_strtoul(), which then becomes
just a wrapper around simple_strtoull().

Code size decreases by 304 bytes:
   text    data     bss     dec     hex filename
  15543       0       8   15551    3cbf lib/vsprintf.o-before
  15239       0       8   15247    3b8f lib/vsprintf.o-after

Removed unnecessary cast as noticed by Jan Engelhardt.

Signed-off-by: André Goddard Rosa <andre.goddard@gmail.com>
Acked-by: Ingo Molnar <mingo@elte.hu>
---
 lib/vsprintf.c |   48 ++++++++++++++----------------------------------
 1 files changed, 14 insertions(+), 34 deletions(-)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 566c947..7ec96a3 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -49,14 +49,14 @@ static unsigned int simple_guess_base(const char *cp)
 }
 
 /**
- * simple_strtoul - convert a string to an unsigned long
+ * simple_strtoull - convert a string to an unsigned long long
  * @cp: The start of the string
  * @endp: A pointer to the end of the parsed string will be placed here
  * @base: The number base to use
  */
-unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
+unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
 {
-	unsigned long result = 0;
+	unsigned long long result = 0;
 
 	if (!base)
 		base = simple_guess_base(cp);
@@ -78,54 +78,34 @@ unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
 
 	return result;
 }
-EXPORT_SYMBOL(simple_strtoul);
+EXPORT_SYMBOL(simple_strtoull);
 
 /**
- * simple_strtol - convert a string to a signed long
+ * simple_strtoul - convert a string to an unsigned long
  * @cp: The start of the string
  * @endp: A pointer to the end of the parsed string will be placed here
  * @base: The number base to use
  */
-long simple_strtol(const char *cp, char **endp, unsigned int base)
+unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
 {
-	if (*cp == '-')
-		return -simple_strtoul(cp + 1, endp, base);
-
-	return simple_strtoul(cp, endp, base);
+	return simple_strtoull(cp, endp, base);
 }
-EXPORT_SYMBOL(simple_strtol);
+EXPORT_SYMBOL(simple_strtoul);
 
 /**
- * simple_strtoull - convert a string to an unsigned long long
+ * simple_strtol - convert a string to a signed long
  * @cp: The start of the string
  * @endp: A pointer to the end of the parsed string will be placed here
  * @base: The number base to use
  */
-unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
+long simple_strtol(const char *cp, char **endp, unsigned int base)
 {
-	unsigned long long result = 0;
-
-	if (!base)
-		base = simple_guess_base(cp);
-
-	if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
-		cp += 2;
-
-	while (isxdigit(*cp)) {
-		unsigned int value;
-
-		value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
-		if (value >= base)
-			break;
-		result = result * base + value;
-		cp++;
-	}
-	if (endp)
-		*endp = (char *)cp;
+	if (*cp == '-')
+		return -simple_strtoul(cp + 1, endp, base);
 
-	return result;
+	return simple_strtoul(cp, endp, base);
 }
-EXPORT_SYMBOL(simple_strtoull);
+EXPORT_SYMBOL(simple_strtol);
 
 /**
  * simple_strtoll - convert a string to a signed long long
-- 
1.6.5.2.153.g6e31f.dirty


WARNING: multiple messages have this Message-ID (diff)
From: "André Goddard Rosa" <andre.goddard@gmail.com>
To: Martin Schwidefsky <schwidefsky@de.ibm.com>,
	Heiko Carstens <heiko.carstens@de.ibm.com>,
	linux390@de.ibm.com, Michael Holzheu <holzheu@linux.vnet.ibm.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Stoyan Gaydarov <stoyboyker@gmail.com>,
	Julia Lawall <julia@diku.dk>, Jeff Dike <jdike@addtoit.com>,
	James Morris <jmorris@namei.org>, WANG Cong <wangcong@zeuux.org>,
	Pekka Enberg <penberg@cs.helsinki.fi>,
	David Howells <dhowells@redhat.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, "H . Peter Anvin" <hpa@zytor.com>,
	x86@kernel.org, Alexey Dobriyan <adobriyan@gmail.com>,
	Joe Perches <joe@perches.com>,
	Alan Cox <alan@lxorguk.ukuu.org.uk>,
	Arjan van de Ven <arjan@linux.intel.com>,
	Neil Brown <neilb@suse.de>, Alasdair G Kergon <agk@redhat.com>,
	Mike Snitzer <snitzer@redhat.com>,
	Mikulas Patocka <mpatocka@redhat.com>,
	Jens Axboe <jens.axboe@oracle.com>,
	"Martin K . Petersen" <martin.petersen@oracle.com>,
	Andre Noll <maan@systemlinux.org>,
	Kyle McMartin <kyle@mcmartin.ca>, Helge Deller <deller@gmx.de>,
	"James E . J . Bottomley" <jejb@parisc-linux.org>,
	Roel Kluin <roel.kluin@gmail.com>,
	Henrique de Moraes Holschuh <ibm-acpi@hmh.eng.br>,
	Len Brown <len.brown@intel.com>, Adam Belay <abelay@mit.edu>,
	Bjorn Helgaas <bjorn.helgaas@hp.com>,
	Stefan Haberland <stefan.haberland@de.ibm.com>,
	Stefan Weinhuber <wein@de.ibm.com>,
	Richard Purdie <rpurdie@rpsys.net>,
	Andrea Righi <righi.andrea@gmail.com>,
	Greg Kroah-Hartman <gregkh@suse.de>,
	Pavel Roskin <proski@gnu.org>,
	Andrey Borzenkov <arvidjaar@mail.ru>,
	Steve Dickson <steved@redhat.com>,
	Trond Myklebust <Trond.Myklebust@netapp.com>,
	Daire Byrne <Daire.Byrne@framestore.com>,
	Al Viro <viro@zeniv.linux.org.uk>, Theodore Ts'o <tytso@mit.edu>,
	Andreas Dilger <adilger@sun.com>,
	Eric Sandeen <sandeen@redhat.com>, Jan Kara <jack@suse.cz>,
	Rusty Russell <rusty@rustcorp.com.au>,
	Takashi Iwai <tiwai@suse.de>,
	Frederic Weisbecker <fweisbec@gmail.com>,
	Sitsofe Wheeler <sitsofe@yahoo.com>,
	Christof Schmitt <christof.schmitt@de.ibm.com>,
	Greg Banks <gnb@melbourne.sgi.com>,
	Jason Baron <jbaron@redhat.com>,
	"David S . Miller" <davem@davemloft.net>,
	Steven Rostedt <rostedt@goodmis.org>,
	Samuel Ortiz <samuel@sortiz.org>,
	Patrick McHardy <kaber@trash.net>,
	Jan Engelhardt <jengelh@medozas.de>,
	Roman Hoog Antink <rha@open.ch>, Jaroslav Kysela <perex@perex.cz>,
	linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org,
	user-mode-linux-devel@lists.sourceforge.net,
	user-mode-linux-user@lists.sourceforge.net, dm-devel@redhat.com,
	linux-raid@vger.kernel.org, linux-parisc@vger.kernel.org,
	ibm-acpi-devel@lists.sourceforge.net, linux-cachefs@redhat.com,
	linux-ext4@vger.kernel.org, netdev@vger.kernel.org,
	netfilter-devel@vger.kernel.org, netfilter@vger.kernel.org,
	coreteam@netfilter.org, alsa-devel@alsa-project.org
Cc: "André Goddard Rosa" <andre.goddard@gmail.com>
Subject: [uml-devel] [PATCH v4 08/12] vsprintf: reuse almost identical simple_strtoulX() functions
Date: Sat,  7 Nov 2009 13:16:16 -0200	[thread overview]
Message-ID: <57d62b178a5bbd0393e83a2293ba933e60ddc658.1257602781.git.andre.goddard@gmail.com> (raw)
In-Reply-To: <cover.1257602781.git.andre.goddard@gmail.com>
In-Reply-To: <cover.1257602781.git.andre.goddard@gmail.com>

The difference between simple_strtoul() and simple_strtoull() is just
the size of the variable used to keep track of the sum of characters
converted to numbers:

unsigned long simple_strtoul() {...}
unsigned long long simple_strtoull(){...}

Both are same size (8 bytes) on my Core 2/gcc 4.4.1.
Overflow condition is not checked on both functions, so an extremely large
string can break these functions so that they don't even notice it.

As we do not care for overflowing on these functions, always keep the sum
using the larger variable around (unsigned long long) on simple_strtoull()
and cast it to (unsigned long) on simple_strtoul(), which then becomes
just a wrapper around simple_strtoull().

Code size decreases by 304 bytes:
   text    data     bss     dec     hex filename
  15543       0       8   15551    3cbf lib/vsprintf.o-before
  15239       0       8   15247    3b8f lib/vsprintf.o-after

Signed-off-by: André Goddard Rosa <andre.goddard@gmail.com>
Acked-by: Ingo Molnar <mingo@elte.hu>
---
 lib/vsprintf.c |   48 ++++++++++++++----------------------------------
 1 files changed, 14 insertions(+), 34 deletions(-)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 566c947..7ec96a3 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -49,14 +49,14 @@ static unsigned int simple_guess_base(const char *cp)
 }
 
 /**
- * simple_strtoul - convert a string to an unsigned long
+ * simple_strtoull - convert a string to an unsigned long long
  * @cp: The start of the string
  * @endp: A pointer to the end of the parsed string will be placed here
  * @base: The number base to use
  */
-unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
+unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
 {
-	unsigned long result = 0;
+	unsigned long long result = 0;
 
 	if (!base)
 		base = simple_guess_base(cp);
@@ -78,54 +78,34 @@ unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
 
 	return result;
 }
-EXPORT_SYMBOL(simple_strtoul);
+EXPORT_SYMBOL(simple_strtoull);
 
 /**
- * simple_strtol - convert a string to a signed long
+ * simple_strtoul - convert a string to an unsigned long
  * @cp: The start of the string
  * @endp: A pointer to the end of the parsed string will be placed here
  * @base: The number base to use
  */
-long simple_strtol(const char *cp, char **endp, unsigned int base)
+unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
 {
-	if (*cp == '-')
-		return -simple_strtoul(cp + 1, endp, base);
-
-	return simple_strtoul(cp, endp, base);
+	return (unsigned long)simple_strtoull(cp, endp, base);
 }
-EXPORT_SYMBOL(simple_strtol);
+EXPORT_SYMBOL(simple_strtoul);
 
 /**
- * simple_strtoull - convert a string to an unsigned long long
+ * simple_strtol - convert a string to a signed long
  * @cp: The start of the string
  * @endp: A pointer to the end of the parsed string will be placed here
  * @base: The number base to use
  */
-unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
+long simple_strtol(const char *cp, char **endp, unsigned int base)
 {
-	unsigned long long result = 0;
-
-	if (!base)
-		base = simple_guess_base(cp);
-
-	if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
-		cp += 2;
-
-	while (isxdigit(*cp)) {
-		unsigned int value;
-
-		value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
-		if (value >= base)
-			break;
-		result = result * base + value;
-		cp++;
-	}
-	if (endp)
-		*endp = (char *)cp;
+	if (*cp == '-')
+		return -simple_strtoul(cp + 1, endp, base);
 
-	return result;
+	return simple_strtoul(cp, endp, base);
 }
-EXPORT_SYMBOL(simple_strtoull);
+EXPORT_SYMBOL(simple_strtol);
 
 /**
  * simple_strtoll - convert a string to a signed long long
-- 
1.6.5.2.153.g6e31f.dirty


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
User-mode-linux-devel mailing list
User-mode-linux-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/user-mode-linux-devel

  parent reply	other threads:[~2009-11-08 16:34 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-07 15:16 [PATCH v4 00/12] introduce skip_spaces(), reducing code size plus some clean-ups André Goddard Rosa
2009-11-07 15:16 ` [uml-devel] " André Goddard Rosa
2009-11-07 15:16 ` [PATCH v4 01/12] vsprintf: factorize "(null)" string André Goddard Rosa
2009-11-07 15:16 ` [uml-devel] " André Goddard Rosa
2009-11-08 15:37   ` Jan Engelhardt
2009-11-08 15:49     ` André Goddard Rosa
2009-11-09  3:28   ` Rusty Russell
2009-11-10 14:33     ` André Goddard Rosa
2009-11-07 15:16 ` [uml-devel] [PATCH v4 02/12] vsprintf: pre-calculate final string length for later use André Goddard Rosa
2009-11-07 15:16 ` André Goddard Rosa
2009-11-07 15:16 ` [PATCH v4 03/12] vsprintf: give it some care to please checkpatch.pl André Goddard Rosa
2009-11-07 15:16 ` [uml-devel] " André Goddard Rosa
2009-11-07 15:16 ` [uml-devel] [PATCH v4 04/12] vsprintf: use TOLOWER whenever possible André Goddard Rosa
2009-11-07 15:16 ` André Goddard Rosa
2009-11-07 15:16 ` [uml-devel] [PATCH v4 05/12] vsprintf: reduce code size by avoiding extra check André Goddard Rosa
2009-11-07 15:16 ` André Goddard Rosa
2009-11-07 15:16 ` [uml-devel] [PATCH v4 06/12] vsprintf: move local vars to block local vars and remove unneeded ones André Goddard Rosa
2009-11-07 15:16 ` André Goddard Rosa
2009-11-07 15:16 ` [uml-devel] [PATCH v4 07/12] vsprintf: factor out skip_space code in a separate function André Goddard Rosa
2009-11-08 15:44   ` Jan Engelhardt
2009-11-08 15:52     ` André Goddard Rosa
     [not found]   ` <31525.1257770343@redhat.com>
2009-11-09 15:31     ` André Goddard Rosa
2009-11-07 15:16 ` [PATCH v4 07/12] vsprintf: factor out skip_space code in a separate function André Goddard Rosa
2009-11-07 15:16 ` [PATCH v4 08/12] vsprintf: reuse almost identical simple_strtoulX() functions André Goddard Rosa
2009-11-07 15:16 ` André Goddard Rosa [this message]
2009-11-07 16:33   ` André Goddard Rosa
2009-11-07 15:16 ` [PATCH v4 09/12] ctype: constify read-only _ctype string André Goddard Rosa
2009-11-07 15:16 ` [uml-devel] " André Goddard Rosa
2009-11-07 15:16 ` [PATCH v4 10/12] string: factorize skip_spaces and export it to be generally available André Goddard Rosa
2009-11-07 16:23   ` André Goddard Rosa
2009-11-07 15:16   ` [uml-devel] " André Goddard Rosa
2009-11-08 15:54   ` Jan Engelhardt
2009-11-08 16:38   ` James Bottomley
2009-11-08 16:54     ` Jan Engelhardt
2009-11-08 16:59       ` André Goddard Rosa
2009-11-08 16:50   ` Alan Cox
2009-11-08 16:50     ` [uml-devel] " Alan Cox
2009-11-08 16:50     ` Alan Cox
2009-11-09 14:02     ` André Goddard Rosa
2009-11-08 16:56   ` Jan Engelhardt
2009-11-07 15:16 ` André Goddard Rosa
2009-11-07 15:16 ` [uml-devel] [PATCH v4 11/12] string: on strstrip(), first remove leading spaces before running over str André Goddard Rosa
2009-11-07 15:16 ` André Goddard Rosa
2009-11-07 15:16 ` [PATCH v4 12/12] tree-wide: convert open calls to remove spaces to skip_spaces() lib function André Goddard Rosa
2009-11-07 15:16 ` André Goddard Rosa
2009-11-07 15:16   ` [uml-devel] " André Goddard Rosa
2009-11-08 18:47   ` Theodore Tso
2009-11-08 18:47     ` [uml-devel] " Theodore Tso
2009-11-08 18:47     ` Theodore Tso
2009-11-08 18:47     ` Theodore Tso
2009-11-08 20:23     ` Julia Lawall
2009-11-08 20:23       ` [uml-devel] " Julia Lawall
2009-11-15  6:19       ` André Goddard Rosa
2009-11-15  6:37     ` André Goddard Rosa
2009-11-14  4:20   ` [ibm-acpi-devel] " Henrique de Moraes Holschuh
2009-11-14  7:44     ` André Goddard Rosa
2009-11-08 16:05 ` [dm-devel] [PATCH v4 00/12] introduce skip_spaces(), reducing code size plus some clean-ups James Bottomley
2009-11-08 16:05   ` James Bottomley
2009-11-08 16:05   ` [uml-devel] " James Bottomley
2009-11-08 16:05   ` James Bottomley
2009-11-08 16:05   ` James Bottomley
2009-11-08 16:52   ` André Goddard Rosa
2009-11-08 16:52     ` [uml-devel] " André Goddard Rosa
2009-11-08 16:52     ` André Goddard Rosa
2009-11-08 16:55     ` Jan Engelhardt
2009-11-08 17:02 ` Alexey Dobriyan

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=57d62b178a5bbd0393e83a2293ba933e60ddc658.1257602781.git.andre.goddard@gmail.com \
    --to=andre.goddard@gmail.com \
    --cc=jengelh@medozas.de \
    --cc=linux-kernel@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.