GNU bug report logs - #60989
[PATCH] rm: fail on duplicate input if force not enabled

Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.

Package: coreutils; Reported by: Łukasz Sroka <sroka.dev@HIDDEN>; Keywords: patch; dated Sat, 21 Jan 2023 14:29:02 UTC; Maintainer for coreutils is bug-coreutils@HIDDEN.

Message received at 60989 <at> debbugs.gnu.org:


Received: (at 60989) by debbugs.gnu.org; 21 Jan 2023 14:53:24 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Sat Jan 21 09:53:23 2023
Received: from localhost ([127.0.0.1]:48420 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1pJFFD-0000UP-Ia
	for submit <at> debbugs.gnu.org; Sat, 21 Jan 2023 09:53:23 -0500
Received: from mail-wr1-f45.google.com ([209.85.221.45]:44854)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <pixelbeat@HIDDEN>) id 1pJFFB-0000UC-5U
 for 60989 <at> debbugs.gnu.org; Sat, 21 Jan 2023 09:53:21 -0500
Received: by mail-wr1-f45.google.com with SMTP id bk16so7157165wrb.11
 for <60989 <at> debbugs.gnu.org>; Sat, 21 Jan 2023 06:53:21 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
 h=content-transfer-encoding:in-reply-to:from:references:to
 :content-language:subject:user-agent:mime-version:date:message-id
 :sender:from:to:cc:subject:date:message-id:reply-to;
 bh=uMV8nSBhVYM+JyJJ4FDVbY7NULb1N2UYJ6exVSz/K/E=;
 b=FWAVkS5cjLONrbDcBotEOl7JUcQkIiQkge6uIKKJGxTm4lKHlbHLOSMFWFlFs0QQSB
 LUH90xut9NvVryHj0KNMxERGs9U/z2eobiG8+aV9UR81jvNNwt9k+FjEK6J261+d5Zci
 yA/YtpPbpfA8k9CfEC9EwRjBq2ml2ES894dm4abUqsmzaCjkDtGqoggcC/xarsUaT3YI
 IbNiuKlCID0rtMn5qkCUsteN51OAqivGnGkBDZy5vGNULl3oTp8YR35YqdrFEJDCI7NN
 QNBRa3rky74ZEiEeEAoXhsWUApmQZjY8k9oxHCfvwX1JkLT+HgqtID8s1MwVxYLFx0sQ
 BBwA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20210112;
 h=content-transfer-encoding:in-reply-to:from:references:to
 :content-language:subject:user-agent:mime-version:date:message-id
 :sender:x-gm-message-state:from:to:cc:subject:date:message-id
 :reply-to;
 bh=uMV8nSBhVYM+JyJJ4FDVbY7NULb1N2UYJ6exVSz/K/E=;
 b=iAyCCpYCwpNFrrfGtnhCnU8lp4x+M+0+p4L3japZ+oUg+u7ujMHx0vBYcqTVrD3iCR
 hQPOQHmoYOl21ZOTSvhBzHpv8d3sYo/Xu4GxAie5tdljj796c4Fon/y2Fjdzxgws1H3s
 OJYNOXMpeMs6RSzVsKo1UWuZy9QSha/XUlvkOCJxAyrmSTDxZs3ifAmzOybDbL/Ir/f6
 6ss2ahaA7IWeYaC9+16HgCtBOWo/E4IBEVHg+t2WJrAPRlVGNtHu8nJWPmC6fXJdJWGJ
 LYm1UkUY0Ls+dPru3SxX0Gq/1HAqVRtx4JHOim8tuzxmxF4KWJv6u9vDjsVMkeuEQHGv
 u9ew==
X-Gm-Message-State: AFqh2kpqrvIXYzbDvETyhueVpMn0D+3MaHeumZcRpjx8kIrTWYvT4yXv
 v+C8i9Nn+JOp3BHKf9DZso0=
X-Google-Smtp-Source: AMrXdXsoqf6qlSOtKRXwHyG3BqA9OYUWxFAWh7z/xAvqwnTckvEL06uDYvV4CIoFN+8cwlvGhtTbbA==
X-Received: by 2002:adf:f54d:0:b0:273:7d1b:7337 with SMTP id
 j13-20020adff54d000000b002737d1b7337mr25049079wrp.7.1674312795195; 
 Sat, 21 Jan 2023 06:53:15 -0800 (PST)
Received: from [192.168.1.9]
 (95-44-90-175-dynamic.agg2.lod.rsl-rtd.eircom.net. [95.44.90.175])
 by smtp.googlemail.com with ESMTPSA id
 c6-20020adffb06000000b002423620d356sm18196936wrr.35.2023.01.21.06.53.14
 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);
 Sat, 21 Jan 2023 06:53:14 -0800 (PST)
Message-ID: <0461e966-8ae5-4fcb-7f9f-42283f6b30cb@HIDDEN>
Date: Sat, 21 Jan 2023 14:53:13 +0000
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101
 Thunderbird/109.0
Subject: Re: bug#60989: [PATCH] rm: fail on duplicate input if force not
 enabled
Content-Language: en-US
To: =?UTF-8?Q?=C5=81ukasz_Sroka?= <sroka.dev@HIDDEN>, 60989 <at> debbugs.gnu.org
References: <CAGtcvOiSkkFv59ceFApbYVXWQS57jgQEVEf=O=sZDZ1+aXE98A@HIDDEN>
From: =?UTF-8?Q?P=C3=A1draig_Brady?= <P@HIDDEN>
In-Reply-To: <CAGtcvOiSkkFv59ceFApbYVXWQS57jgQEVEf=O=sZDZ1+aXE98A@HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Spam-Score: -0.9 (/)
X-Debbugs-Envelope-To: 60989
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.9 (-)

On 21/01/2023 13:05, Łukasz Sroka wrote:
>      When the input files contain duplicates, then the rm fails. Because
>      duplicates occur most often when the * is used and the shell unwraps it.
>      There is a very common scenario when a user accidentally enters space
>      after a filename, or enters space instead of forward slash.
>      Example:
> 
>        rm prefix_ *
> 
>      The user intended to remove all files with a `prefix_` but removed all
>      of the files in cwd.
>      The program quits immediately when a duplicate is detected, to prevent
>      pressing `y` because user expected a prompt regarding removing multiple
>      files.
>      The force option disables this function to enable scripts to work
>      without modifying them.
> 
> ```
> diff --git a/src/rm.c b/src/rm.c
> index 354e2b0df..e4f9949f0 100644
> --- a/src/rm.c
> +++ b/src/rm.c
> @@ -123,6 +123,16 @@ diagnose_leading_hyphen (int argc, char **argv)
>       }
>   }
> 
> +static bool
> +find_duplicates (int n_files, char **files)
> +{
> +  for (int l = 0; l < n_files-1; l++)
> +    for (int r = l+1; r < n_files; r++)
> +      if (strcmp(files[l], files[r]) == 0)
> +          return true;
> +  return false;
> +}
> +
>   void
>   usage (int status)
>   {
> @@ -211,6 +221,7 @@ main (int argc, char **argv)
>     bool preserve_root = true;
>     struct rm_options x;
>     bool prompt_once = false;
> +  bool force_rm = false;
>     int c;
> 
>     initialize_main (&argc, &argv);
> @@ -238,6 +249,7 @@ main (int argc, char **argv)
>             x.interactive = RMI_NEVER;
>             x.ignore_missing_files = true;
>             prompt_once = false;
> +          force_rm = true;
>             break;
> 
>           case 'i':
> @@ -352,6 +364,17 @@ main (int argc, char **argv)
>     uintmax_t n_files = argc - optind;
>     char **file =  argv + optind;
> 
> +  if (!force_rm && find_duplicates(n_files, file))
> +    {
> +      /* Because usually when the input files are duplicated it means
> that the user
> +         sumbitted both a directory and an * as separate arguments,
> probably by accident */
> +      fprintf (stderr,
> +               "%s: input contains duplicates, most likely you've put "
> +               "both * and a file from the same directory.\n",
> +               program_name);
> +      return EXIT_FAILURE;
> +    }
> +
>     if (prompt_once && (x.recursive || 3 < n_files))
>       {
>         fprintf (stderr,
> ```

An interesting proposal.
The main protection would be for `dir/ *` rather than `file_prefix_ *`.
The former would be unusual for a user to type, while the latter more usual, but wouldn't trigger the protection AFAICS.
This ads O(N^2) on each interaction, so if it was to be included probably only enabled with --interactive.

cheers,
Pádraig




Information forwarded to bug-coreutils@HIDDEN:
bug#60989; Package coreutils. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 21 Jan 2023 14:28:13 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Sat Jan 21 09:28:13 2023
Received: from localhost ([127.0.0.1]:48377 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1pJEqr-0008Ct-3L
	for submit <at> debbugs.gnu.org; Sat, 21 Jan 2023 09:28:13 -0500
Received: from lists.gnu.org ([209.51.188.17]:36020)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <sroka.dev@HIDDEN>) id 1pJDYz-0006Dp-Uk
 for submit <at> debbugs.gnu.org; Sat, 21 Jan 2023 08:05:42 -0500
Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <sroka.dev@HIDDEN>)
 id 1pJDYz-00006v-J4
 for bug-coreutils@HIDDEN; Sat, 21 Jan 2023 08:05:41 -0500
Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.90_1) (envelope-from <sroka.dev@HIDDEN>)
 id 1pJDYx-0004gu-OA
 for bug-coreutils@HIDDEN; Sat, 21 Jan 2023 08:05:41 -0500
Received: by mail-pf1-x42e.google.com with SMTP id c26so5844330pfp.10
 for <bug-coreutils@HIDDEN>; Sat, 21 Jan 2023 05:05:38 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
 h=to:subject:message-id:date:from:mime-version:from:to:cc:subject
 :date:message-id:reply-to;
 bh=NxxCzssBD0cIWcnLLssSDbBmG2H3TOUJCGa0t8Dn0oI=;
 b=TUEwOmhC/Scd4FL0OydFEIGgQ/CfmzHfwvfNGEwXNoHSn5iR4F00glNAJbQz4oqVIY
 LRW/0PmP+ep6iqvxmo/Wk7rDF7x0oYesJDde61axxylyqJxu6QQzGEQhtukZwab3bSX7
 szCyTaEPjgRnft96BwrVzF9qONRlw2u0oLE0JPGZOo4dRd6W23TaC4F95+ADK3KjpS0Y
 JRwwquET9Td+817yli1McNqmxGJ19yNTwyTrZrdW2yOe1QR4niEQuKL+5A1zXE78hKWz
 zZF2/0F+6lP89U9if5xSW0zEVOGRZBRPMLF2ydyjm/0n9lYEROYoijbdNIJzyyzB6m3U
 QY1w==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20210112;
 h=to:subject:message-id:date:from:mime-version:x-gm-message-state
 :from:to:cc:subject:date:message-id:reply-to;
 bh=NxxCzssBD0cIWcnLLssSDbBmG2H3TOUJCGa0t8Dn0oI=;
 b=78uSW2oKUukWJRHmuwouxRPIxcAXn7GWQEuj+EcJFZNWdVRMizzMRLJgYbLT961Fva
 xIjibS9NmU5lAv3HUpnh051B4mw87Nb6Eje4paogGKJEHswWof88P4dSuqPC5hXedWrk
 3fvIlMUpSeeeFueObiRtictjCkaMXiPIQUSnoxh5vm1QymW4zIaOAoNWqYSZIe1JSomN
 Zd21JsBJWpW7RmVpXaYZZOgdtBc0PWXKm35rX4lX12lU/ttV0XVWBYMr2tCPy3at1zhC
 MTHwtJnzG8+P5tcUJizA41n0doR6PclaDXZB8WBXOHYVx3FhshfP6iRMK0rZ55eWtTFt
 O8FQ==
X-Gm-Message-State: AFqh2kpAkPeOwDhtgMSAwqWytk0Cd6O9uMrFv0PVyfx3ehUrCuXXt7Hm
 6U+jmDra6Xoo/UzByEOYCIF+59TofD9/HH2yLJQkufrCX0E=
X-Google-Smtp-Source: AMrXdXsR0CpADgpH8WunKEGtZlyFChIuhHFUl9imzPuxoTx1/t0xwIEv86hCu1hRt6n+jkxsr1QU9TWPjXbsQgm6jLk=
X-Received: by 2002:aa7:820f:0:b0:58d:a713:d1dd with SMTP id
 k15-20020aa7820f000000b0058da713d1ddmr1970474pfi.59.1674306337429; Sat, 21
 Jan 2023 05:05:37 -0800 (PST)
MIME-Version: 1.0
From: =?UTF-8?Q?=C5=81ukasz_Sroka?= <sroka.dev@HIDDEN>
Date: Sat, 21 Jan 2023 14:05:25 +0100
Message-ID: <CAGtcvOiSkkFv59ceFApbYVXWQS57jgQEVEf=O=sZDZ1+aXE98A@HIDDEN>
Subject: [PATCH] rm: fail on duplicate input if force not enabled
To: bug-coreutils@HIDDEN
Content-Type: text/plain; charset="UTF-8"
Received-SPF: pass client-ip=2607:f8b0:4864:20::42e;
 envelope-from=sroka.dev@HIDDEN; helo=mail-pf1-x42e.google.com
X-Spam_score_int: -20
X-Spam_score: -2.1
X-Spam_bar: --
X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,
 DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001,
 RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001,
 SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-Spam-Score: -1.3 (-)
X-Debbugs-Envelope-To: submit
X-Mailman-Approved-At: Sat, 21 Jan 2023 09:28:12 -0500
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -2.3 (--)

    When the input files contain duplicates, then the rm fails. Because
    duplicates occur most often when the * is used and the shell unwraps it.
    There is a very common scenario when a user accidentally enters space
    after a filename, or enters space instead of forward slash.
    Example:

      rm prefix_ *

    The user intended to remove all files with a `prefix_` but removed all
    of the files in cwd.
    The program quits immediately when a duplicate is detected, to prevent
    pressing `y` because user expected a prompt regarding removing multiple
    files.
    The force option disables this function to enable scripts to work
    without modifying them.

```
diff --git a/src/rm.c b/src/rm.c
index 354e2b0df..e4f9949f0 100644
--- a/src/rm.c
+++ b/src/rm.c
@@ -123,6 +123,16 @@ diagnose_leading_hyphen (int argc, char **argv)
     }
 }

+static bool
+find_duplicates (int n_files, char **files)
+{
+  for (int l = 0; l < n_files-1; l++)
+    for (int r = l+1; r < n_files; r++)
+      if (strcmp(files[l], files[r]) == 0)
+          return true;
+  return false;
+}
+
 void
 usage (int status)
 {
@@ -211,6 +221,7 @@ main (int argc, char **argv)
   bool preserve_root = true;
   struct rm_options x;
   bool prompt_once = false;
+  bool force_rm = false;
   int c;

   initialize_main (&argc, &argv);
@@ -238,6 +249,7 @@ main (int argc, char **argv)
           x.interactive = RMI_NEVER;
           x.ignore_missing_files = true;
           prompt_once = false;
+          force_rm = true;
           break;

         case 'i':
@@ -352,6 +364,17 @@ main (int argc, char **argv)
   uintmax_t n_files = argc - optind;
   char **file =  argv + optind;

+  if (!force_rm && find_duplicates(n_files, file))
+    {
+      /* Because usually when the input files are duplicated it means
that the user
+         sumbitted both a directory and an * as separate arguments,
probably by accident */
+      fprintf (stderr,
+               "%s: input contains duplicates, most likely you've put "
+               "both * and a file from the same directory.\n",
+               program_name);
+      return EXIT_FAILURE;
+    }
+
   if (prompt_once && (x.recursive || 3 < n_files))
     {
       fprintf (stderr,
```




Acknowledgement sent to Łukasz Sroka <sroka.dev@HIDDEN>:
New bug report received and forwarded. Copy sent to bug-coreutils@HIDDEN. Full text available.
Report forwarded to bug-coreutils@HIDDEN:
bug#60989; Package coreutils. Full text available.
Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.
Last modified: Sat, 21 Jan 2023 15:00:02 UTC

GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997 nCipher Corporation Ltd, 1994-97 Ian Jackson.