Subject: bug#20095: conditional branching

From: Davide Brini <[email-masked]>
Date: Thu, 12 Mar 2015 10:01:31 +0100

On Thu, 12 Mar 2015 02:42:07 +0000 (UTC), Erik Popp <[email-masked]>
wrote:

> I'm using sed to verify that a log file is in the correct format, to make
> it impossible for crafted input to cause any problems in a shell script.
> 
> I'm stumped, and the only explanation that I can think of for why my
> script isn't working is that sed's "t" command is not working properly.
> 
> If I comment out any 2 of the 3 main commands, the output is as expected:
> white space stripper always "succeeds", and thus the script jumps to the
> success section curly brace detector jumps to the "success" section if
> triggered.  otherwise, the t command fails, because there wasn't a
> substitution in the first place entry converter succeeds if format of
> entry is valid, assuming that there isn't any surrounding white space
> However, if I un-comment all 3 of the main commands, the script doesn't
> fail when input is invalid.  Instead, it spits out the un-transformed
> invalid input, as if it was valid.
> 
> Am I missing something, or is this a bug?

If I understand correctly (I've had only a quick look at the code): the
description for the "t" command says

     Branch to LABEL only if there has been a successful `s'ubstitution
     since the last input line was read or conditional branch was taken.
     The LABEL may be omitted, in which case the next cycle is started.

(note: since the last conditional branch was taken) which, in simple terms,
means that with this sample code:

s/foo/bar/
s/xxx/yyy/
t successxxx

the code will branch to :successxxx if *either* substitution succeeds.
For this reason it's sometimes needed to "reset" the state of the "t"
command, so to speak, as follows:

s/foo/bar/
t successfoo
:successfoo
s/xxx/yyy/
t successxxx

with this modification, the code will branch to :successxxx only if the
last substitution succeeds.

-- 
D.