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

Memory checks for arrays #5

Open
Sh3b0 opened this issue Mar 6, 2022 · 2 comments
Open

Memory checks for arrays #5

Sh3b0 opened this issue Mar 6, 2022 · 2 comments
Labels
invalid This doesn't seem right

Comments

@Sh3b0
Copy link
Owner

Sh3b0 commented Mar 6, 2022

Example code

routine main() is
	var x : array[5] integer;
	x[-1] := -1;
	x[0] := 0;
	x[6] := 6;
	println x[-1];
	println x[0];
	println x[6];
	return;
end

Expected behavor

Runtime errors as we access invalid indexes (btw, C+ arrays are 1-indexed)

Actual behavior

Prints -1, 0, 6
Although seems cool, it shouldn't be like this.

@Sh3b0 Sh3b0 added enhancement New feature or request invalid This doesn't seem right and removed enhancement New feature or request labels Mar 6, 2022
@TonyDecvA180XN
Copy link
Collaborator

Array offest work on a very simple formula: cell = start + index * stride; where stride is a size of an array element. If the index of an element exceeds the desired range - segmentation fault happens. Unfortuantely, tracking segfaults is not a trivial task as it might seem, C++ does not have it either. (More like it is C+ programmer responsibility). Index range validation would require some exception mechanism or any other error recovery system, which is huge task.
Btw, errors can tracked on CRT level, however we would have to introduce exceptions (and thus, classes in C+) or platform-dependent code for POSIX or Win32 substitution which is not pleasant.

@Sh3b0
Copy link
Owner Author

Sh3b0 commented Mar 16, 2022

Well, I was thinking of just inserting an internal conditional branch on each array access (e.g., in IRGenerator::visit(ast::Identifier *id)) to compare the runtime values of array size and index being accessed, and potenitially exit with smth like "Array index out of bounds".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants