bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] truncate: Ignore whitespace in --size parameters


From: Pádraig Brady
Subject: [PATCH] truncate: Ignore whitespace in --size parameters
Date: Sun, 29 Jun 2008 02:09:46 +0100
User-agent: Thunderbird 2.0.0.6 (X11/20071008)

Jim Meyering wrote:
> Pádraig Brady <address@hidden> wrote:
> 
>> Paul Eggert wrote:
>>> This indicates a bug in the underlying code, which GCC was right to
>>> warn us about, and which we should not have worked around by inserting
>>> casts blindly.
>> As I said in the patch the casts were to confirm the intent,
>> rather than just to silence the errors.
> 
> I see the intent, but we can sneak past the check
> that detects this:
> 
>     $ ./truncate -s '<-9' k
>     ./truncate: multiple relative modifiers specified
>     Try `./truncate --help' for more information.
> 
> by adding a space before the "-":
> 
>     $ ./truncate -s '< -9' k && echo whoops
>     whoops

Well found! That space would mean that
`truncate -s '> -1' file` would truncate file to 0!

It also means that the more likely command of
`truncate -s " +1" would also truncate a file to 1,
rather than extending the size of file by 1 byte.

patch attached.

thanks,
Pádraig.
>From fbb9a05e83f4af8b4aa99c82c6fbad0849afd9a0 Mon Sep 17 00:00:00 2001
From: =?utf-8?q?P=C3=A1draig=20Brady?= <address@hidden>
Date: Sun, 29 Jun 2008 01:55:03 +0100
Subject: [PATCH] truncate: Ignore whitespace in --size parameters.
 Without this `truncate -s '> -1' file` would truncate file to 0,
 and `truncate -s " +1" would truncate a file to 1 byte.

src/truncate.c: For --size parameter, skip leading whitespace
and any whitespace after one of the relative modifiers so that
the presence of the +/- modifiers can be checked for reliably.
tests/misc/truncate-parameters: Add tests for spaces in --size parameter
---
 src/truncate.c                 |    6 ++++++
 tests/misc/truncate-parameters |    6 ++++++
 2 files changed, 12 insertions(+), 0 deletions(-)

diff --git a/src/truncate.c b/src/truncate.c
index fd321c6..3bc52ca 100644
--- a/src/truncate.c
+++ b/src/truncate.c
@@ -286,6 +286,9 @@ main (int argc, char **argv)
           break;
 
         case 's':
+          /* skip any whitespace */
+          while (isspace (*optarg))
+            optarg++;
           switch (*optarg)
             {
             case '<':
@@ -305,6 +308,9 @@ main (int argc, char **argv)
               optarg++;
               break;
             }
+          /* skip any whitespace */
+          while (isspace (*optarg))
+            optarg++;
           if (*optarg == '+' || *optarg == '-')
             {
               if (rel_mode)
diff --git a/tests/misc/truncate-parameters b/tests/misc/truncate-parameters
index e416831..d08c9b2 100755
--- a/tests/misc/truncate-parameters
+++ b/tests/misc/truncate-parameters
@@ -40,4 +40,10 @@ truncate --io-blocks --reference=file file && fail=1
 # must specify valid numbers
 truncate --size="invalid" file && fail=1
 
+# spaces not significant around size
+truncate --size="> -1" file && fail=1
+truncate --size=" >1" file || fail=1 #file now 1
+truncate --size=" +1" file || fail=1 #file now 2
+[ $(du -b file | cut -f1) -eq 2 ] || fail=1
+
 (exit $fail); exit $fail
-- 
1.5.3.6


reply via email to

[Prev in Thread] Current Thread [Next in Thread]