Menu

#2303 stack overflow during variable assignment with command substitution and pipe

MSYS
assigned
Bug
works-for-me
Unknown
False
2016-06-10
2016-06-10
No

this sscce says it all:

x=$(echo a | grep a)
echo a=$x

expected output: "a=a"
actual output: "a="

when run, the above command does not assign the value 'a' to x as expected. also, it produces a bash.exe.stackdump in the pwd contianing "STATUS_STACK_OVERFLOW". this stack overflow seem so occur any time you combine variable assignment, command substitution and a pipe. backtick syntax seems to have the same issue.

the only work-around i have found is to use a temp file. e.g.

echo a | grep a > xfile
x=$(cat xfile)
rm xfile

details:
operating system: windows 7 (service pack 1)
mingw version: 64 bit bundled with git version 2.8.3.windows.1

$ uname -a
MINGW64_NT-6.1 REDACTED 2.5.0(0.295/5/3) 2016-03-31 18:47 x86_64 Msys

here is an example stack dump:

Exception: STATUS_STACK_OVERFLOW at rip=7FEFCB816D7
rax=0000000000003AB0 rbx=0000000000000000 rcx=000000000053B310
rdx=00000000003465D8 rsi=0000000000000025 rdi=000000000053B310
r8 =0000000000000010 r9 =00000000000000E0 r10=00000000FFFF7000
r11=00000000FFE03E90 r12=00000000003465D8 r13=0000000000000000
r14=0000000000533080 r15=FFFFFFFFFFFFFFFF
rbp=00000000FFFF7450 rsp=00000000FFFFADE8
program=C:\Program Files\Git\bin\..\usr\bin\bash.exe, pid 10696, thread unknown (0x604)
cs=0033 ds=002B es=002B fs=0053 gs=002B ss=002B
Stack trace:
Frame        Function    Args
000FFFF7450  7FEFCB816D7 (00000533080, 00000000000, 000003465D8, 0000053B310)
000FFFF7450  7FEFCB76AFE (00000000000, 00000000000, 000FFFFAE78, 000003465D8)
000FFFFAE78  7FEFCB7A966 (00000000025, 000003465D8, 00000533080, 000FFFFB030)
000FFFFAFC0  7FEFCB79B73 (00000539290, 001802A8940, 00076D67971, 42000000001)
00000000000  7FEFCB73D24 (00000000420, 00000000000, 001802A8940, 001802A8940)
000FFFFB9E0  00076E7064C (000FFFFB930, 000FFFFB938, 000FFFFB968, 00000000001)
000FFFFB9E0  0007E11FFDD (00000305DCC, 001802A8940, 001802A8940, 00000000001)
000FFFFB9E0  001800BEA3B (000FFFFBAA0, 00000000000, 7FE00000000, 00000000000)
000FFFFBB20  001800BF5E5 (00000000000, 001004D5858, 0010040F65E, 0007E16A1D8)
00000000000  0018012F44B (00000000000, 001004D5858, 0010040F65E, 0007E16A1D8)
00000000000  0000022E458 (001004D5858, 0010040F65E, 0007E16A1D8, 00000080002)
00000000000  0060017F830 (001004D5858, 0010040F65E, 0007E16A1D8, 00000080002)
End of stack trace

Discussion

  • teldon james turner

    it seems this bug does not appear when using the export command.
    so this might be a better work-around for some use cases:
    export x=$(echo a | grep a)

     
  • teldon james turner

    it seems this also occurs in conditional checks. sscce:

    if [ "0" == "`echo 0 | grep 0`" ]; then echo true;else echo false; fi
    expected output: true
    actual output: false
    

    this scenario also produces a stack overflow stack dump

     
  • Keith Marshall

    Keith Marshall - 2016-06-10
    • status: unread --> assigned
    • assigned_to: Cesar Strauss
    • Resolution: none --> works-for-me
     
  • Keith Marshall

    Keith Marshall - 2016-06-10

    Your original SSCCE (and its backtick variant) WJFFM, running our official MSYS distribution in a 32-bit WinXP VM, on 64-bit LinuxMint host:

    $ uname -a
    MINGW32_NT-5.1 MINGW-ZB01OH0LG 1.0.17(0.48/3/2) 2011-04-24 23:39 i686 Msys
    
    $ x=`echo a | grep a`
    
    $ echo a=$x
    a=a
    
    $ x=$(echo b | grep b)
    
    $ echo b=$x
    b=b
    

    Likewise, your second SSCCE:

    $ if [ "0" = "`echo 0 | grep 0`" ]; then echo true; else echo false; fi
    true
    

    Please be advised that MSYS, as bundled with git for windows, may be modified from our official distribution, (and 64-bit MinGW certainly isn't ours); thus we don't formally support either of these. Nonetheless, I'll invite Cesar Strauss, (the official MSYS maintainer), to comment.

     
MongoDB Logo MongoDB