Assaf Gordon 2cb09e1463 sed: fix extraneous NUL in s///n command
Under certain conditions sed would add an extraneous NUL:

    $ echo 0 | sed -e 's/$/a/2' | od -tx1 -An
     30 00 0a

This would happen when the regex is an empty (zero-length) match at the
end of the line (e.g. '$' and 'a*$') and the substitute number flag
('n' in s///n) is higher than the number of actual matches (multiple
EOL matches are possible with multiline match, e.g. 's/$/a/3m').

Details:
The comment at the top of 'execute.c:do_subst()' says:

       /* The first part of the loop optimizes s/xxx// when xxx is at the
          start, and s/xxx$// */

Which refers to lines 1051-3:

  1051 /* Copy stuff to the left of this match into the output string. */
  1052 if (start < offset)
  1053   str_append(&s_accum, line.active + start, offset - start);

The above code appends text to 's_accum' but does *not* update 'start'.

Later on, if the s/// command includes 'n' flag, and if 'matched == 0'
(an empty match), this comparison will be incorrect:

  1081  if (start < line.length)
  1082     matched = 1;

Will in turn will set 'matched' to 1, and the 'str_append' call that
follows (line 1087) will append an additional character.
Because the empty match is EOL, the appended character is NUL.

More examples that trigger the bug:

  echo 0 | sed -e 's/a*$/X/3'
  printf "%s\n" 0 0 0 | sed -e 'N;N;s/a*$/X/4m'

Examples that do not trigger the bug:

  # The 'a*' empty regex matches at the beginning of the line (in
  # addition to the end of the line), and the optimization in line
  # 1052 is skipped.
  echo 0 | sed -e 's/a*/X/3'

  # There are 3 EOLs in the pattern space, s///3 is not too large.
  printf "%s\n" 0 0 0 | sed -e 'N;N;s/a*$/X/3m'

This was discovered while investigating bug#32271 reported by bugs@feusi.co
in https://lists.gnu.org/r/bug-sed/2018-07/msg00018.html .

* NEWS: Mention the fix.
* sed/execute.c (do_subst): Update 'start' as needed.
* testsuite/bug-32271-1.sh: New test.
* testsuite/local.mk (T): Add test.
2018-08-03 19:36:19 -06:00
2018-07-25 10:45:16 -06:00
2018-03-28 13:22:25 -07:00
2014-11-30 18:59:33 -08:00
2018-03-22 16:12:26 -07:00
2008-04-10 11:14:38 +02:00
2018-03-22 16:12:26 -07:00
2016-08-26 11:54:30 -04:00
2016-08-22 20:57:02 -04:00
2018-03-22 16:00:43 -07:00
2015-05-01 18:09:51 -07:00
2018-03-31 18:41:03 -07:00
2015-05-01 18:09:51 -07:00
2018-08-03 19:36:19 -06:00

This is the GNU implementation of sed, the Unix stream editor.

GNU Sed website: https://www.gnu.org/software/sed/

See the NEWS file for a brief summary and the ChangeLog for
more detailed descriptions of changes.

If you obtained this file as part of a "git clone", then see the
README-hacking file.  If this file came to you as part of a tar archive,
then see the file INSTALL for compilation and installation instructions.

See the file BUGS for instructions about reporting bugs.

See the files AUTHORS and THANKS for a list of authors and other contributors.

After installation run 'sed --help' or 'man sed' for short usage information,
and 'info sed' for the complete manual. The manual is also available on
sed's website.
S
Description
SED (stream editor) - неинтерактивный текстовый редактор командной строки
https://www.gnu.org/software/sed/
Readme GPL-3.0
3 MiB
0 Stars 1 Watchers 0 Forks
Languages
Shell 43%
C 26.4%
Perl 16.5%
sed 7.3%
Makefile 5.2%
Other 1.6%