evsinev / ber-tlv

BER-TLV parser and builder
Apache License 2.0
126 stars 53 forks source link

why parse function only parsed 100 tlv? #20

Open royzhao opened 2 years ago

royzhao commented 2 years ago

for(int i=0; i<100; i++) { ParseResult result = parseWithResult(0, aBuf, offset, aLen-offset); tlvs.add(result.tlv);

if(result.offset>=aOffset+aLen) {
    break;
}

offset = result.offset;

}

i think that code is better

int curOffset; do { ParseResult result = parseWithResult(0, aBuf, offset, aLen-offset); tlvs.add(result.tlv);

  if(result.offset>=aOffset+aLen) {
      break;
  }
  curOffset = result.offset;

  offset = result.offset;

} while (curOffset<aOffset+aLen);

IonutNegru87 commented 1 year ago

Wondering about this also - is this just to avoid very long parses? That magic number of 100 does not really make sense.

eximius313 commented 1 year ago

simple:

    int offset = aOffset;
    do {
      final ParseResult result =  parseWithResult(0, aBuf, offset, aLen-offset);

      tlvs.add(result.tlv);
      offset = result.offset;
    } while(offset < aOffset+aLen);

passes all the tests

martinpaljak commented 1 year ago

it's usually about not having unbound loops on possibly unverified input.

eximius313 commented 1 year ago

fair point. But this could be left as a setting