commit: 0b0b9666b46a98d9ab5e6d06eeaa64a696292a3e
parent 5893b843e937742761c928f8f208eec3c167afb1
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 23 Aug 2024 05:06:53 +0200
lib/truncation: switch apply_size_suffix to unsigned to avoid UB from overflows
Diffstat:
4 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/cmd/head.c b/cmd/head.c
@@ -198,7 +198,7 @@ main(int argc, char *argv[])
{
assert(errno == 0);
char *endptr = NULL;
- long size = strtol(optarg, &endptr, 0);
+ unsigned long size = strtoul(optarg, &endptr, 0);
if(errno != 0)
{
fprintf(
@@ -209,7 +209,7 @@ main(int argc, char *argv[])
if(endptr != NULL && *endptr != 0)
if(apply_size_suffix(&size, endptr) != 0) return 1;
- bytes = (size_t)size;
+ bytes = size;
copy_action = ©_bytes;
break;
}
diff --git a/cmd/split.c b/cmd/split.c
@@ -234,7 +234,7 @@ main(int argc, char *argv[])
return 1;
}
- long opt_b = strtoul(optarg, &endptr, 0);
+ unsigned long opt_b = strtoul(optarg, &endptr, 0);
if(opt_b == 0)
{
fprintf(stderr, "split: Error while parsing '-b %s': %s\n", optarg, strerror(errno));
@@ -244,7 +244,7 @@ main(int argc, char *argv[])
if(endptr != NULL && *endptr != 0)
if(apply_size_suffix(&opt_b, endptr) != 0) return 1;
- bytes = (size_t)opt_b;
+ bytes = opt_b;
lines = 0;
break;
}
diff --git a/lib/truncation.c b/lib/truncation.c
@@ -69,14 +69,14 @@ apply_truncation(int fd, struct truncation tr, char *arg)
}
int
-apply_size_suffix(long *size, char *endptr)
+apply_size_suffix(unsigned long *size, char *endptr)
{
char units[] = "KMGTPEZ";
- if(endptr[0] == 0) return 0;
+ if(endptr[0] == '\0') return 0;
size_t i = 0;
- long si = 1, iec = 1;
+ unsigned long si = 1, iec = 1;
char pfx = toupper(endptr[0]);
for(; i < sizeof(units); i++)
{
@@ -143,7 +143,7 @@ parse_size(char *arg, struct truncation *buf)
assert(errno == 0);
char *endptr = NULL;
- long size = strtol(arg, &endptr, 10);
+ unsigned long size = strtoul(arg, &endptr, 10);
if(errno != 0)
{
fprintf(stderr, "%s: Failed parsing '%s' as a number: %s\n", argv0, arg, strerror(errno));
diff --git a/lib/truncation.h b/lib/truncation.h
@@ -22,5 +22,5 @@ struct truncation
};
int parse_size(char *arg, struct truncation *buf);
-int apply_size_suffix(long *size, char *endptr);
+int apply_size_suffix(unsigned long *size, char *endptr);
int apply_truncation(int fd, struct truncation tr, char *arg);