Skip to content

Commit

Permalink
Fix crash when just -d is given
Browse files Browse the repository at this point in the history
This would result in a pointer dereference of one past the end of argv.
Add some tests to check this and similar conditions.
  • Loading branch information
dfandrich committed Feb 29, 2024
1 parent 4acb1eb commit 9b28cf8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,20 @@ little unknown:
# This can be used to run a program with something like wine or qemu
TESTWRAPPER=
test:
$(TESTWRAPPER) ./fvcbm --
$(TESTWRAPPER) ./fvcbm -d --
$(TESTWRAPPER) ./fvcbm || test "$$?" = 1
$(TESTWRAPPER) ./fvcbm > generate.txt || test "$$?" = 1
$(TESTWRAPPER) ./fvcbm -h > generate.txt || test "$$?" = 1
$(TESTWRAPPER) ./fvcbm -d > generate.txt || test "$$?" = 1
$(TESTWRAPPER) ./fvcbm testdata/* > generate.txt 2>&1
diff expect.txt generate.txt
$(TESTWRAPPER) ./fvcbm -d testdata/* > generate.txt 2>&1
diff expect-d.txt generate.txt
$(TESTWRAPPER) ./fvcbm testdata/test1 > generate.txt 2>&1
diff expect-x.txt generate.txt
$(TESTWRAPPER) ./fvcbm -- testdata/test1 > generate.txt 2>&1
diff expect-x.txt generate.txt

#
# fvcbm targets below this line
Expand Down
27 changes: 14 additions & 13 deletions fvcbm.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,20 @@ int main(int argc, char *argv[])
setvbuf(stdout, NULL, _IOLBF, 82); /* speed up screen output */
#endif

if ((argc < 2) ||
if ((argc > 1) &&
((argv[FirstFileName][0] == '-') || (argv[FirstFileName][0] == '/')) &&
((argv[FirstFileName][1] == 'd')
#ifdef CPM
|| (argv[FirstFileName][1] == 'D')
#endif
) && (argv[FirstFileName][2] == '\x0')) {
WideFormat = 0; /* 1541-style output */
++FirstFileName;
} else {
WideFormat = 1; /* wide FV-style output */
}

if ((argc <= FirstFileName) ||
(((argv[FirstFileName][0] == '-') || (argv[FirstFileName][0] == '/')) &&
((argv[FirstFileName][1] == '?') || (argv[FirstFileName][1] == 'h')
#ifdef CPM
Expand All @@ -246,18 +259,6 @@ int main(int argc, char *argv[])
return 1;
}

if (((argv[FirstFileName][0] == '-') || (argv[FirstFileName][0] == '/')) &&
((argv[FirstFileName][1] == 'd')
#ifdef CPM
|| (argv[FirstFileName][1] == 'D')
#endif
) && (argv[FirstFileName][2] == '\x0')) {
WideFormat = 0; /* 1541-style output */
++FirstFileName;
} else {
WideFormat = 1; /* wide FV-style output */
}

/* -- ends options */
if ((argv[FirstFileName][0] == '-') && (argv[FirstFileName][1] == '-')) {
++FirstFileName;
Expand Down

0 comments on commit 9b28cf8

Please sign in to comment.