intval = 0;
+ // cursor must be valid
+ if ( cursor < beg || cursor > end )
+ return false;
+
// check if we can read int data
- if ( cursor + size > end )
+ if ( size > end - cursor )
return false;
for ( unsigned i = 0; i < size; i++ )
type = 0;
- if ( cursor + 1 > end )
+ // cursor must be valid
+ if ( cursor < beg || cursor + 1 > end )
return false;
b = *cursor++;
length = 0;
- if ( cursor + 1 > end )
+ // cursor must be valid
+ if ( cursor < beg || cursor + 1 > end )
return false;
b = *cursor++;
{
const uint8_t* start = c;
- if ( c < beg || c > end )
- return false;
-
cursor = c;
if ( !read_type(e.type) )
// set BER data pointer
e.data = cursor;
- // jump BER data
- cursor += e.length;
-
- // cursor must be > start
- if ( cursor <= start )
+ // integer underflow check
+ if ( start > cursor )
return false;
+ // calculate BER header length
+ e.header_length = cursor - start;
+
// calculate total BER length
- e.total_length = cursor - start;
+ e.total_length = e.header_length + e.length;
+
+ // integer overflow check
+ if ( e.total_length < e.header_length )
+ return false;
return true;
}
if ( e.type != BerType::INTEGER )
return false;
- if ( e.data < beg || e.data > end )
- return false;
-
// set cursor to int data
cursor = e.data;
if ( !read(c, e) )
return false;
- // save end of element position
- c = cursor;
-
if ( !convert(e, intval) )
return false;
+ // save end of element position
+ c = cursor;
+
return true;
}
if ( e.type != type )
return false;
+ // integer underflow check
+ if ( cursor > end )
+ return false;
+
+ // check if we can jump BER data
+ if ( e.length > end - cursor )
+ return false;
+
+ // jump BER data
+ cursor += e.length;
+
+ // save end of element position
c = cursor;
return true;