How exactly does Java prevent itself from buffer overflow ? Is it just by throwing the "ArrayIndexOutofBounds" ? How is it different from C++ ? Why does C++ have bufferoverflow and not Java ?
-
Here is a threat that might help you answer this question: [enter link description here][1] [1]: http://stackoverflow.com/questions/479701/does-java-have-buffer-overflows – AlexUseche Sep 06 '13 at 14:48
3 Answers
The point to remember is that "Buffer Overflow" is the name for a trick used to hack systems as well as the name of a common programming error.
Java protects the programmer from the common programming error called "Buffer Overflow" by consistently throwing ArrayIndexOutofBounds
exceptions whenever an attempt to access an array outside its bounds is made.
Java defends the system from the common hacking technique called "Buffer Overflow" by making sure that attempts to access outside the bounds of allocated memory does not cause memory corruption.
C and C++ are vulnerable to the hacking technique called "Buffer Overflow" because they allow access of unallocated memory and merely state that the effect of this activity is undefined
.
See Java Language Specification - Chapter 11. Exceptions for:
When a program violates the semantic constraints of the Java programming language, the Java Virtual Machine signals this error to the program as an exception.
An example of such a violation is an attempt to index outside the bounds of an array. ...
My emphasis.

- 1
- 1

- 64,482
- 16
- 119
- 213
-
could you show some source of what you are saying ? How is java making sure that the access outside the bounds does not cause memory corruption ? what method is implemented ? – Marc Sep 06 '13 at 14:46
-
@Marc - It is not done by a method, in Java it is intrinsic in the language because every array access is automatically preceded by a bounds check. – OldCurmudgeon Sep 06 '13 at 14:47
-
Well with method I didn't meant method in the sense of a function but rather what technique of buffer overflow protection is implemented (my bad). It looks like its bound checking but I would love to have more details... – Marc Sep 06 '13 at 14:55
The two languages are underpinned by different philosophies:
To quote Bjarne Stroustrup, "C++ is lean and mean. The underlying principle is that you don't pay for what you don't use." This means that if you don't want bounds checking (for example, for performance reasons), you shouldn't have to pay for it.
One of early design goals for Java was to enable the secure execution of untrusted code. This necessitates bounds checking on array access, as out-of-bounds access is a potential attack vector.

- 486,780
- 108
- 951
- 1,012
A buffer overflow happens when you attempt write data past the end of a fixed-size buffer.
In a language like Java that does bounds checking, attempting to write past the end of the buffer will throw an exception. Depending on how the program handles the exception, this can mean that it recovers cleanly (rejecting the data that overflowed the buffer), or becomes unstable (if the programmer didn't plan for the exception).
In a language like C that expects the programmer to know how long his/her buffers are, writing past the end of the buffer will silently corrupt data used elsewhere in the program. Worse, in systems that do not have a hard separation between code and data (eg, Windows-95), the buffer overflow may corrupt executable code. If a person has studied the program, s/he can carefully construct a buffer overflow that lets him/her replace the original program code with something else -- perhaps something that installs a virus on the system that didn't protect against the buffer overflow.
So, while Java (and other manage languages) prevent the types of buffer overflows that corrupt data (and modern OS's prevent the type that allow malicious code), they do not prevent the types of (attempted) buffer overflows that cause unstable programs. That's up to a programmer properly dealing with the out-of-bounds exception.

- 174
- 3