Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Console crash if break is evaluated on foreach-face loop #3991

Open
planetsizecpu opened this issue Aug 15, 2019 · 10 comments
Open

Console crash if break is evaluated on foreach-face loop #3991

planetsizecpu opened this issue Aug 15, 2019 · 10 comments
Assignees
Labels
status.built A change in codebase has been done to address the ticket. status.tested The change in code has been manually tested and verified to fix the issue. type.bug Ticket describes an abnormal behavior, not conforming to the specs or expectation.

Comments

@planetsizecpu
Copy link

planetsizecpu commented Aug 15, 2019

Describe the bug
The Red console crashes if BREAK is evaluated inside a FOREACH-FACE loop

To reproduce
Steps to reproduce the behavior:

Copy this code on console:

l: layout [a: area 100x100 red b: box 100x100 green c: field 100x100 blue]
foreach-face l [if face/size/x = 100 [break]]

Expected behavior
The loop ends and return to the console prompt.

Platform version (please complete the following information)
Tested on W10

Red 0.6.4 for Windows built 15-Aug-2019/19:02:53+02:00 commit #b255dee

@dockimbel
Copy link
Member

dockimbel commented Aug 15, 2019

The loop ends and return to the console prompt.

That is not the expected behavior. break only breaks from native loops, foreach-face is a function!.

The minimal code to reproduce it is:

foreach-face layout [base] [break]

@dockimbel dockimbel added the type.bug Ticket describes an abnormal behavior, not conforming to the specs or expectation. label Aug 15, 2019
@dockimbel dockimbel self-assigned this Aug 15, 2019
dockimbel added a commit that referenced this issue Aug 15, 2019
… loop)

Note: the root cause of the crash is not fixed.
@dockimbel
Copy link
Member

dockimbel commented Aug 15, 2019

In order to properly escape from nested code, throw an exception:

l: layout [a: area red b: box 100x100 green c: field 100x100 blue]
catch [
    foreach-face l [if face/size/x = 100 [print "no more" throw 'break] print "loop"]
]

throw takes a value as argument. In this case, it doesn't matter which one as we don't intercept it for post-processing, so we can use a word to make the intention explicit.

@dockimbel dockimbel added status.built A change in codebase has been done to address the ticket. status.tested The change in code has been manually tested and verified to fix the issue. labels Aug 15, 2019
@dockimbel dockimbel added this to the 0.6.5 milestone Aug 15, 2019
@planetsizecpu
Copy link
Author

Thanks Doc, in that case I prefer to let the loop end completely, since I'm not sure if wrapping it with catch will have a higher computational cost, so when you can determine a way to break the loop, if there is one, I can always use it in my code.

Reading the break help I thought it was applicable in foreach-face:

? break
USAGE:
BREAK

DESCRIPTION:
Breaks out of a loop, while, until, repeat, foreach, etc. <---
BREAK is a native! value.

REFINEMENTS:
/ return => Forces the loop function to return a value.
value [any-type!]

So it seems rather to indicate that it applies to all types of loop, which is very useful.

@greggirwin
Copy link
Contributor

The computational cost in an interactive environment should rarely matter. Users are slow. ;^)

Applying to all types of loops is great in theory, but how do you determine what a loop is? e.g. foreach-face uses foreach internally. This is where catch throw function attributes can be applied in the future (R2 has them), but also where we have to document what's what. It's hard because a name, or part of it, doesn't imply a particular implementation. That is, just because foreach-face has foreach in its name doesn't mean it behaves like foreach. We have to think about that in standard funcs, to avoid confusion, but it's a general problem that comes from Red's flexibility, and the ability to even define control funcs, which few other langs allow.

@planetsizecpu
Copy link
Author

planetsizecpu commented Aug 17, 2019

Thanks Gregg, with Doc's explanation I clearly saw that it is my confusion
about the name of the function, in fact I did not stop to think that it could
be a function, so deeply involved was in the script code!

It occurs to me that it could be a use-case, where it is interesting to break
the internal loop in some way, I do use it in a function that is called at a high
rate in my game by several other funcs that look for face overlaps, so severy
cycle counts:

; Check if some face overlaps other face in the cave
CheckOverlaps: function [f [object!]][
	Ret: none
	foreach-face GameData/CaveFace [
		if face <> f [
			if overlap? face f [Ret: face] ;Break here would save cycles
		]
	]
	return Ret
]

@dockimbel
Copy link
Member

since I'm not sure if wrapping it with catch will have a higher computational cost,

There is zero overhead running code in a catch block, compared with do.

@planetsizecpu
Copy link
Author

planetsizecpu commented Aug 23, 2019

Thx Doc, I may test catch next weeks.

This days I'm replacing occurrences with much logical operators like if cond-a or cond-b or cond-c or cond-d [...] by the formif any [...][...] also if cond-a and cond-b and cond-c and cond-d [...] by the form if all [...][...] in seek of more speed, although I don't know if there is a difference of speed in evaluation, at least they are more clear.

@9214
Copy link
Collaborator

9214 commented Aug 23, 2019

@dockimbel I believe at this point ticket can be closed.

@dockimbel
Copy link
Member

@9214 The crash has not been fixed, just a workaround provided. Such crash should never happen, even with faulty Red code.

qtxie pushed a commit that referenced this issue Aug 28, 2019
* FIX: help error info will be printed to buffer

* FIX: issue #3333 (dump-reactions instabilities and regression since march) where `is` produces a wrong excessive reaction

* TESTS: for PR #3884 partly fixing issue #3333

* FIX: issues #3087 (Console size is not defined when script starts), #3678 (`help` is often useless when invoked from within a CLI script)

* FIX: issue #3835 (wrong rounded cornors )

* FIX: issue #3835 (capture window with title bar)

* FIX: issue #3951 (parse-macros don't get substituted anymore)

Extra fix for #3927.

* FIX: issue #3835 (combine title bar and nsview to avoid some lost area)

* FIX: issue #3545 (set the text to "", if it has no value)

* TESTS: add test for #3951.

* FIX: issue #3883 (Compiled code does not follow standard operator precedence rules)

* FIX: issue #3961 (gui-console crashed on macOS)

* TESTS: adds test for issue #3961.

* FIX: macOS: issue #3980 (`layout/parent` causes access violation)

* FIX: issue #3776 (update button image)

* FIX: issue #3776 (free the unused image list)

* FIX: issue #3795 (dpi scale issue for event/offset)

* FIX: issue #3741 (over/away's event/offset not right)

* FIX: issue #3619 (event/picked return none, if menu's id not be set)

* FIX: use more clear value

* FIX: issue #3805 ([CRASH] when reactor!/on-change* is overridden)

* FIX: issue #3768 (sort with comparator function returning number sorts incorrectly)

* FIX: issue #3771 (Inside a function macros expand only once)

* TESTS: adds runtime preprocessor tests.

This file is not included yet in the tests suite, as it crashes badly, so need preliminary investigation and fixing.

* TEST: update SORT tests.

* FIX: issue #3759 (`round/to/floor -2.4 1.0` should equal `round/to/floor -2.4 1` )

* FEAT: round/to support time!

* FIX: issue #1957 (REFLECT not defined on string! vector! binary! and hash!)

* FEAT: code refactoring in port type to remove unnecessary extra code.

* FIX: issue #2554 (Fields of error value can be set, with fatal result)

* FIX: issue #2688 (SAVE does not accept binary! as destination)

* FIX: make SAVE preserve the formatting of a molded argument value.

Example:
    save %src.red :append
    print read %src.red

* FIX: issue #2659 (Programming error and strange decision in comparing pairs)

* FIX: issue #395 (Console crash if break is evaluated on foreach-face loop)

Note: the root cause of the crash is not fixed.

* FIX: issue #3991 (Console crash if break is evaluated on foreach-face loop)

Note: the root cause of the crash is not fixed.

* FIX: issue #2657 (Inconsistency in comparing object with error)

* FIX: issue #2792 (revert #1558 e85962, and no crashing)

* FIX: issue #2658 (Strange algorithm for comparing objects)

* FIX: issue #2184 (Tests float-divide 37, 38, 47, & 48 fail on Windows.)

* FIX: workaround float loading issues for tests in #2184.

Cross-platform float loading issues documented in #3993.

* FIX: additional workaround for ARM in float tests fro #2184.

* FIX: issue #2662 (comparison of time and integer is inconsistent)

* FIX: Win: flickering when drag the `drag me` button in view-test.red

* FIX: gui-console doesn't quit properly in some cases.

* FIX: paste text not be prompted on macOS

* FIX: issue #3427 (`parse/part` meets `end` = strange behavior)

* TESTS: adds regression tests for #3427.

* FEAT: adds thread.reds in runtime.

* FIX: issue #3653 (expand-directives allows a paren argument, but this always errors out)

* FIX: issue #2556 (Wrong error message when setting event component other than type)

* FIX: renames a few function names in %thread.reds.

* FIX: rename %thread.reds to %threads.reds.

* FIX: issue #2646 (make op! allows invalid construction syntax / returned op! causes crash)

* FIX: issue #2660 (Pairs are wrongly compared for greater etc)

* FIX: issue #3104 (map: same date keys treated differently because of hidden timezone state)

* FIX: issue #3143 (trim/lines resulting in vertical block)

* FIX: issue #3187 (DOC: Red/System literal arrays may contain c-strings, contrary to what is stated in the syntax)

* TESTS: adds test for issue #3636

* FIX: issue #3356 (spec block in OBJECT! and FUNCTION! is used from head rather than from index)

* FIX: issue #3395 (max and min are not compatible with > and <)

* FIX: issue #3522 (return spec of modify action is incorrrect)

* FIX: minor comments re-indentation.

* FEAT: console support history

* FIX: insert line at history head in cli console

* FIX: rename save-cfg like internal function

* FIX: update max if insert line on history

* FIX: add cli config file to include.r

* FIX: red.exe create cli issue

* FIX: issue #3406 (`construct` result is order-dependent)

* TESTS: add tests for #3406.

* FIX: same ordering issue as #3406 for `make <obj> <proto>` pattern.

* FEAT: check uncompressed data size in decompress/zlib.

* FEAT: adds support for system/cpu/fence for setting a memory ordering barrier.

The barrier is a sequentially consistent fence type.

* FIX: removes some unused local words.

* FEAT: preliminary support for atomic operations using system/atomic/*

Note: system/cpu/fence was moved to system/atomic/fence.

* FEAT: adds support for system/atomic/cas.

* FEAT: adds get-word support for pointer argument after atomic/cas and atomic/<op>.

* TESTS: add some tests for atomic operations.

* TESTS: add test for CAS operation.

* TEST: add some failed cases in atomic tests.

* FEAT: improved the implementation of atomic operations.

The pointer argument can now be any expression returning a pointer! value.

* FEAT: adds support for system/atomic/<op>/old, returning the old value.

* FEAT: adds a FIFO MPMC queue in R/S.

* FEAT: implements system/atomic/fence for ARM.

* FEAT: the queue.reds can be compiled now.

* TESTS: add tests for R/S queue.

* TESTS: add more tests for atomic operations.

* FIX: issue #4005 ([View] regression in Windows backend).

* FIX: issue #4006 (Pen 'OFF in Draw PUSH block turns the Pen off after the PUSH block on MacOS)

* FIX: issue #3077 (`insert` key work for richtext)

* FEAT: implements system/atomic/load & store for ARM.

* FIX: properly set the Tag_CPU_arch in ELF/ARM for newer architectures.

* FEAT: implements system/atomic/cas for ARM.

* FEAT: minor code reduction.

* FEAT: implements atomic math operations for ARM.

* FIX: gui-console's context menu shortcuts on macOS

* FIX: cannot create thread on POSIX.

* FIX: `quit` console can't save config

* FIX: extra print when called from other app

* FIX: avoid to allocate buffer foreach console input

* FIX: improve `terminal initial flow`

* FIX: fixes and improves returned value handling in atomic math ops on IA-32.

* DOCS: replaces default font Trebuchet MS by Arial.

* DOCS: adds description of system/atomic/* intrinsics.

* FIX: issue #3997 (Using `#include %file.red` in context will not be compiled successfully)

* FIX: Windows: CLI console cannot be compiled.

* TESTS: enables the atomic tests and queue tests. (#4011)

* TESTS: adds atomic tests and queue tests into the testsuite.

* TESTS: adds cdecl calling convention in thread functions on non-Windows platforms.

* TESTS: atomic operations are not supported on ARMv5 yet.

* TESTS: makes the test framework happy.

* FIX: macOS: line properties cannot be set in some cases in DRAW.
@hiiamboris
Copy link
Collaborator

FYI the crash output in relevant builds (before the workaround) was:

*** Runtime Error 95: no CATCH for THROW
*** in file: common.reds                
*** at line: 260                        
***                                     
***   stack: ***-uncaught-exception     
***   stack: ***-uncaught-exception     

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status.built A change in codebase has been done to address the ticket. status.tested The change in code has been manually tested and verified to fix the issue. type.bug Ticket describes an abnormal behavior, not conforming to the specs or expectation.
Projects
None yet
Development

No branches or pull requests

5 participants