x=<< functionality cannot be used in functions

Bug #1380389 reported by Thorsten Glaser
256
This bug affects 1 person
Affects Status Importance Assigned to Milestone
mksh
Fix Released
Medium
Thorsten Glaser

Bug Description

Several bugs with this construct:

x=<<EOF
foo
EOF

x+=<<
foo
<<

1. It cannot be used in functions: assignment is done at parse time
2. It uses ATEMP of parse time, which is gone at runtime in functions
   ⇒ hides first bug unless exec.c alloc(z * 2 + n, ATEMP) → APERM
3. Size calculations not fully checked, one too big
   ⇒ (z-1) * 2 + 1 /* EOS */ + n

Ad 1:

(sid-amd64)tglase@tglase:~/b $ ./mksh ~/x
a
abla5424 bar
abla5424 bar bla5424 bar
function bar {
        x+=\b\l\a\5\4\2\4\ \b\a\r\

}
(sid-amd64)tglase@tglase:~/b $ cat ~/x
function bar {
x+=<<
bla$RANDOM bar
<<
}
x=a
echo $x
bar
echo $x
bar
echo $x
typeset -f bar

Substitution happens at first use:

(sid-amd64)tglase@tglase:~/b $ ./mksh ~/x2
function bar {
        x=<<
bla$RANDOM bar
<<

}
function bar {
        x=\b\l\a\1\2\2\8\1\ \b\a\r\

}
(sid-amd64)tglase@tglase:~/b $ /bin/mksh ~/x2
function bar {
        x=<<
bla$RANDOM bar
<<

}
function bar {
        x=\b\l\a\2\6\7\7\9\ \b\a\r\

}
(sid-amd64)tglase@tglase:~/b $ cat ~/x2
function bar {
x=<<
bla$RANDOM bar
<<
}
typeset -f bar
bar
typeset -f bar

So this is even worse than thought, the ATEMP context is the one from the first use of the function…

This was introduced in R40, when the x=<< syntax was added.

Changed in mksh:
importance: High → Medium
Revision history for this message
Thorsten Glaser (mirabilos) wrote :

This additionally requires a reparse hack (see lex.c for IONDELIM) because the tree.c printing code appends a trailing newline to all constructs unconditionally; we can remove that when the latter is no longer true (just documenting).

Revision history for this message
Thorsten Glaser (mirabilos) wrote :

fix in commitid 100576F1D4100EFF065

Changed in mksh:
status: New → Fix Committed
Changed in mksh:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public Security information  
Everyone can see this security related information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.