Skip to content

Commit cbaaa2c

Browse files
committed
introduced bounds checking functions and macro
1 parent 978cfb6 commit cbaaa2c

3 files changed

Lines changed: 60 additions & 6 deletions

File tree

src/rcv/adnav.c

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,44 @@
4545
#define TRKS_SNR 0x20 /* SNR valid */
4646

4747
/* get fields (little-endian) ------------------------------------------------*/
48-
#define U1(p) (*((uint8_t *)(p)))
49-
#define I1(p) (*((int8_t *)(p)))
50-
static uint16_t U2(uint8_t *p) {uint16_t u; memcpy(&u,p,2); return u;}
51-
static uint32_t U4(uint8_t *p) {uint32_t u; memcpy(&u,p,4); return u;}
52-
static float R4(uint8_t *p) {float r; memcpy(&r,p,4); return r;}
53-
static double R8(uint8_t *p) {double r; memcpy(&r,p,8); return r;}
48+
static uint8_t U1(const raw_t *raw, size_t index) {
49+
RTKBOUNDSCHECK(raw->buff, sizeof(raw->buff), index);
50+
RTKBOUNDSCHECK(raw->buff, raw->len, index);
51+
return raw->buff[index];
52+
}
53+
static int8_t I1(const raw_t *raw, size_t index) {
54+
RTKBOUNDSCHECK(raw->buff, sizeof(raw->buff), index);
55+
RTKBOUNDSCHECK(raw->buff, raw->len, index);
56+
return (int8_t)raw->buff[index];
57+
}
58+
static uint16_t U2(const raw_t *raw, size_t index) {
59+
RTKBOUNDSCHECK(raw->buff, sizeof(raw->buff), index + 1);
60+
RTKBOUNDSCHECK(raw->buff, raw->len, index + 1);
61+
uint16_t u;
62+
memcpy(&u, raw->buff + index, 2);
63+
return u;
64+
}
65+
static uint32_t U4(const raw_t *raw, size_t index) {
66+
RTKBOUNDSCHECK(raw->buff, sizeof(raw->buff), index + 3);
67+
RTKBOUNDSCHECK(raw->buff, raw->len, index + 3);
68+
uint32_t u;
69+
memcpy(&u, raw->buff + index, 4);
70+
return u;
71+
}
72+
static float R4(const raw_t *raw, size_t index) {
73+
RTKBOUNDSCHECK(raw->buff, sizeof(raw->buff), index + 3);
74+
RTKBOUNDSCHECK(raw->buff, raw->len, index + 3);
75+
float r;
76+
memcpy(&r, raw->buff + index, 4);
77+
return r;
78+
}
79+
static double R8(const raw_t *raw, size_t index) {
80+
RTKBOUNDSCHECK(raw->buff, sizeof(raw->buff), index + 7);
81+
RTKBOUNDSCHECK(raw->buff, raw->len, index + 7);
82+
double r;
83+
memcpy(&r, raw->buff + index, 8);
84+
return r;
85+
}
5486

5587
/* receiver state ------------------------------------------------------------*/
5688
typedef struct {

src/rtkcmn.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,24 @@ extern void add_fatal(fatalfunc_t *func)
390390
{
391391
fatalfunc=func;
392392
}
393+
/* check indices within bounds -------------------------------------------------
394+
* Check that the index is within the buffer, generating a fatal error if not.
395+
* See the macro RTKBOUNDSCHECK()
396+
* args : const char *func I name of caller performing check
397+
* int line I line number
398+
* const void *buff I buffer to check against
399+
* size_t size I size of the buffer in bytes
400+
* size_t index I index to check; when checking against a byte
401+
* size, this is the index of the last byte to
402+
* be accessed
403+
*-----------------------------------------------------------------------------*/
404+
extern void rtkboundscheck(const char *func, int line, const void *buff, size_t size, size_t index) {
405+
if (index >= size) {
406+
fatalerr("rtk out of bound in %s line %d for buffer %p of size %zu at index %zu\n",
407+
func, line, buff, size, index);
408+
}
409+
}
410+
393411
/* satellite system+prn/slot number to satellite number ------------------------
394412
* convert satellite system+prn/slot number to satellite number
395413
* args : int sys I satellite system (SYS_GPS,SYS_GLO,...)

src/rtklib.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,10 @@ EXPORT void traceb_impl (int level, const uint8_t *p, int n);
15601560

15611561
#endif /* TRACE */
15621562

1563+
/* correctness utility function ----------------------------------------------*/
1564+
#define RTKBOUNDSCHECK(buff, size, index) rtkboundscheck(__func__, __LINE__, buff, size, index);
1565+
EXPORT void rtkboundscheck(const char *func, int line, const void *buff, size_t size, size_t index);
1566+
15631567
/* platform dependent functions ----------------------------------------------*/
15641568
EXPORT int execcmd(const char *cmd);
15651569
EXPORT int expath (const char *path, char *paths[], int nmax);

0 commit comments

Comments
 (0)