From: Richard Kettlewell Date: Sat, 24 May 2014 14:24:22 +0000 (+0100) Subject: Stricter argument checking. X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=394ceb18353471a9c8c2968fad3b3e67d1f145fd;p=vbig.git Stricter argument checking. --- diff --git a/Makefile.am b/Makefile.am index 9ba379f..c498624 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,5 +1,5 @@ bin_PROGRAMS=vbig vbig_SOURCES=vbig.cc Arcfour.h Arcfour.cc man_MANS=vbig.1 -TESTS=t-both t-seeded t-separate t-corrupt t-truncated t-extended +TESTS=t-both t-seeded t-separate t-corrupt t-truncated t-extended t-args EXTRA_DIST=${man_MANS} README.md ${TESTS} diff --git a/t-args b/t-args new file mode 100755 index 0000000..c10744e --- /dev/null +++ b/t-args @@ -0,0 +1,39 @@ +#! /bin/sh +set -e +rm -f testoutput testexpect + +echo 'ERROR: invalid size: Numerical result out of range' > testexpect +if ./vbig testfile 9223372036854775808 2>testoutput; then + echo >&2 ERROR: unexpectedly succeeded + exit 1 +fi +diff -u testexpect testoutput + +echo 'ERROR: invalid size' > testexpect +if ./vbig testfile 9007199254740992K 2>testoutput; then + echo >&2 ERROR: unexpectedly succeeded + exit 1 +fi +diff -u testexpect testoutput + +if ./vbig testfile 8796093022208G 2>testoutput; then + echo >&2 ERROR: unexpectedly succeeded + exit 1 +fi +diff -u testexpect testoutput + +echo 'ERROR: invalid scale' > testexpect +if ./vbig testfile 1T 2>testoutput; then + echo >&2 ERROR: unexpectedly succeeded + exit 1 +fi +diff -u testexpect testoutput + +if ./vbig testfile 1KK 2>testoutput; then + echo >&2 ERROR: unexpectedly succeeded + exit 1 +fi +diff -u testexpect testoutput + + +rm -f testoutput testexpect diff --git a/vbig.cc b/vbig.cc index 86e24bf..6717f2d 100644 --- a/vbig.cc +++ b/vbig.cc @@ -121,6 +121,18 @@ static void flushCache(FILE *fp) { #endif } +static void scale(int shift, long long &value) { + switch(shift) { + case 'K': shift = 10; break; + case 'M': shift = 20; break; + case 'G': shift = 30; break; + default: fatal(0, "invalid scale"); + } + if(value > (LLONG_MAX >> shift)) + fatal(0, "invalid size"); + value <<= shift; +} + static long long execute(mode_type mode, bool entire, const char *show); static const char default_seed[] = "hexapodia as the key insight"; @@ -214,14 +226,11 @@ int main(int argc, char **argv) { fatal(errno, "invalid size"); if(end == argv[1]) fatal(0, "invalid size"); - if(!strcmp(end, "K")) - size <<= 10; - else if(!strcmp(end, "M")) - size <<= 20; - else if(!strcmp(end, "G")) - size <<= 30; - else if(*end) - fatal(0, "invalid size"); + if(*end) { + if(end[1]) + fatal(0, "invalid scale"); + scale(*end, size); + } } else if(entireopt) { /* Use stupidly large size as a proxy for 'infinite' */ size = LLONG_MAX;