Array bounds check

Monkey Forums/Monkey Bug Reports/Array bounds check

ziggy(Posted 2013) [#1]
I've spend a whole evening trying to find why my App crashed on Android and not in HTML5 and randomly on GLFW with a memory access violation (and not useful details on the crash in debug mode).

After a long time, I've found out it was an array out of bounds. As it seems this kind of bug has different effects on different targets, I think Monkey does really need a built-in internal array bounds index check in debug mode, so a proper error is generated.


marksibly(Posted 2013) [#2]
If I run this:

Import mojo

Class MyApp Extends App

	Method OnCreate()
		Local t:=New Int[5]
		For Local i:=0 Until 100
			t[i]=i
		Next
	End
	
	Method OnUpdate()
	End
	
	Method OnRender()
	End

End

Function Main()
	New MyApp
End


In debug mode, on glfw, android, xna, html5, flash (Mac is busy) I get what I consider to be an appropriate error message - complete with monkey line/file. eg for android:


I/[Monkey](24231): Monkey Runtime Error : length=5; index=5
I/[Monkey](24231): C:/Users/Mark Sibly/Desktop/test.monkey<9>
I/[Monkey](24231): C:/Dropbox/monkey/modules/mojo/app.monkey<49>
E/AndroidRuntime(24231): FATAL EXCEPTION: GLThread 4531
E/AndroidRuntime(24231): java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
E/AndroidRuntime(24231): at com.monkey.c_MyApp.p_OnCreate(MonkeyGame.java:2650)
E/AndroidRuntime(24231): at com.monkey.c_GameDelegate.StartGame(MonkeyGame.java:2693)
E/AndroidRuntime(24231): at com.monkey.BBGame.StartGame(MonkeyGame.java:512)
E/AndroidRuntime(24231): at com.monkey.BBAndroidGame.onDrawFrame(MonkeyGame.java:1259)
E/AndroidRuntime(24231): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516)
E/AndroidRuntime(24231): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)



For glfw, the debugger opens and I get taken to correct line/file, along with an 'array index out of bounds' dialog.

Not sure what else I can do! Adding manual array checks to java/cs wont provide any more info than this, at the cost of reducing app execution speed in debug mode.


ziggy(Posted 2013) [#3]
Ok, the problem is exactly when accesing a string as an integer array.
Sample:
Function Main()
	Local a:= "Hello"
	Print a[6]
End


This using mojo:
Import mojo

Class MyApp Extends App

	Method OnCreate()
		Local a:= "Hello"
		Print a[6]
	End
	
	Method OnUpdate()
	End
	
	Method OnRender()
	End

End

Function Main()
	New MyApp
End


If you do it like this, there's no proper error message. GLFW just access random memory. Android crashes, and Javascript returns NaN but does not crash.


marksibly(Posted 2013) [#4]
Ok, will fix on cpp/js/as, but java/cs seem to be working - app halts/crashes with...


I/[Monkey](32372): Monkey Runtime Error : length=5; index=6
I/[Monkey](32372): C:/Users/Mark Sibly/Desktop/test.monkey<4>
E/AndroidRuntime(32372): FATAL EXCEPTION: main
E/AndroidRuntime(32372): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.monkey/com.monkey.MonkeyGame}: java.lang.StringIndexOutOfBoundsException: length=5; index=6
...more...



...on Android, something similar on xna.