Added: NDBUF_RORB flag is a read-only aware flag. Set this to avoid

writes to read-only buffers. E.g. use
ndbuf_setflags (b, NDBUF_RORB);
Fixes: More checks for non-reallocatable buffer are added;
master
Alexander Vdolainen 6 years ago
parent baa1574746
commit 6451c66518

@ -30,6 +30,7 @@
#define NDBUF_BURN (1 << 1) /* burn buffer data before free */ #define NDBUF_BURN (1 << 1) /* burn buffer data before free */
#define NDBUF_NREA (1 << 2) /* non reallocatable buffer */ #define NDBUF_NREA (1 << 2) /* non reallocatable buffer */
#define NDBUF_UCMO (1 << 3) /* user defined memops functions */ #define NDBUF_UCMO (1 << 3) /* user defined memops functions */
#define NDBUF_RORB (1 << 4) /* read-only buffer */
struct ndbuf_memops { struct ndbuf_memops {
void *(*alloc)(size_t); void *(*alloc)(size_t);

@ -336,12 +336,25 @@ uint32_t ndbuf_write_u64(ndbuf_t *b, uint64_t uu)
return sizeof(uint64_t); return sizeof(uint64_t);
} }
static inline int __ndbuf_is_ro(ndbuf_t *b)
{
return (b->flags & NDBUF_RORB) ? 1 : 0;
}
static inline int __ndbuf_is_nrea(ndbuf_t *b)
{
return (b->flags & NDBUF_NREA) ? 1 : 0;
}
/* write raw data with the given length */ /* write raw data with the given length */
uint32_t ndbuf_write_raw(ndbuf_t *b, void *wi, uint32_t len) uint32_t ndbuf_write_raw(ndbuf_t *b, void *wi, uint32_t len)
{ {
if(!b || !b->raw) return 0; if(!b || !b->raw) return 0;
if(!wi || !len) return 0; if(!wi || !len) return 0;
if(__ndbuf_is_ro(b)) return 0; /* read only buffer */
if(b->ulength + len >= b->rlength) { if(b->ulength + len >= b->rlength) {
if(__ndbuf_is_nrea(b)) return 0; /* non reallocatable buffer */
if(__rdb_grow(b, len)) return 0; if(__rdb_grow(b, len)) return 0;
} }
@ -361,7 +374,10 @@ uint32_t ndbuf_write_raw_head(ndbuf_t *b, void *wi, uint32_t len)
if(!b || !b->raw) return 0; if(!b || !b->raw) return 0;
if(!wi || !len) return 0; if(!wi || !len) return 0;
if(__ndbuf_is_ro(b)) return 0; /* read only buffer */
if(b->ulength + len > b->rlength) { if(b->ulength + len > b->rlength) {
if(__ndbuf_is_nrea(b)) return 0; /* non reallocatable buffer */
/* allocate a new one and copy it right */ /* allocate a new one and copy it right */
if(b->rlength + len > NDBUF_MAXLENGTH) return -1; if(b->rlength + len > NDBUF_MAXLENGTH) return -1;
@ -617,6 +633,8 @@ uint32_t ndbuf_print_va(ndbuf_t *b, const char *fmt, int argc, va_list ap)
uint32_t len = 0, clen; uint32_t len = 0, clen;
int r, count; int r, count;
if(__ndbuf_is_ro(b)) return 0; /* read only buffer */
for(t = fmt, count = 0; *t != '\0'; t++, count++) { for(t = fmt, count = 0; *t != '\0'; t++, count++) {
if(count > argc && argc != -1) return 0; if(count > argc && argc != -1) return 0;
switch(*t) { switch(*t) {

Loading…
Cancel
Save