logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: 6db49bc56848ccf9d23ff5940b87084545160267
parent 508f78b935d0f2ebd66bfeb64624f03e1e1b31a2
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat,  3 Jun 2023 13:24:16 +0200

lib/iso_parse: Use strtol for @ to detect overflows

Diffstat:

Mlib/iso_parse.c10+++++++++-
Mtest-cmd/date2+-
2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/lib/iso_parse.c b/lib/iso_parse.c @@ -6,6 +6,7 @@ #define _XOPEN_SOURCE 700 // strptime (NetBSD) #define _POSIX_C_SOURCE 200809L // st_atim/st_mtim +#include <errno.h> /* errno */ #include <stdio.h> /* perror, sscanf */ #include <stdlib.h> /* exit */ #include <string.h> /* memset */ @@ -23,7 +24,14 @@ iso_parse(char *arg) if(arg[0] == '@') { arg++; - if(sscanf(arg, "%ld", &time.tv_sec) < 1) exit(EXIT_FAILURE); + errno = 0; + + time.tv_sec = strtol(arg, NULL, 10); + if(errno != 0) + { + perror("strtol"); + exit(EXIT_FAILURE); + } return time; } diff --git a/test-cmd/date b/test-cmd/date @@ -62,7 +62,7 @@ timestamp_body() { atf_check -s 'exit:1' -e "inline:date: Error: Missing operand for option: '-d'\ndate [-uR][-d datetime] [+format]\n" ../cmd/date -u -d # 36893488147419103232 = 2^65 - #atf_check -s 'exit:1' -e not-empty ../cmd/date -u -d @36893488147419103232 + atf_check -s 'exit:1' -e not-empty ../cmd/date -u -d @36893488147419103232 } atf_test_case isodate