]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
tests: factor,numfmt: verify embedded NUL handling
authorPádraig Brady <P@draigBrady.com>
Wed, 25 Feb 2026 14:49:12 +0000 (14:49 +0000)
committerPádraig Brady <P@draigBrady.com>
Wed, 25 Feb 2026 14:52:57 +0000 (14:52 +0000)
* tests/factor/factor.pl: Verify that embedded NULs
on stdin terminate the _number_.
* tests/numfmt/numfmt.p: Verify that embedded NULs
on stdin terminate the _line_.
https://github.com/coreutils/coreutils/pull/196

tests/factor/factor.pl
tests/numfmt/numfmt.pl

index fa38202427b45870a19beab27eb69d8139b1ce0b..2b8e540869603011f8468ae03ee54e8cb7d4a5f1 100755 (executable)
@@ -97,6 +97,13 @@ my @Tests =
       {OUT => '4999 340282366920938463463374607431768211297'}],
      ['h-1', '-h 3000', {OUT => '2^3 3 5^3'}],
      ['h-2', '3000 --exponents', {OUT => '2^3 3 5^3'}],
+     ['nul1', {IN_PIPE => '3'},      {OUT => '3'}],
+     ['nul2', {IN_PIPE => "3\000"},  {OUT => '3'}],
+     ['nul3', {IN_PIPE => "3\000foo"},  {OUT => '3'}],
+     ['nul4', {IN_PIPE => "3\000foo bar"},  {OUT => "3: 3\n"},
+      {EXIT => 1},
+      {ERR => "$prog: 'bar' is not a valid positive integer\n"}],
+     # ['nul5', "3\000"},  {OUT => '3'}],  # Not supported by perl framework
     );
 
 
@@ -107,8 +114,22 @@ my $t;
 Test:
 foreach $t (@Tests)
   {
-    (my $arg1 = $t->[1]) =~ s| *\+?||;     # strip '+'
-    ($arg1 = $arg1) =~ s| *-[^ ]+ *||;     # strip option
+    my $arg1;
+
+    # For IN_PIPE tests, the input number comes from the pipe, not $t->[1].
+    foreach my $e (@$t)
+      {
+        ref $e eq 'HASH' && exists $e->{IN_PIPE}
+          and $arg1 = $e->{IN_PIPE};
+      }
+    if (!defined $arg1)
+      {
+        $arg1 = $t->[1];
+      }
+
+    $arg1 =~ s| *\+?||;        # strip '+'
+    $arg1 =~ s| *-[^ ]+ *||;   # strip option
+    $arg1 =~ s|\0[^ \t\n]*||g; # strip NUL to whitespace
 
     # Don't fiddle with expected OUT string if there's a nonzero exit status.
     foreach my $e (@$t)
index c999923caebd9ed67566de3d5736cf492bdcffb5..b899a46f540517fb56e70ad67ed0591312cba1e3 100755 (executable)
@@ -43,7 +43,8 @@ my @Tests =
      ['5.1', '--from=iec-i 1Ki',  {OUT => "1024"}],
      ['5.2', '--from=iec-i 1',  {OUT => "1"}],
 
-     ['6', {IN_PIPE => "1234\n"},            {OUT => "1234"}],
+     ['6', {IN_PIPE => "1234\n"},              {OUT => "1234"}],
+     ['6nul', {IN_PIPE => "1234\000foo bar\n"},{OUT => "1234"}],
      ['7', '--from=si', {IN_PIPE => "2K\n"}, {OUT => "2000"}],
      ['7a', '--invalid=fail', {IN_PIPE => "no_NL"}, {OUT => "no_NL"},
               {ERR => "$prog: invalid number: 'no_NL'\n"},