diff options
| author | John MacFarlane <jgm@berkeley.edu> | 2015-06-07 13:24:26 -0700 | 
|---|---|---|
| committer | John MacFarlane <jgm@berkeley.edu> | 2015-06-07 13:24:26 -0700 | 
| commit | 802270f434a72935ba75c725b3cadcae4f478735 (patch) | |
| tree | b045b8831ac4c691ce90c41efa8e04b5330918da /src/chunk.h | |
| parent | 3adc586d9d7539e4d33f737110ffd4e236379099 (diff) | |
| parent | fdfa1e4bedf95691389efb9991ac8a6a4599c158 (diff) | |
Merge pull request #56 from nwellnhof/bufsize_t
Safer handling of string buffer sizes and indices
Diffstat (limited to 'src/chunk.h')
| -rw-r--r-- | src/chunk.h | 15 | 
1 files changed, 8 insertions, 7 deletions
diff --git a/src/chunk.h b/src/chunk.h index a246a9d..f23a02d 100644 --- a/src/chunk.h +++ b/src/chunk.h @@ -11,8 +11,8 @@  typedef struct {  	unsigned char *data; -	int len; -	int alloc;  // also implies a NULL-terminated string +	bufsize_t len; +	bufsize_t alloc;  // also implies a NULL-terminated string  } cmark_chunk;  static inline void cmark_chunk_free(cmark_chunk *c) @@ -51,10 +51,10 @@ static inline void cmark_chunk_trim(cmark_chunk *c)  	cmark_chunk_rtrim(c);  } -static inline int cmark_chunk_strchr(cmark_chunk *ch, int c, int offset) +static inline bufsize_t cmark_chunk_strchr(cmark_chunk *ch, int c, bufsize_t offset)  {  	const unsigned char *p = (unsigned char *)memchr(ch->data + offset, c, ch->len - offset); -	return p ? (int)(p - ch->data) : ch->len; +	return p ? (bufsize_t)(p - ch->data) : ch->len;  }  static inline const char *cmark_chunk_to_cstr(cmark_chunk *c) @@ -87,7 +87,7 @@ static inline void cmark_chunk_set_cstr(cmark_chunk *c, const char *str)  		c->data  = NULL;  		c->alloc = 0;  	} else { -		c->len   = strlen(str); +		c->len   = cmark_strbuf_safe_strlen(str);  		c->data  = (unsigned char *)malloc(c->len + 1);  		c->alloc = 1;  		memcpy(c->data, str, c->len + 1); @@ -96,11 +96,12 @@ static inline void cmark_chunk_set_cstr(cmark_chunk *c, const char *str)  static inline cmark_chunk cmark_chunk_literal(const char *data)  { -	cmark_chunk c = {(unsigned char *)data, data ? strlen(data) : 0, 0}; +	bufsize_t len = data ? cmark_strbuf_safe_strlen(data) : 0; +	cmark_chunk c = {(unsigned char *)data, len, 0};  	return c;  } -static inline cmark_chunk cmark_chunk_dup(const cmark_chunk *ch, int pos, int len) +static inline cmark_chunk cmark_chunk_dup(const cmark_chunk *ch, bufsize_t pos, bufsize_t len)  {  	cmark_chunk c = {ch->data + pos, len, 0};  	return c;  | 
