chiark / gitweb /
Stricter argument checking.
authorRichard Kettlewell <rjk@greenend.org.uk>
Sat, 24 May 2014 14:24:22 +0000 (15:24 +0100)
committerRichard Kettlewell <rjk@greenend.org.uk>
Sat, 24 May 2014 14:24:22 +0000 (15:24 +0100)
Makefile.am
t-args [new file with mode: 0755]
vbig.cc

index 9ba379f4d95689025641275df4d3cde6f6cc8ed1..c4986246e7884c70734038f1ed1dd60c7abb0084 100644 (file)
@@ -1,5 +1,5 @@
 bin_PROGRAMS=vbig
 vbig_SOURCES=vbig.cc Arcfour.h Arcfour.cc
 man_MANS=vbig.1
 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}
 EXTRA_DIST=${man_MANS} README.md ${TESTS}
diff --git a/t-args b/t-args
new file mode 100755 (executable)
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 86e24bf45d26d2f35689733ffdf3d9095fcd7e14..6717f2d383b4da9137842dd6db1d1d526759f198 100644 (file)
--- a/vbig.cc
+++ b/vbig.cc
@@ -121,6 +121,18 @@ static void flushCache(FILE *fp) {
 #endif
 }
 
 #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";
 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");
       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;
   } else if(entireopt) {
     /* Use stupidly large size as a proxy for 'infinite' */
     size = LLONG_MAX;