- while (tptr < mptr) {
- TCHECK(tptr[0]);
- kptr = bgp_as_path_segment_known;
- while (kptr[0] != 0) {
- if (kptr[0] == tptr[0])
- break;
- ++kptr;
- }
- if (kptr[0] == 0)
- goto trunc;
- TCHECK(tptr[1]);
- tptr += 2 + tptr[1] * asnlen;
- }
- if (tptr == mptr)
- return 1;
+ /*
+ * Let us assume that ASs are of 2 bytes in size, and check if the AS-Path
+ * TLV is good. If not, ask the caller to try with AS encoded as 4 bytes
+ * each.
+ */
+ while (tptr < pptr + len) {
+ TCHECK(tptr[0]);
+
+ /*
+ * If we do not find a valid segment type, our guess might be wrong.
+ */
+ if (tptr[0] < BGP_AS_SEG_TYPE_MIN || tptr[0] > BGP_AS_SEG_TYPE_MAX) {
+ goto trunc;
+ }
+ TCHECK(tptr[1]);
+ tptr += 2 + tptr[1] * 2;
+ }
+
+ /*
+ * If we correctly reached end of the AS path attribute data content,
+ * then most likely ASs were indeed encoded as 2 bytes.
+ */
+ if (tptr == pptr + len) {
+ return 2;
+ }