]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
base64: use stricter validation on wrap column
authorPádraig Brady <P@draigBrady.com>
Sun, 16 Aug 2015 14:31:17 +0000 (15:31 +0100)
committerPádraig Brady <P@draigBrady.com>
Wed, 2 Sep 2015 23:34:12 +0000 (00:34 +0100)
* src/base64.c (main): Use the higher level xnumtoumax()
rather than xstrtoumax(), which is simpler and improves
validation of input.  Also pass the _empty_ rather than NULL
string as the suffixes parameter so that invalid trailing
characters are not allowed.  For example -w08 is now
flagged as an error, rather than being interpreted as 0.
A subsequent commit will further improve verification
of numbers with leading zeros by dropping backwards compatibility
wrt auto parsing oct and hex numbers.
* tests/misc/base64.pl: Add tests for invalid wrap values.

src/base64.c
tests/misc/base64.pl

index 679044e4099c5e97b93ec239e52fcfae314bc774..15888fa83a2e894b4d329536c47152a1df451c96 100644 (file)
@@ -29,7 +29,7 @@
 #include "fadvise.h"
 #include "xstrtol.h"
 #include "quote.h"
-#include "quotearg.h"
+#include "xdectoint.h"
 #include "xfreopen.h"
 
 #define AUTHORS proper_name ("Simon Josefsson")
@@ -289,9 +289,8 @@ main (int argc, char **argv)
         break;
 
       case 'w':
-        if (xstrtoumax (optarg, NULL, 0, &wrap_column, NULL) != LONGINT_OK)
-          error (EXIT_FAILURE, 0, _("invalid wrap size: %s"),
-                 quotearg (optarg));
+        wrap_column = xnumtoumax (optarg, 0, 0, UINTMAX_MAX, "",
+                                  _("invalid wrap size"), 0);
         break;
 
       case 'i':
index 872535afb26aaedd6172cdd90c26894f7762eefe..44c3d21ab88687ec14f91b48ba656e2dfb3d6dc6 100755 (executable)
@@ -71,6 +71,7 @@ sub gen_tests($)
      ['inout4', {IN=>'a'x4}, {OUT=>&$enc(4)."\n"}],
      ['inout5', {IN=>'a'x5}, {OUT=>&$enc(5)."\n"}],
      ['wrap', '--wrap 0', {IN=>'a'}, {OUT=>&$enc(1)}],
+     ['wrap-hex', '--wrap 0x0', {IN=>'a'}, {OUT=>&$enc(1)}],
      ['wrap5-39', '--wrap=5', {IN=>'a' x 39}, {OUT=>wrap &$enc(39),5}],
      ['wrap5-40', '--wrap=5', {IN=>'a' x 40}, {OUT=>wrap &$enc(40),5}],
      ['wrap5-41', '--wrap=5', {IN=>'a' x 41}, {OUT=>wrap &$enc(41),5}],
@@ -80,6 +81,19 @@ sub gen_tests($)
      ['wrap5-45', '--wrap=5', {IN=>'a' x 45}, {OUT=>wrap &$enc(45),5}],
      ['wrap5-46', '--wrap=5', {IN=>'a' x 46}, {OUT=>wrap &$enc(46),5}],
 
+     ['wrap-bad-1', '-w08', {IN=>''}, {OUT=>""},
+      {ERR_SUBST => 's/base..:/base..:/'},
+      {ERR => "base..: invalid wrap size: '08'\n"}, {EXIT => 1}],
+     ['wrap-bad-2', '-w1k', {IN=>''}, {OUT=>""},
+      {ERR_SUBST => 's/base..:/base..:/'},
+      {ERR => "base..: invalid wrap size: '1k'\n"}, {EXIT => 1}],
+     ['wrap-bad-3', '-w-1', {IN=>''}, {OUT=>""},
+      {ERR_SUBST => 's/base..:/base..:/'},
+      {ERR => "base..: invalid wrap size: '-1'\n"}, {EXIT => 1}],
+     ['wrap-bad-4', '-w-0', {IN=>''}, {OUT=>""},
+      {ERR_SUBST => 's/base..:/base..:/'},
+      {ERR => "base..: invalid wrap size: '-0'\n"}, {EXIT => 1}],
+
      ['buf-1',   '--decode', {IN=>&$enc(1)}, {OUT=>'a' x 1}],
      ['buf-2',   '--decode', {IN=>&$enc(2)}, {OUT=>'a' x 2}],
      ['buf-3',   '--decode', {IN=>&$enc(3)}, {OUT=>'a' x 3}],