Reputation: 2877
For a small chat server thingie I'm making, I decided to use D; finding myself with a very neat example from listener.d to get a kick start I decided to pretty much take the example over! However, I'm stuck on a bug I can't truly wrap my finger around. Most likely it's my own fault and I'm doing something wrong, but considering I took the code pretty much from the example I am more inclined to believe the example is broken.
I'll explain what happens:
This is the main process and it's loop: https://github.com/JGBrands/BlaatServer/blob/master/source/bserver.d
This is the server class, the code where it deletes the socket is at the complete bottom in the function void destroySocket(int index);
https://github.com/JGBrands/BlaatServer/blob/master/source/server.d
Actually let me copy paste that. :-)
void destroySocket(int index) {
this.reads[index].close(); /* release resources. */
/* Remove the socket now. We don't want this around! It'll crash us! */
if (index != this.reads.length -1)
this.reads[index] = this.reads[this.reads.length -1];
this.reads = this.reads[0 .. this.reads.length -1];
writeln("Total connections: " ~ to!string(this.reads.length));
}
The code is primarily taken over from the listener.d example like I said, the error I get is this:
core.exception.RangeError@server(61): Range violation
----------------
----------------
I'm lead to believe the function is deleting something it shouldn't, for those interested, this is line 61 in server.d:
if (this.sset.isSet(this.reads[i])) {
Hopefully you guys can help me make more sense about this, am I missing something really obvious here?
Upvotes: 3
Views: 197
Reputation: 6774
As ratchet points out:
if (index != this.reads.length -1)
This does not verify that index is within range. It only validates that index is not the last element. This is fine as long as index has already been verified to be within range.
void destroySocket(int index)
in {
assert(index > -1);
assert(index < this.reads.length);
} body {
{
Upvotes: 0