capnproto-java icon indicating copy to clipboard operation
capnproto-java copied to clipboard

StructList size should be checked and throw an exception

Open a-zen opened this issue 10 years ago • 1 comments

When using a StructList the size is not checked when adding new entries. For example:

    StructList.Builder<PhoneNumber.Builder> personPhones
            = person.initPhones(0);
    personPhones.get(0).setNumber("08031/12345");
    personPhones.get(0).setType(PhoneType.WORK);

    personPhones.get(1).setNumber("0160/9876543");
    personPhones.get(1).setType(PhoneType.MOBILE);

Will give no error or exception but the data is not written.

Or worste if you have a bit of space:

    StructList.Builder<PhoneNumber.Builder> personPhones
            = person.initPhones(1);
    personPhones.get(0).setNumber("08031/12345");
    personPhones.get(0).setType(PhoneType.WORK);

    personPhones.get(1).setNumber("0160/9876543");
    personPhones.get(1).setType(PhoneType.MOBILE);

You get the following:

  work phone: 031/12

So your data is destroyed.

In both cases I would expect an exception.

a-zen avatar Mar 10 '15 09:03 a-zen

There's a related issue: indexes aren't checked on reading either. So you can get a C++ style out of bounds read when reading lists! This is not intuitive.

mikehearn avatar Apr 25 '24 13:04 mikehearn