[PATCH] sort: use size_t for pointer offsets

From: Jakob Kramer <jakob.kramer_AT_gmx.de>
Date: Sun, 8 Feb 2015 20:22:05 +0100

The estrtoll function is meant to simplify the parse_keydef
function.
---
 sort.c | 44 +++++++++++++++++++++++++++-----------------
 1 file changed, 27 insertions(+), 17 deletions(-)
diff --git a/sort.c b/sort.c
index 6184b0b..128945f 100644
--- a/sort.c
+++ b/sort.c
_AT_@ -1,6 +1,8 @@
 /* See LICENSE file for copyright and license details. */
 #include <ctype.h>
+#include <errno.h>
 #include <stdio.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
_AT_@ -9,10 +11,10 @@
 #include "util.h"
 
 struct keydef {
-	int start_column;
-	int end_column;
-	int start_char;
-	int end_char;
+	size_t start_column;
+	size_t end_column;
+	size_t start_char;
+	size_t end_char;
 	int flags;
 };
 
_AT_@ -32,6 +34,7 @@ static struct kdlist *head = NULL;
 static struct kdlist *tail = NULL;
 
 static void addkeydef(char *, int);
+static long long estrtoll(char *, char **, long long, long long);
 static void check(FILE *);
 static int linecmp(const char **, const char **);
 static char *skipblank(char *);
_AT_@ -214,6 +217,20 @@ parse_flags(char **s, int *flags, int bflag)
 	return 0;
 }
 
+static long long
+estrtoll(char *nptr, char **endptr, long long minval, long long maxval)
+{
+	long long n;
+
+	n = strtoll(nptr, endptr, 10);
+	if (n < minval || (n == LLONG_MIN && errno == ERANGE))
+		enprintf(2, "strtoll: %lld too small\n", n);
+	if (n > maxval || (n == LLONG_MAX && errno == ERANGE))
+		enprintf(2, "strtoll: %lld too large\n", n);
+
+	return n;
+}
+
 static int
 parse_keydef(struct keydef *kd, char *s, int flags)
 {
_AT_@ -226,24 +243,17 @@ parse_keydef(struct keydef *kd, char *s, int flags)
 	kd->end_char = 0;
 	kd->flags = flags;
 
-	kd->start_column = strtol(rest, &rest, 10);
-	if (kd->start_column < 1)
-		return -1;
+	kd->start_column = estrtoll(rest, &rest, 1, MIN(SIZE_MAX, LLONG_MAX));
 	if (*rest == '.')
-		kd->start_char = strtol(rest+1, &rest, 10);
-	if (kd->start_char < 1)
-		return -1;
+		kd->start_char = estrtoll(rest + 1, &rest, 1, MIN(SIZE_MAX, LLONG_MAX));
 	if (parse_flags(&rest, &kd->flags, MOD_STARTB) < 0)
 		return -1;
 	if (*rest == ',') {
-		kd->end_column = strtol(rest+1, &rest, 10);
+		kd->end_column = estrtoll(rest + 1, &rest, 0, MIN(SIZE_MAX, LLONG_MAX));
 		if (kd->end_column && kd->end_column < kd->start_column)
 			return -1;
-		if (*rest == '.') {
-			kd->end_char = strtol(rest+1, &rest, 10);
-			if (kd->end_char < 1)
-				return -1;
-		}
+		if (*rest == '.')
+			kd->end_char = estrtoll(rest + 1, &rest, 1, MIN(SIZE_MAX, LLONG_MAX));
 		if (parse_flags(&rest, &kd->flags, MOD_ENDB) < 0)
 			return -1;
 	}
_AT_@ -280,7 +290,7 @@ static char *
 columns(char *line, const struct keydef *kd)
 {
 	char *start, *end;
-	int i;
+	size_t i;
 
 	for (i = 1, start = line; i < kd->start_column; i++)
 		start = nextcol(start);
-- 
1.9.1
--------------030800010706080104080704--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Sun Feb 15 2015 - 01:00:04 CET