lib: add STREAM_FORWARD_[GET|ENDP]

Safe stream macros for adjusting buffer pointers

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
This commit is contained in:
Quentin Young 2020-05-13 19:10:39 -04:00 committed by Anuradha Karuppiah
parent 93b35ca9f8
commit 4adddf0a29
2 changed files with 38 additions and 0 deletions

View File

@ -256,6 +256,18 @@ void stream_forward_getp(struct stream *s, size_t size)
s->getp += size;
}
bool stream_forward_getp2(struct stream *s, size_t size)
{
STREAM_VERIFY_SANE(s);
if (!GETP_VALID(s, s->getp + size))
return false;
s->getp += size;
return true;
}
void stream_forward_endp(struct stream *s, size_t size)
{
STREAM_VERIFY_SANE(s);
@ -268,6 +280,18 @@ void stream_forward_endp(struct stream *s, size_t size)
s->endp += size;
}
bool stream_forward_endp2(struct stream *s, size_t size)
{
STREAM_VERIFY_SANE(s);
if (!ENDP_VALID(s, s->endp + size))
return false;
s->endp += size;
return true;
}
/* Copy from stream to destination. */
bool stream_get2(void *dst, struct stream *s, size_t size)
{

View File

@ -173,7 +173,9 @@ extern struct stream *stream_dupcat(const struct stream *s1,
extern void stream_set_getp(struct stream *, size_t);
extern void stream_set_endp(struct stream *, size_t);
extern void stream_forward_getp(struct stream *, size_t);
extern bool stream_forward_getp2(struct stream *, size_t);
extern void stream_forward_endp(struct stream *, size_t);
extern bool stream_forward_endp2(struct stream *, size_t);
/* steam_put: NULL source zeroes out size_t bytes of stream */
extern void stream_put(struct stream *, const void *, size_t);
@ -453,6 +455,18 @@ static inline const uint8_t *ptr_get_be32(const uint8_t *ptr, uint32_t *out)
goto stream_failure; \
} while (0)
#define STREAM_FORWARD_GETP(STR, SIZE) \
do { \
if (!stream_forward_getp2((STR), (SIZE))) \
goto stream_failure; \
} while (0)
#define STREAM_FORWARD_ENDP(STR, SIZE) \
do { \
if (!stream_forward_endp2((STR), (SIZE))) \
goto stream_failure; \
} while (0)
#ifdef __cplusplus
}
#endif