[e-lang] Joe-E ByteArray API for working with streams
Tyler Close
tyler.close at gmail.com
Sun Sep 23 10:19:56 EDT 2007
Hi David,
On 9/22/07, David Wagner <daw at cs.berkeley.edu> wrote:
> Tyler writes:
> >I'd like to add a couple new members to the ByteArray class to make it
> >easier to work with streams. In particular, the method [open]:
> >and the inner class [Generator]:
>
> Looks good to me.
>
> One minor nitpick on the inner class Generator:
>
> > public void
> > write(final byte[] b, final int off, final int len) {
> > if (size + len > buffer.length) {
> > System.arraycopy(buffer,0, buffer = new byte[size+len],0, size);
> > }
> > System.arraycopy(b, off, buffer, size, len);
> > size += len;
> > }
>
> If someone with a reference to a Generator g calls
>
> g.write(.., .., -1);
>
> then this destroys the Generator's internal invariants and renders
> it in an inconsistent state. In particular, the body of the method
> will create a new byte array that is a bit shorter and whose elements
> are initialized to zero, set g.buffer to that new byte array, and
> then throw IndexOutOfBoundsException.
I don't think it would happen that way. The size is always less than,
or equal to, buffer.length, which is always at least 1. So (size - 1)
is also always less than buffer.length, so the if test is false, so
there's no new array allocated or assigned. The System.arraycopy
method will then throw IndexOutOfBoundsException, because len is
negative. Execution then exits the function, without any of the
internal state variables being modified.
Good thing to look at though.
I think the only question remaining is whether or not it makes sense
to add an explicit test for this case at the start of the method, so
as not to use up reviewer time. Since it's a one line test, I suspect
the extra code is worth the added clarity.
> I don't know whether this matters much. It seems like a minor
> blemish.
Actually, I would have credited you with finding a full fledged bug,
had it been so.
--Tyler
--
The web-calculus is the union of REST and capability-based security:
http://www.waterken.com/dev/Web/
Name your trusted sites to distinguish them from phishing sites.
https://addons.mozilla.org/firefox/957/
More information about the e-lang
mailing list