]> git.ipfire.org Git - thirdparty/bash.git/commitdiff
fix for completing quoted filenames with show-all-if-ambiguous set; avoid signed...
authorChet Ramey <chet.ramey@case.edu>
Tue, 10 Jan 2023 15:23:14 +0000 (10:23 -0500)
committerChet Ramey <chet.ramey@case.edu>
Tue, 10 Jan 2023 15:23:14 +0000 (10:23 -0500)
CWRU/CWRU.chlog
doc/bash.1
doc/bashref.texi
input.c
input.h
lib/readline/complete.c
lib/sh/random.c
parse.y

index 6a0741943bc55137353fd44cb33e4d2ed140901b..575c9cecffd5a5c94026b4f2a75d1bab6b7c50eb 100644 (file)
@@ -4978,3 +4978,30 @@ lib/readline/{vi_mode.c,histfile.c,funmap.c,complete.c,display.c,bind.c,isearch.
 lib/tilde/tilde.c
        - final code cleanups for ANSI C and lint/ubsan/asan runs
 
+                                   1/7
+                                   ---
+lib/readline/complete.c
+       - rl_complete_internal: when computing nontrivial_lcd and the length
+         of the text from the line buffer, dequote the text from the line
+         buffer before comparing it against the common prefix of the matches
+         (matches[0]), so we don't get spurious mismatches if the quoted text
+         from the line is longer than the unquoted match. Report from
+         Martin Castillo <castilma@uni-bremen.de>
+
+input.[ch]
+       - get_buffered_stream(fd): new function, return the BUFFERED_STREAM *
+         corresponding to file descriptor FD, if it exists
+
+                                   1/8
+                                   ---
+parse.y
+       - shell_getc: if yy_getc returns EOF but the buffered stream input
+         indicates an error state, set shell_input_line_terminator to
+         READERR. Currently treated the same as EOF.
+
+                                  1/10
+                                  ----
+lib/sh/random.c
+       - intrand32: use % operator instead of (mathematically equivalent)
+         subtraction and multiplication, which can cause signed 32-bit
+         overflow. Report from Sam James <sam@gentoo.org>
index f8696707c3892fd99548f17f0747c0edec54d8c2..41ae20840e9a0ca5f8f6b9b6db1c59313d66630d 100644 (file)
@@ -10094,7 +10094,7 @@ arithmetic \fBfor\fP command, display the expanded value of
 .SM
 .BR PS4 ,
 followed by the command and its expanded arguments
-or associated word list.
+or associated word list, to standard error.
 .TP 8
 .B \-B
 The shell performs brace expansion (see
index d419540ee6230937eb13ae8d337e22f33dfe01a8..3eb253e562afbd648992f437419744384a301ef4 100644 (file)
@@ -5455,9 +5455,9 @@ Print shell input lines as they are read.
 @item -x
 Print a trace of simple commands, @code{for} commands, @code{case}
 commands, @code{select} commands, and arithmetic @code{for} commands
-and their arguments or associated word lists after they are
-expanded and before they are executed.  The value of the @env{PS4}
-variable is expanded and the resultant value is printed before
+and their arguments or associated word lists to standard error
+after they are expanded and before they are executed.
+The shell prints the expanded value of the @env{PS4} variable before
 the command and its expanded arguments.
 
 @item -B
diff --git a/input.c b/input.c
index 0e569e7d313f3c84b8609f5efc27753fb19031fd..76496cd594846c363f0b48b5dbc2cd7869043843 100644 (file)
--- a/input.c
+++ b/input.c
@@ -464,6 +464,13 @@ set_buffered_stream (int fd, BUFFERED_STREAM *bp)
   return ret;
 }
 
+/* Return the BUFFERED_STREAM associated with FD, if any. */
+BUFFERED_STREAM *
+get_buffered_stream (int fd)
+{
+  return (buffers && fd < nbuffers) ? buffers[fd] : (BUFFERED_STREAM *)0;
+}
+
 /* Read a buffer full of characters from BP, a buffered stream. */
 static int
 b_fill_buffer (BUFFERED_STREAM *bp)
diff --git a/input.h b/input.h
index 5fcfa7432c046a2fef7518885aea17667ec14832..f4d91b0437de1e07e1111432445bd40e0a75d24d 100644 (file)
--- a/input.h
+++ b/input.h
 
 #include "stdc.h"
 
-/* Function pointers can be declared as (Function *)foo. */
-#if !defined (_FUNCTION_DEF)
-#  define _FUNCTION_DEF
-typedef int Function ();
-typedef void VFunction ();
-typedef char *CPFunction ();           /* no longer used */
-typedef char **CPPFunction ();         /* no longer used */
-#endif /* _FUNCTION_DEF */
-
 typedef int sh_cget_func_t (void);     /* sh_ivoidfunc_t */
 typedef int sh_cunget_func_t (int);    /* sh_intfunc_t */
 
@@ -70,6 +61,12 @@ extern BUFFERED_STREAM **buffers;
 extern int default_buffered_input;
 extern int bash_input_fd_changed;
 
+#undef beof
+#undef berror
+
+#define beof(bp)       (((bp)->b_flag & B_EOF) != 0)   
+#define berror(bp)     (((bp)->b_flag & B_ERROR) != 0)
+
 #endif /* BUFFERED_INPUT */
 
 typedef union {
@@ -122,6 +119,7 @@ extern int check_bash_input (int);
 extern int duplicate_buffered_stream (int, int);
 extern BUFFERED_STREAM *fd_to_buffered_stream (int);
 extern BUFFERED_STREAM *set_buffered_stream (int, BUFFERED_STREAM *);
+extern BUFFERED_STREAM *get_buffered_stream (int);
 extern BUFFERED_STREAM *open_buffered_stream (char *);
 extern void free_buffered_stream (BUFFERED_STREAM *);
 extern int close_buffered_stream (BUFFERED_STREAM *);
index c8f96e6f64c76fb7ae2ac926c3e790778cea6087..fe0b8798fd24c618a0ff912da86b587c6247a3c6 100644 (file)
@@ -2037,9 +2037,25 @@ rl_complete_internal (int what_to_do)
 
   text = rl_copy_text (start, end);
   matches = gen_completion_matches (text, start, end, our_func, found_quote, quote_char);
+  /* If TEXT contains quote characters, it will be dequoted as part of
+     generating the matches, and the matches will not contain any quote
+     characters. We need to dequote TEXT before performing the comparison.
+     Since compare_match performs the dequoting, and we only want to do it
+     once, we don't call compare_matches after dequoting TEXT; we call
+     strcmp directly. */
   /* nontrivial_lcd is set if the common prefix adds something to the word
      being completed. */
-  nontrivial_lcd = matches && compare_match (text, matches[0]) != 0;
+  if (rl_filename_completion_desired && rl_filename_quoting_desired &&
+      rl_completion_found_quote && rl_filename_dequoting_function)
+    {
+      char *t;
+      t = (*rl_filename_dequoting_function) (text, rl_completion_quote_character);
+      xfree (text);
+      text = t;
+      nontrivial_lcd = matches && strcmp (text, matches[0]) != 0;
+    }
+  else
+    nontrivial_lcd = matches && strcmp (text, matches[0]) != 0;
   if (what_to_do == '!' || what_to_do == '@')
     tlen = strlen (text);
   xfree (text);
index d1b3df2831e5e608375cf9b94bdaa56e4fe90a09..1fcd50e41ad58e7ff5db7f4992e95cdc994df1b4 100644 (file)
@@ -1,6 +1,6 @@
 /* random.c -- Functions for managing 16-bit and 32-bit random numbers. */
 
-/* Copyright (C) 2020,2022 Free Software Foundation, Inc.
+/* Copyright (C) 2020,2022,2023 Free Software Foundation, Inc.
 
    This file is part of GNU Bash, the Bourne Again SHell.
 
@@ -75,7 +75,7 @@ intrand32 (u_bits32_t last)
   /* Can't seed with 0. */
   ret = (last == 0) ? 123459876 : last;
   h = ret / 127773;
-  l = ret - (127773 * h);
+  l = ret % 127773;
   t = 16807 * l - 2836 * h;
   ret = (t < 0) ? t + 0x7fffffff : t;
 
diff --git a/parse.y b/parse.y
index e430f3aac0c8d6cbecce7de19c8ce8ee2878b9a5..5786e14e6d0737f4d046328764ef7278647a4eaa 100644 (file)
--- a/parse.y
+++ b/parse.y
@@ -97,6 +97,10 @@ typedef void *alias_t;
 
 #define END_ALIAS      -2
 
+#ifndef READERR
+#  define READERR      -2
+#endif
+
 #ifdef DEBUG
 #  define YYDEBUG 1
 #else
@@ -2433,6 +2437,15 @@ shell_getc (int remove_quoted_newline)
 
              if (i == 0)
                shell_input_line_terminator = EOF;
+#if defined (BUFFERED_INPUT)
+             if (i == 0)
+               {
+                  BUFFERED_STREAM *bp;
+                  bp = get_buffered_stream (default_buffered_input);
+                  if (bp && berror (bp))
+                    shell_input_line_terminator = READERR;
+               }
+#endif
 
              shell_input_line[i] = '\0';
              break;
@@ -2525,7 +2538,8 @@ shell_getc (int remove_quoted_newline)
             reason for the test against shell_eof_token, which is set to a
             right paren when parsing the contents of command substitutions. */
          if (echo_input_at_read && (shell_input_line[0] ||
-                                      shell_input_line_terminator != EOF) &&
+                                      (shell_input_line_terminator != EOF &&
+                                       shell_input_line_terminator != READERR)) &&
                                     shell_eof_token == 0)
            fprintf (stderr, "%s\n", shell_input_line);
        }
@@ -2540,7 +2554,7 @@ shell_getc (int remove_quoted_newline)
 
       /* Add the newline to the end of this string, iff the string does
         not already end in an EOF character.  */
-      if (shell_input_line_terminator != EOF)
+      if (shell_input_line_terminator != EOF && shell_input_line_terminator != READERR)
        {
          if (shell_input_line_size < SIZE_MAX-3 && (shell_input_line_len+3 > shell_input_line_size))
            shell_input_line = (char *)xrealloc (shell_input_line,
@@ -2676,6 +2690,9 @@ pop_alias:
 
   if (uc == 0 && shell_input_line_terminator == EOF)
     return ((shell_input_line_index != 0) ? '\n' : EOF);
+  else if (uc == 0 && shell_input_line_terminator == READERR)
+    /* Treat read errors like EOF here. */
+    return ((shell_input_line_index != 0) ? '\n' : EOF);
 
 #if defined (ALIAS) || defined (DPAREN_ARITHMETIC)
   /* We already know that we are not parsing an alias expansion because of the