commit: 3b8a24e0fddbe3cc11e55917d2659542c2c0409e
parent 437351928c485ffd3485fc1958bf9a7067150bc3
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Mon, 23 Sep 2024 05:34:44 +0200
cmd/cut: make sure reallocarray isn't passed zero
Diffstat:
1 file changed, 11 insertions(+), 11 deletions(-)
diff --git a/cmd/cut.c b/cmd/cut.c
@@ -36,7 +36,7 @@ size_t list_len = 0;
bool nostop = false;
const char *argv0 = "cut";
-static ssize_t
+static size_t
parse_list_num(char **s)
{
char *endptr = NULL;
@@ -45,19 +45,19 @@ parse_list_num(char **s)
if(errno != 0)
{
fprintf(stderr, "%s: error: Failed parsing '%s' as a number: %s\n", argv0, *s, strerror(errno));
- return -1;
+ return 0;
}
if(n < 1)
{
fprintf(stderr, "%s: error: Invalid number in list: %zu\n", argv0, n);
- return -1;
+ return 0;
}
if(endptr != NULL && strchr(",-", *endptr) == NULL)
{
fprintf(stderr, "%s: error: Invalid character in list: %c\n", argv0, *endptr);
- return -1;
+ return 0;
}
*s = endptr;
@@ -78,15 +78,15 @@ parse_list(char *s)
return -1;
}
- ssize_t min = 1;
+ size_t min = 1;
if(*s != '-')
{
min = parse_list_num(&s);
- if(min < 0) return -1;
+ if(min == 0) return -1;
}
// min-- as cut(1) is 1-indexed and max needs to be at least min+1
- ssize_t max = min--;
+ size_t max = min--;
if(s && *s == '-')
{
@@ -99,7 +99,7 @@ parse_list(char *s)
else
{
max = parse_list_num(&s);
- if(max < 0) return -1;
+ if(max == 0) return -1;
if(max < min)
{
@@ -112,16 +112,16 @@ parse_list(char *s)
// Needs to be after *s == '-'
if(s && *s == ',') s++;
- if((size_t)max > list_len)
+ if(max > list_len)
{
- list = reallocarray(list, (size_t)max, sizeof(*list));
+ list = reallocarray(list, max, sizeof(*list));
if(list == NULL)
{
fprintf(stderr, "%s: error: Failed memory allocation: %s\n", argv0, strerror(errno));
return -1;
}
- if((size_t)min > list_len)
+ if(min > list_len)
{
memset(list + list_len, 0, min - list_len);
}