commit: 24436e97c9bb9b4bfa65325d8fe20cc3231af3c9
parent 3ebc626245f9c0aa23e7b783d2836cc4110071ca
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 11 Mar 2022 21:05:31 +0100
test-bin/strings: Add checks on -n operands
Diffstat:
2 files changed, 23 insertions(+), 7 deletions(-)
diff --git a/bin/strings.c b/bin/strings.c
@@ -27,6 +27,7 @@ print_string(char *buffer, size_t offset)
}
else
{
+ /* flawfinder: ignore. opt_offset_format isn't user-provided */
ret = printf(opt_offset_format, offset, buffer);
}
@@ -97,7 +98,7 @@ concat(int fd, const char *fdname)
void
usage()
{
- puts("strings: [-a] [-t format] [-n number] [file...]");
+ fprintf(stderr, "strings: [-a] [-t format] [-n number] [file...]\n");
}
int
@@ -106,6 +107,7 @@ main(int argc, char *argv[])
int c;
const char *errstr = NULL;
+ /* flawfinder: ignore. Old implementations of getopt should fix themselves */
while((c = getopt(argc, argv, ":an:t:")) != -1)
{
switch(c)
@@ -117,7 +119,9 @@ main(int argc, char *argv[])
opt_min_strlen = (size_t)strtonum(optarg, 1, 4096, &errstr);
if(errstr)
{
- fprintf(stderr, "Minimal string length is %s: %s", errstr, optarg);
+ fprintf(stderr, "Minimal string length is %s: %s\n", errstr, optarg);
+ usage();
+ return 1;
}
break;
case 't':
@@ -139,6 +143,7 @@ main(int argc, char *argv[])
opt_offset_format = "%zd %s\n";
break;
default:
+ fprintf(stderr, "Unknown format: %s\n", optarg);
usage();
return 1;
}
diff --git a/test-bin/strings b/test-bin/strings
@@ -76,15 +76,25 @@ atf_test_case badformat
badformat_body() {
usage="strings: [-a] [-t format] [-n number] [file...]\n"
- atf_check -s exit:1 -o "inline:${usage}" ../bin/strings -tt inputs/all_bytes
- atf_check -s exit:1 -o "inline:${usage}" ../bin/strings -tt /dev/null
- atf_check -s exit:1 -o "inline:${usage}" ../bin/strings -tt inputs/strings/true
- atf_check -s exit:1 -o "inline:${usage}" ../bin/strings -tt -n 8 inputs/strings/true
+ atf_check -s exit:1 -e "inline:Unknown format: t\n${usage}" ../bin/strings -tt inputs/all_bytes
+ atf_check -s exit:1 -e "inline:Unknown format: t\n${usage}" ../bin/strings -tt /dev/null
+ atf_check -s exit:1 -e "inline:Unknown format: t\n${usage}" ../bin/strings -tt inputs/strings/true
+ atf_check -s exit:1 -e "inline:Unknown format: t\n${usage}" ../bin/strings -tt -n 8 inputs/strings/true
+}
+
+atf_test_case erange_n
+erange_n_body() {
+ usage="strings: [-a] [-t format] [-n number] [file...]\n"
+
+ atf_check -s exit:1 -e "inline:Minimal string length is too small: 0\n${usage}" -- ../bin/strings -n 0 inputs/all_bytes
+ atf_check -s exit:1 -e "inline:Minimal string length is too large: 4097\n${usage}" -- ../bin/strings -n 4097 inputs/all_bytes
+ atf_check -s exit:1 -e "inline:Minimal string length is invalid: f\n${usage}" -- ../bin/strings -n f inputs/all_bytes
+ atf_check -s exit:1 -e "inline:Minimal string length is invalid: 42f\n${usage}" -- ../bin/strings -n 42f inputs/all_bytes
}
atf_test_case usage
usage_body() {
- atf_check -s exit:1 -o 'inline:strings: [-a] [-t format] [-n number] [file...]\n' ../bin/strings -t aa inputs/all_bytes
+ atf_check -s exit:1 -e 'inline:strings: [-a] [-t format] [-n number] [file...]\n' ../bin/strings -t aa inputs/all_bytes
}
atf_init_test_cases() {
@@ -99,6 +109,7 @@ atf_init_test_cases() {
atf_add_test_case devfull
atf_add_test_case noperm
+ atf_add_test_case erange_n
atf_add_test_case octalformat
atf_add_test_case hexformat