commit: 8db047d4599783d367b202e9b7799e0ce1bcaa06
parent 225884f534c5eec6b898316c7d4559b9747431e1
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 11 Mar 2022 19:50:30 +0100
test-bin/date: strtol overflow, badarg
Diffstat:
2 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/bin/date.c b/bin/date.c
@@ -9,16 +9,17 @@
#include <stdlib.h> /* exit(), strtol() */
#include <time.h> /* time, localtime, tm, strftime */
#include <unistd.h> /* getopt(), optarg, optind */
-#include <errno.h> /* errno */
int
custom_datetime(time_t *now, char *optarg)
{
- if(optarg[0] == '@') {
+ if(optarg[0] == '@')
+ {
optarg++;
errno = 0;
- *now = (time_t)strtol(optarg, NULL, 10);
- if(errno != 0) {
+ *now = (time_t)strtol(optarg, NULL, 10);
+ if(errno != 0)
+ {
perror("strtol");
return 0;
}
diff --git a/test-bin/date b/test-bin/date
@@ -4,6 +4,11 @@ noargs_body() {
atf_check -o not-empty ../bin/date
}
+atf_test_case badarg
+badarg_body() {
+ atf_check -s 'exit:1' -e "inline:Error: Unrecognised option: '-x'\ndate [-u][-d datetime] [+format]\n" ../bin/date -x
+}
+
atf_test_case epoch
epoch_body() {
atf_check -o "match:^[0-9]+$" ../bin/date '+%s'
@@ -46,6 +51,9 @@ timestamp_body() {
atf_check -s 'exit:1' -e "inline:Error: Missing operand for option: '-d'\ndate [-u][-d datetime] [+format]\n" ../bin/date -u -d
atf_check -s 'exit:1' ../bin/date -u -d 69
+
+ # 36893488147419103232 = 2^65
+ atf_check -s 'exit:1' -e 'inline:strtol: Result not representable\n' ../bin/date -u -d @36893488147419103232
}
atf_init_test_cases() {
@@ -54,6 +62,7 @@ atf_init_test_cases() {
. ../test_functions.sh
atf_add_test_case noargs
+ atf_add_test_case badarg
atf_add_test_case empty
atf_add_test_case echolike
atf_add_test_case devfull