From aab5852676a9b5d4b8058e10b01eaf392d992dab6443f82d2c34e3bd19d4ab43 Mon Sep 17 00:00:00 2001 From: Stanislav Brabec Date: Thu, 27 Aug 2015 16:10:55 +0000 Subject: [PATCH] Accepting request 326633 from home:sbrabec:branches:Base:System - Cherry picking of the most important fixes from the upstream sed GIT (not backporting testsuite, as it was completely refactored): * Fix y command in the RHS of a y/LHS/RHS/ transliteration (sed-y-NUL-RHS.patch). * Fix mishandling of overlapping address ranges (sed-fix-overlapping-address-ranges.patch). * Fix fail to remove a temporary file (sed-temp-delete.patch). * Fix behavior of --follow-symlinks when reading from stdin (bnc#933029, gnu#20795, sed-follow-symlinks-stdin.patch). * Make "sed --follow-symlinks -" consistent with "sed -" again, and process stdin instead of ./- (bnc#933029#c6, gnu#20796, sed-follow-symlinks-hyphen.patch). OBS-URL: https://build.opensuse.org/request/show/326633 OBS-URL: https://build.opensuse.org/package/show/Base:System/sed?expand=0&rev=22 --- sed-fix-overlapping-address-ranges.patch | 85 +++++++++++++++++ sed-follow-symlinks-hyphen.patch | 51 +++++++++++ sed-follow-symlinks-stdin.patch | 74 +++++++++++++++ sed-temp-delete.patch | 112 +++++++++++++++++++++++ sed-y-NUL-RHS.patch | 45 +++++++++ sed.changes | 16 ++++ sed.spec | 17 +++- 7 files changed, 399 insertions(+), 1 deletion(-) create mode 100644 sed-fix-overlapping-address-ranges.patch create mode 100644 sed-follow-symlinks-hyphen.patch create mode 100644 sed-follow-symlinks-stdin.patch create mode 100644 sed-temp-delete.patch create mode 100644 sed-y-NUL-RHS.patch diff --git a/sed-fix-overlapping-address-ranges.patch b/sed-fix-overlapping-address-ranges.patch new file mode 100644 index 0000000..aad2bba --- /dev/null +++ b/sed-fix-overlapping-address-ranges.patch @@ -0,0 +1,85 @@ +This is a backport of the fix itself. + +From 4c75f64068a7e1446c9aa6ae8f764e0ebddd67ef Mon Sep 17 00:00:00 2001 +From: Norihiro Tanaka +Date: Fri, 20 Feb 2015 01:54:35 +0900 +Subject: [PATCH 81/92] sed: fix mishandling of overlapping address ranges + +When the line number ranges of two or more editing commands overlap, +sed applies all commands but the first to a line that is just beyond +the union of all ranges. E.g., with this command, +seq 9|sed '2,7d;3,6d', sed would mistakenly delete line 8. + +* sed/execute.c (match_an_address_p) [ADDR_IS_NUM]: Make this +function work also in this case. +(match_address_p): Move the ADDR_IS_NUM + +...->line_number == ...->addr_number comparison "up" into +match_an_address_p, so we can hoist two similar if/return +blocks out of "if" and "else" branches. +Change so that this function returns false when the current +line number is outside the specified range. +* testsuite/range-overlap.sh: New file, to test for this. +* testsuite/Makefile.am (T): Add it to the list. +* NEWS (Bug fixes): Mention it. +Reported as http://bugs.gnu.org/19899 +--- + NEWS | 15 +++++++++++++++ + sed/execute.c | 16 ++++++++-------- + testsuite/Makefile.am | 3 ++- + testsuite/range-overlap.sh | 34 ++++++++++++++++++++++++++++++++++ + 4 files changed, 59 insertions(+), 9 deletions(-) + create mode 100755 testsuite/range-overlap.sh + +Index: sed-4.2.2/sed/execute.c +=================================================================== +--- sed-4.2.2.orig/sed/execute.c ++++ sed-4.2.2/sed/execute.c +@@ -860,8 +860,10 @@ match_an_address_p(addr, input) + case ADDR_IS_LAST: + return test_eof(input); + +- /* ADDR_IS_NUM is handled in match_address_p. */ + case ADDR_IS_NUM: ++ /* reminder: these are only meaningful for a1 addresses */ ++ return (addr->addr_number == input->line_number); ++ + default: + panic("INTERNAL ERROR: bad address type"); + } +@@ -881,23 +883,20 @@ match_address_p(cmd, input) + + if (cmd->range_state != RANGE_ACTIVE) + { ++ if (!cmd->a2) ++ return match_an_address_p(cmd->a1, input); ++ + /* Find if we are going to activate a range. Handle ADDR_IS_NUM + specially: it represent an "absolute" state, it should not + be computed like regexes. */ + if (cmd->a1->addr_type == ADDR_IS_NUM) + { +- if (!cmd->a2) +- return (input->line_number == cmd->a1->addr_number); +- + if (cmd->range_state == RANGE_CLOSED + || input->line_number < cmd->a1->addr_number) + return false; + } + else + { +- if (!cmd->a2) +- return match_an_address_p(cmd->a1, input); +- + if (!match_an_address_p(cmd->a1, input)) + return false; + } +@@ -913,7 +912,8 @@ match_address_p(cmd, input) + /* Same handling as below, but always include at least one line. */ + if (input->line_number >= cmd->a2->addr_number) + cmd->range_state = RANGE_CLOSED; +- return true; ++ return (input->line_number <= cmd->a2->addr_number ++ || match_an_address_p(cmd->a1, input)); + case ADDR_IS_STEP: + cmd->a2->addr_number = input->line_number + cmd->a2->addr_step; + return true; diff --git a/sed-follow-symlinks-hyphen.patch b/sed-follow-symlinks-hyphen.patch new file mode 100644 index 0000000..8a5f050 --- /dev/null +++ b/sed-follow-symlinks-hyphen.patch @@ -0,0 +1,51 @@ +This is a backport of the fix itself. + +From c033bdee411128dfebfea1974d1ee3c1d9eac572 Mon Sep 17 00:00:00 2001 +From: Jim Meyering +Date: Sat, 20 Jun 2015 07:38:49 -0700 +Subject: [PATCH] sed -i: do not treat "-" as a file name + +Most GNU utilities treat "-" as standard input, sed itself does that -- +in most contexts. However, since the addition of support for +--in-place (-i) in sed-4.0, sed -i has treated a "-" argument as a +file name, i.e., like ./-. Now, that usage evokes a diagnostic: + + $ sed -i s/a/b/ - + sed: couldn't edit -: is a terminal + +If you require the old behavior, specify the file name as "./-". +Prompted by the report/patch from Stanislav Brabec in +http://bugs.gnu.org/20796 to document the strangely +inconsistent legacy behavior. + +* sed/execute.c (open_next_file): Call panic before +even attempting to operate on the file descriptor. +* testsuite/in-place-hyphen.sh: New file. Test for this. +* testsuite/Makefile.am (T): Add it. +* NEWS (Feature removal): Mention it. Admittedly, the old behavior +feels more like a misfeature. +--- + NEWS | 3 +++ + sed/execute.c | 5 ++++- + testsuite/Makefile.am | 1 + + testsuite/in-place-hyphen.sh | 29 +++++++++++++++++++++++++++++ + 4 files changed, 37 insertions(+), 1 deletion(-) + create mode 100755 testsuite/in-place-hyphen.sh + +Index: sed-4.2.2/sed/execute.c +=================================================================== +--- sed-4.2.2.orig/sed/execute.c ++++ sed-4.2.2/sed/execute.c +@@ -580,8 +580,11 @@ open_next_file(name, input) + { + buffer.length = 0; + +- if (name[0] == '-' && name[1] == '\0' && !in_place_extension) ++ if (name[0] == '-' && name[1] == '\0') + { ++ if (in_place_extension) ++ panic(_("couldn't edit %s: is a terminal"), name); ++ + clearerr(stdin); /* clear any stale EOF indication */ + #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS) || defined(__EMX__) + input->fp = ck_fdopen (fileno (stdin), "stdin", read_mode, false); diff --git a/sed-follow-symlinks-stdin.patch b/sed-follow-symlinks-stdin.patch new file mode 100644 index 0000000..f4c8ecf --- /dev/null +++ b/sed-follow-symlinks-stdin.patch @@ -0,0 +1,74 @@ +This is a backport of the fix itself. + +From e387009c78fa08aaebf5192a3ceeb04dcfc7781d Mon Sep 17 00:00:00 2001 +From: Stanislav Brabec +Date: Mon, 13 Jul 2015 22:59:17 +0200 +Subject: [PATCH] sed: fix --follow-symlinks to work when reading stdin + +When reading from stdin, use of --follow-symlinks would cause failure: + + $ echo abc | sed --follow-symlinks s/a/d/ + sed: cannot stat -: No such file or directory + +* sed/execute.c (open_next_file): Set input->in_file_name earlier, +so we can rearrange logic to avoid calling follow_symlink("-"). +* testsuite/follow-symlinks-stdin.sh: New file. +* testsuite/Makefile.am (T): Add it. +* NEWS (Bug fixes): Mention it. +Bug introduced by commit v4.2.1-13-g84066bf. +http://bugs.gnu.org/20795 +--- + NEWS | 3 +++ + sed/execute.c | 24 +++++++++++++----------- + testsuite/Makefile.am | 1 + + testsuite/follow-symlinks-stdin.sh | 29 +++++++++++++++++++++++++++++ + 4 files changed, 46 insertions(+), 11 deletions(-) + create mode 100755 testsuite/follow-symlinks-stdin.sh + +Index: sed-4.2.2/sed/execute.c +=================================================================== +--- sed-4.2.2.orig/sed/execute.c ++++ sed-4.2.2/sed/execute.c +@@ -580,6 +580,7 @@ open_next_file(name, input) + { + buffer.length = 0; + ++ input->in_file_name = name; + if (name[0] == '-' && name[1] == '\0') + { + if (in_place_extension) +@@ -592,22 +593,23 @@ open_next_file(name, input) + input->fp = stdin; + #endif + } +- else if ( ! (input->fp = ck_fopen(name, read_mode, false)) ) ++ else + { +- const char *ptr = strerror(errno); +- fprintf(stderr, _("%s: can't read %s: %s\n"), myname, name, ptr); +- input->read_fn = read_always_fail; /* a redundancy */ +- ++input->bad_count; +- return; ++ if (follow_symlinks) ++ input->in_file_name = follow_symlink (name); ++ ++ if ( ! (input->fp = ck_fopen (name, read_mode, false)) ) ++ { ++ const char *ptr = strerror (errno); ++ fprintf (stderr, _("%s: can't read %s: %s\n"), myname, name, ptr); ++ input->read_fn = read_always_fail; /* a redundancy */ ++ ++input->bad_count; ++ return; ++ } + } + + input->read_fn = read_file_line; + +- if (follow_symlinks) +- input->in_file_name = follow_symlink (name); +- else +- input->in_file_name = name; +- + if (in_place_extension) + { + int input_fd; diff --git a/sed-temp-delete.patch b/sed-temp-delete.patch new file mode 100644 index 0000000..a15b694 --- /dev/null +++ b/sed-temp-delete.patch @@ -0,0 +1,112 @@ +This is a backport of the fix itself. + +From 768901548e280726f160a1da4434f3fde8f9921a Mon Sep 17 00:00:00 2001 +From: Jim Meyering +Date: Sat, 9 May 2015 19:47:08 -0700 +Subject: [PATCH 84/92] sed -i: don't leave behind a temporary sedXXXXXX file + +For example, running a command like "sed -i s//b/ F" would fail +to remove a temporary file named sedXXXXXX (for random XXXXXX) +in the directory alongside F. +* sed/sed.c (G_file_to_unlink): New global. +(register_cleanup_file, cancel_cleanup, cleanup): New functions. +(main): Call atexit. +* sed/execute.c (open_next_file): Register for unlink. +(closedown): Call cancel_cleanup right after the rename. +* sed/sed.h: Declare two of the new functions. +* NEWS (Bug fixes): Mention it. +* testsuite/temp-file-cleanup.sh: New file. Test for this. +* testsuite/Makefile.am (T): Add it. +Reported by David Jones in http://bugs.gnu.org/20002. +--- + NEWS | 4 ++++ + sed/execute.c | 2 ++ + sed/sed.c | 35 ++++++++++++++++++++++++++++++++++- + sed/sed.h | 2 ++ + testsuite/Makefile.am | 3 ++- + testsuite/temp-file-cleanup.sh | 38 ++++++++++++++++++++++++++++++++++++++ + 6 files changed, 82 insertions(+), 2 deletions(-) + create mode 100755 testsuite/temp-file-cleanup.sh + +Index: sed-4.2.2/sed/execute.c +=================================================================== +--- sed-4.2.2.orig/sed/execute.c ++++ sed-4.2.2/sed/execute.c +@@ -656,6 +656,7 @@ open_next_file(name, input) + + output_file.fp = ck_mkstemp (&input->out_file_name, tmpdir, "sed", + write_mode); ++ register_cleanup_file (input->out_file_name); + output_file.missing_newline = false; + free (tmpdir); + +@@ -713,6 +714,7 @@ closedown(input) + } + + ck_rename (input->out_file_name, target_name, input->out_file_name); ++ cancel_cleanup (); + free (input->out_file_name); + } + else +Index: sed-4.2.2/sed/sed.c +=================================================================== +--- sed-4.2.2.orig/sed/sed.c ++++ sed-4.2.2/sed/sed.c +@@ -69,6 +69,35 @@ countT lcmd_out_line_len = 70; + /* The complete compiled SED program that we are going to run: */ + static struct vector *the_program = NULL; + ++/* When we've created a temporary for an in-place update, ++ we may have to exit before the rename. This is the name ++ of the temporary that we'll have to unlink via an atexit- ++ registered cleanup function. */ ++static char const *G_file_to_unlink; ++ ++/* When exiting between temporary file creation and the rename ++ associated with a sed -i invocation, remove that file. */ ++static void ++cleanup (void) ++{ ++ if (G_file_to_unlink) ++ unlink (G_file_to_unlink); ++} ++ ++/* Note that FILE must be removed upon exit. */ ++void ++register_cleanup_file (char const *file) ++{ ++ G_file_to_unlink = file; ++} ++ ++/* Clear the global file-to-unlink global. */ ++void ++cancel_cleanup (void) ++{ ++ G_file_to_unlink = NULL; ++} ++ + static void usage (int); + static void + contact(errmsg) +@@ -200,6 +229,10 @@ main(argc, argv) + #endif + initialize_mbcs (); + ++ /* Arrange to remove any un-renamed temporary file, ++ upon premature exit. */ ++ atexit (cleanup); ++ + #if ENABLE_NLS + + /* Tell program which translations to use and where to find. */ +Index: sed-4.2.2/sed/sed.h +=================================================================== +--- sed-4.2.2.orig/sed/sed.h ++++ sed-4.2.2/sed/sed.h +@@ -262,4 +262,6 @@ extern bool is_utf8; + + extern int brlen (int ch, mbstate_t *ps); + extern void initialize_mbcs (void); ++extern void register_cleanup_file (char const *file); ++extern void cancel_cleanup (void); + diff --git a/sed-y-NUL-RHS.patch b/sed-y-NUL-RHS.patch new file mode 100644 index 0000000..56bb08c --- /dev/null +++ b/sed-y-NUL-RHS.patch @@ -0,0 +1,45 @@ +This is a backport of the fix itself. + +From 643f692bc7b02086e91c2afd12bf9ac8b40b6fe4 Mon Sep 17 00:00:00 2001 +From: Jim Meyering +Date: Thu, 4 Sep 2014 20:52:12 -0700 +Subject: [PATCH 12/92] fix "y" to work with NUL in the RHS + +* sed/execute.c (do_list) [case 'y']: Handle NUL bytes +in the RHS of a y/LHS/RHS/ transliteration. +* testsuite/y-zero.good: New test-related files. +* testsuite/y-zero.inp: +* testsuite/y-zero.sed: +* testsuite/Makefile.am (SEDTESTS): Add y-zero here. +* testsuite/Makefile.tests: And here. +* NEWS (Bug fixes): Describe it. +The bug was reported by table@inventati.org, +with the execute.c change mostly by Paolo Bonzini. +--- + ChangeLog | 14 ++++++++++++++ + NEWS | 18 +++++++++++++++++- + sed/execute.c | 3 ++- + testsuite/Makefile.am | 3 ++- + testsuite/Makefile.tests | 2 +- + testsuite/y-zero.good | Bin 0 -> 4 bytes + testsuite/y-zero.inp | 1 + + testsuite/y-zero.sed | 1 + + 8 files changed, 38 insertions(+), 4 deletions(-) + create mode 100644 testsuite/y-zero.good + create mode 100644 testsuite/y-zero.inp + create mode 100644 testsuite/y-zero.sed + +Index: sed-4.2.2/sed/execute.c +=================================================================== +--- sed-4.2.2.orig/sed/execute.c ++++ sed-4.2.2/sed/execute.c +@@ -1572,7 +1572,8 @@ execute_program(vec, input) + if (strncmp(line.active + idx, trans[2*i], mbclen) == 0) + { + bool move_remain_buffer = false; +- int trans_len = strlen(trans[2*i+1]); ++ const char *tr = trans[2*i+1]; ++ size_t trans_len = *tr == '\0' ? 1 : strlen (tr); + + if (mbclen < trans_len) + { diff --git a/sed.changes b/sed.changes index 3a7c390..63ac43c 100644 --- a/sed.changes +++ b/sed.changes @@ -1,3 +1,19 @@ +------------------------------------------------------------------- +Tue Aug 25 16:54:40 CEST 2015 - sbrabec@suse.com + +- Cherry picking of the most important fixes from the upstream sed + GIT (not backporting testsuite, as it was completely refactored): + * Fix y command in the RHS of a y/LHS/RHS/ transliteration + (sed-y-NUL-RHS.patch). + * Fix mishandling of overlapping address ranges + (sed-fix-overlapping-address-ranges.patch). + * Fix fail to remove a temporary file (sed-temp-delete.patch). + * Fix behavior of --follow-symlinks when reading from stdin + (bnc#933029, gnu#20795, sed-follow-symlinks-stdin.patch). + * Make "sed --follow-symlinks -" consistent with "sed -" again, + and process stdin instead of ./- + (bnc#933029#c6, gnu#20796, sed-follow-symlinks-hyphen.patch). + ------------------------------------------------------------------- Tue Dec 30 11:15:38 UTC 2014 - meissner@suse.com diff --git a/sed.spec b/sed.spec index f919b3f..45efad9 100644 --- a/sed.spec +++ b/sed.spec @@ -1,7 +1,7 @@ # # spec file for package sed # -# Copyright (c) 2014 SUSE LINUX Products GmbH, Nuernberg, Germany. +# Copyright (c) 2015 SUSE LINUX GmbH, Nuernberg, Germany. # # All modifications and additions to the file contributed by third parties # remain the property of their copyright owners, unless otherwise agreed @@ -28,6 +28,16 @@ Source1: %{name}-%{version}.tar.bz2.sig Source2: %{name}.keyring # PATCH-FIX-SLE sed-dont_close_twice.patch bnc@880817 tcech@suse.cz -- Fix double close. Patch: sed-dont_close_twice.patch +# PATCH-FIX-UPSTREAM sed-follow-symlinks-hyphen.patch bnc933029 gnu20796 sbrabec@suse.com -- Make behavior of "sed --follow-symlinks -" consistent again. +Patch1: sed-follow-symlinks-hyphen.patch +# PATCH-FIX-UPSTREAM sed-follow-symlinks-stdin.patch bnc933029 gnu20795 sbrabec@suse.com -- Fix --follow-symlinks on stdin. +Patch2: sed-follow-symlinks-stdin.patch +# PATCH-FIX-UPSTREAM sed-y-NUL-RHS.patch sbrabec@suse.com -- Fix y command in the RHS of a y/LHS/RHS/ transliteration. +Patch3: sed-y-NUL-RHS.patch +# PATCH-FIX-UPSTREAM sed-fix-overlapping-address-ranges.patch sbrabec@suse.com -- Fix mishandling of overlapping address ranges. +Patch4: sed-fix-overlapping-address-ranges.patch +# PATCH-FIX-UPSTREAM sed-temp-delete.patch sbrabec@suse.com -- Fix fail to remove a temporary file. +Patch5: sed-temp-delete.patch # Use rpmbuild -D 'VERIFY_SIG 1' to verify signature during build or run one-shot check by "gpg-offline --verify --package=sed sed-*.sig". %if 0%{?VERIFY_SIG} BuildRequires: gpg-offline @@ -50,6 +60,11 @@ occurrences of a string within a file. %endif %setup -q %patch -p1 +%patch1 -p1 +%patch2 -p1 +%patch3 -p1 +%patch4 -p1 +%patch5 -p1 %build %define warn_flags -Wall -Wstrict-prototypes -Wpointer-arith -Wformat-security