threads / patch / 16815

patchfast-import.c: stricter strtoul check, silence compiler warning

Subject: [PATCH] fast-import.c: stricter strtoul check, silence compiler warning

## tl;dr

One message between Dec 21, 2008 and Dec 21, 2008. Diffs are folded; open one to read it.

replies: 0people: 1as markdown or json

René Scharfe· Dec 21, 2008, 01:28 UTC · lore

Store the return value of strtoul() in order to avoid compiler warnings on Ubuntu 8.10.

Also check errno after each call, which is the only way to notice an overflow without making ULONG_MAX an illegal date.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
I don't really like the first part, as we're ignoring the return
value anyway, even if we store it in the variable "date", so this
is quite useless.  But better to have a bit more useless code
than to see these equally useless warnings on every build.
Turning them off completely is not a good idea, since some of
them resulted in useful fixes (see 47d32af2, 304dcf26, 7be77de2).
 fast-import.c |   11 +++++++----
 1 files changed, 7 insertions(+), 4 deletions(-)
Show changes to fast-import.c +7 −4
diff --git a/fast-import.c b/fast-import.c
index 171d178..a6bce66 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -1748,18 +1748,21 @@ static int validate_raw_date(const char *src, char *result, int maxlen)
 {
 	const char *orig_src = src;
 	char *endp, sign;
+	unsigned long date;
 
-	strtoul(src, &endp, 10);
-	if (endp == src || *endp != ' ')
+	errno = 0;
+
+	date = strtoul(src, &endp, 10);
+	if (errno || endp == src || *endp != ' ')
 		return -1;
 
 	src = endp + 1;
 	if (*src != '-' && *src != '+')
 		return -1;
 	sign = *src;
 
-	strtoul(src + 1, &endp, 10);
-	if (endp == src || *endp || (endp - orig_src) >= maxlen)
+	date = strtoul(src + 1, &endp, 10);
+	if (errno || endp == src || *endp || (endp - orig_src) >= maxlen)
 		return -1;
 
 	strcpy(result, orig_src);
-- 
1.6.1.rc3.52.g589372

← back to recent threads