red icon indicating copy to clipboard operation
red copied to clipboard

usage of heredoc with QUIT as a delimiting identifier causes access violation

Open 9214 opened this issue 6 years ago • 3 comments

Describe the bug Usage of Red toolchain or console with heredoc and quit as a delimiting identifier leads to allocator-related crash.

To reproduce With console compiled in debug mode:

$ ./console << quit
> 1
> quit
--== Red 0.6.4 ==-- 
Type HELP for starting information. 

== 
root: 3033/4660, runs: 0, mem: 3389944
*** Runtime Error 98: assertion failed
*** in file: .../red/runtime/allocator.reds
*** at line: 359
***
***   stack: red/free-node 00000062h
***   stack: red/free-node 2E3DB374h
***   stack: red/cross-compact-frame 0216A004h 01009428h
***   stack: red/collect-frames 1
***   stack: red/collector/do-mark-sweep
***   stack: red/collector/do-cycle
***   stack: red/alloc-series-buffer 1 1 0
***   stack: red/alloc-series 1 1 0
***   stack: red/alloc-bytes 1
***   stack: red/string/make 003BD0D4h 003BD0E4h 7
***   stack: red/actions/make 003BD0D4h 003BD0E4h
***   stack: red/actions/make*
***   stack: ask
***   stack: ctx419~run 00315A34h
***   stack: ctx419~launch 00315A34h

Entering only quit (or q) sends session into an infinite loop:

$ ./console << q
> q
--== Red 0.6.4 ==-- 
Type HELP for starting information. 

root: 3033/4660, runs: 0, mem: 3389984 => 1509484, mark: 1.4ms, sweep: 3.4ms
root: 3033/4660, runs: 1, mem: 3390684 => 1509484, mark: 1.7ms, sweep: 3.2ms
root: 3033/4660, runs: 2, mem: 3390684 => 1509484, mark: 1.3ms, sweep: 3.0ms
root: 3033/4660, runs: 3, mem: 3390684 => 1509484, mark: 1.3ms, sweep: 3.3ms
root: 3033/4660, runs: 4, mem: 3390684 => 1509484, mark: 1.4ms, sweep: 3.0ms
root: 3033/4660, runs: 5, mem: 3390684 => 1509484, mark: 1.3ms, sweep: 3.3ms
root: 3033/4660, runs: 6, mem: 3390684 => 1509484, mark: 1.5ms, sweep: 3.0ms
root: 3033/4660, runs: 7, mem: 3390684 => 1509484, mark: 1.4ms, sweep: 3.3ms
root: 3033/4660, runs: 8, mem: 3390684 => 1509484, mark: 1.6ms, sweep: 3.4ms
^C

Expected behavior REPL session should be closed without any errors.

Platform version (please complete the following information)

Red 0.6.4 for macOS built 23-Jul-2019/21:51:39+02:00 commit #613d71c

9214 avatar Jul 24 '19 15:07 9214

What's the expected behavior when running the following command?

$ ./console << "print 32"

qtxie avatar Jul 24 '19 16:07 qtxie

@qtxie hard to say, I guess print out 32 and then quit?

9214 avatar Jul 24 '19 17:07 9214

What's the expected behavior when running the following command?

$ ./console << "print 32"

Assuming POSIX shell, then this is not valid syntax. The following would be valid:

$ ./console << "print 32"
print 32

and wouldn't produce any character on output as the executable ./console will open stdin and the first read from this stdin will return EOF and no bytes will be read. To feed hello world bytes one would need to use:

$ ./console << "print 32"
hello world
print 32

Note also, that POSIX shell syntax allows for continued code after the heredoc opening - e.g.

cat <<\x another_arg_for_cat yet_another_arg_for_cat; echo some other code; false || {
content of heredoc with $variable substitution ${disabled} due to the backslash specifier
x
  echo this code is part of the opened brace above
}  # let's close the brace
echo and proceed further with the script

But be careful - the backslash is an escape character and escaping has precedence over the opening heredoc preamble (might be unintuitive especially with standard shell delimiting characters like " or ' etc.):

variable=5
enabled=9
cat <<\'x'yz' another_arg_for_cat yet_another_arg_for_cat; echo some other code; false || {
content of heredoc with working $variable substitution ${enabled}, yay!
'xyz
  echo this is inside of braces
}
echo finally out of it

dumblob avatar Sep 13 '19 14:09 dumblob