-
Notifications
You must be signed in to change notification settings - Fork 26
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
runtime should kill when assigning to an unallocated pointer (was: returning pointer to a local variable) #82
Comments
I don't understand the issue, except that s^ := a; tries to assign a value to an unallocated pointer and this is a runtime error. Am I missing something? |
no, runtime error doesn't happen there. |
what i thought is - I would like to make a warning at the line
because s is a pointer to variable allocated on stack. |
omg, you are right. |
diegosardina's comment is correct. |
so Oberon pointer should only point to the area allocated on heap with NEW, and that kind of error I wanted to illustrate is not possible in Oberon. on the other hand, should we make a warning when there is an assignment to an unallocated pointer? |
The real issue is that the runtime doesn't halt the program in this statement:
In Oberon (or any other strong typing language) segmentation faults must never exist. Type safety implies memory safety. |
you mean the runtime should've been killed the program at that point. |
Of course. |
aside from that, what do you think about compile time warning? |
For what I remember (I don't use an Oberon or Modula-2 compiler since long) the compiler would raise a warning that pointer variable s may not be initialised. However because nested procedures may access intermediate globals, this warning is likely to be false sometimes. |
I don't think it's feasible to reliably generate a compile time warning. Consider:
|
thank you all, I will try several compilers, and will think of solutions. |
@svorkoetter I mean warning when parsing such an assignment. Now when I realized the problem is different. |
Unfortunately, the only 100% reliable solution is a run-time check. At least in Oberon that is possible, since The big problem with unsafe languages is not that they give a seg fault when assigning through an uninitialized pointer, but that they might not give a seg fault, but just quietly clobber some other data structure in your program. |
Same problem if parsing an assignment:
|
voc already has some runtime checks and kills the program in some cases. |
yes, thank you for illustrations. |
This is even more troublesome for a parser:
In general this kind of warning to be useful requires global analysis. |
I tested the above code with Oxford Oberon-2, indeed it raises a warning that is false in this case (it behaves like some old Modula-2 compilers). It's my opinion that a warning that may be false sometimes is better than nothing (but fortunately this is the only case, allocating pointers globally via nested procedures is a bad practice. In Oberon-07 that code wouldn't compile). Like some old Modula-2 compilers, the compiler should raise a warning if a pointer is not passed to NEW(), not assigned by a function procedure or not passed via a VAR parameter. This doesn't guarantee that the pointer will be valid after, but... with that warning you wouldn't have opened this issue ;-) |
A warning that is sometimes false is useless, as the programmer becomes conditioned to ignoring it, and in this case, there's no way to get rid of the nuisance warning (compared to a warning that a variable is used before it is assigned a value in C; one can always just assign 0 to it, which is a cheap operation compared to allocating memory). |
i think we have to add a warning (because as i understand it is not required to throw an error by oberon report) for this case, when returning a pointer to a local variable:
The text was updated successfully, but these errors were encountered: