Buffer Overflow Clarification

6 views
Skip to first unread message

Justin Leung

unread,
Dec 14, 2011, 5:29:17 PM12/14/11
to CPSC410-2011
From the lecture slides the buffer overflow code is as follows:

class Person {
private char[] name = new char[255];

public void setName(char[] value) {
for(int i=0; i < value.length; i++) {
if(name.length >= i) {
throw new ArrayIndexOutOfBounds(..);
}
name[i] = value[i];
}
}
}


This doesn't seem to make sense to me... name.length = 255, when i = 0
or i < 255, the code will throw the ArrayIndexOutOfBounds() exception.
Shouldn't the IF check be the other way around?

Eric Wohlstadter

unread,
Dec 14, 2011, 6:35:12 PM12/14/11
to cpsc41...@googlegroups.com
Yes, definitely it is wrong in the slide then.

Eric
Reply all
Reply to author
Forward
0 new messages