ScintillaOrg / lexilla

A library of language lexers for use with Scintilla
https://www.scintilla.org/Lexilla.html
Other
163 stars 59 forks source link

Shell language lexing mistakes bitwise left shift operator for here document #215

Closed echo-e closed 6 months ago

echo-e commented 7 months ago

Originally reported as bug #1940 in Notepad++. The Shell language bitwise shift operator (as when used between (( )) ) may be interpreted as a heredoc redirect. This has been partially fixed since originally reported in 2016, but I have found a new variant of the problem with this code snippet:

for (( i=0 ; i<W_HID ; i=i+1 )); do
  s_hid=$(( s_hid + (((status & 1 << (W_HID - i - 1)) > 0) << i) ))
done

I've verified that SciTE v5.4.0 also suffers from this error.

zufuliu commented 7 months ago

Simplified as:

for ((i = 0; i < 2; i++)); do
  echo $(((1) << i)) # colored as shift operator
done

for ((i = 0; i < 2; i++)); do
  echo $((((1)) << i)) # colored as here-doc
done
zufuliu commented 7 months ago

patch to fix the bug: 215.patch

diff --git a/lexers/LexBash.cxx b/lexers/LexBash.cxx
index 157af818..199172ec 100644
--- a/lexers/LexBash.cxx
+++ b/lexers/LexBash.cxx
@@ -333,7 +333,7 @@ public:
    }
    bool CountDown(StyleContext &sc, CmdState &cmdState) {
        Current.Count--;
-       if (Current.Count == 1 && sc.Match(')', ')')) {
+       while (Current.Count > 0 && sc.chNext == Current.Down) {
            Current.Count--;
            sc.Forward();
        }
@@ -380,10 +380,6 @@ public:
                    sc.ChangeState(SCE_SH_BACKTICKS);
                }
            }
-           if (current == CmdState::Body && sc.Match('(', '(') && state == SCE_SH_DEFAULT && Depth == 0) {
-               // optimized to avoid track nested delimiter pairs
-               style = QuoteStyle::Literal;
-           }
        } else {
            // scalar has no delimiter pair
            if (!setParamStart.Contains(sc.ch)) {
nyamatongwe commented 7 months ago

Looks good since its a simplification - committed.