Skip to content

Commit

Permalink
ed: runtime error in edSubstitute() (#577)
Browse files Browse the repository at this point in the history
* The program crashes when fed unexpected input after the initial "s" command
* I found this when investigating a capture-variable-outside-of-condition warning from perlcritic
* $char is the 1st character after the "s"; normally it would be "/" as in s/old/new/
* Return early if $args[0] is an empty string
* Adding eval guard around the confusing regex constructed from $char prevents the error
* Possibly the code could be changed later to enforce that $char is '/'

perl ed -p 'ed% ' a.c 
63
ed% 1
#include <stdio.h>
ed% s*asaaa*
Unknown verb pattern '' in regex; marked by <-- HERE in m/((*) <-- HERE [^"*"]*(*)[^"*"]*(*)?)([imsx]*)/ at ed line 519, <> line 2.
  • Loading branch information
mknos committed Apr 24, 2024
1 parent dd14275 commit 1038b0e
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions bin/ed
Expand Up @@ -506,17 +506,22 @@ sub edSubstitute {
edWarn(E_ADDRBAD);
return;
}
unless (defined($args[0])) {
if (!defined($args[0]) || length($args[0]) == 0) {
edWarn(E_PATTERN);
return;
}

# do wierdness to match semantics if last character
# is present or absent

$args[0] =~ /(.).*/;
$char = $1;
($whole,$first,$middle,$last,$flags) = ($args[0] =~ /(($char)[^"$char"]*($char)[^"$char"]*($char)?)([imsx]*)/);
$char = substr $args[0], 0, 1;
eval {
($whole,$first,$middle,$last,$flags) = ($args[0] =~ /(($char)[^"$char"]*($char)[^"$char"]*($char)?)([imsx]*)/);
1;
} or do {
edWarn(E_PATTERN);
return;
};

if (defined($char) and defined($whole) and
($flags eq "") and (not defined($last))) {
Expand Down

0 comments on commit 1038b0e

Please sign in to comment.