FreeBASIC  0.91.0
array_boundchk.c
Go to the documentation of this file.
1 /* Array bound checking functions */
2 
3 #include "fb.h"
4 
5 static void *hThrowError( int linenum, const char *fname )
6 {
7  /* call user handler if any defined */
8  return (void *)fb_ErrorThrowEx( FB_RTERROR_OUTOFBOUNDS, linenum, fname, NULL, NULL );
9 }
10 
12  (
13  ssize_t idx,
14  ssize_t lbound,
15  ssize_t ubound,
16  int linenum,
17  const char *fname
18  )
19 {
20  if( (idx < lbound) || (idx > ubound) )
21  return hThrowError( linenum, fname );
22  else
23  return NULL;
24 }
25 
27  (
28  size_t idx,
29  size_t ubound,
30  int linenum,
31  const char *fname
32  )
33 {
34  /* Assuming lbound is 0, we know ubound must be >= 0, and we can treat
35  index as unsigned too, possibly letting it overflow to a very big
36  value (if it was negative), reducing the bound check to a single
37  unsigned comparison. */
38  if( idx > ubound )
39  return hThrowError( linenum, fname );
40  else
41  return NULL;
42 }