Skip to content

Commit a4a371f

Browse files
committed
Add assertions
1 parent 91e8384 commit a4a371f

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

ext/yajl/yajl_buf.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@
3838

3939
#define YAJL_BUF_INIT_SIZE 2048
4040

41-
typedef enum {
42-
yajl_buf_ok = 0,
43-
yajl_buf_alloc_failed,
44-
yajl_buf_overflow
45-
} yajl_buf_state;
46-
4741
struct yajl_buf_t {
4842
yajl_buf_state state;
4943
unsigned int len;
@@ -59,6 +53,12 @@ static struct yajl_buf_t buf_alloc_error = {
5953

6054
#include <stdio.h>
6155

56+
yajl_buf_state yajl_buf_err(yajl_buf buf)
57+
{
58+
assert(buf);
59+
return buf->state;
60+
}
61+
6262
static
6363
yajl_buf_state yajl_buf_set_error(yajl_buf buf, yajl_buf_state err)
6464
{
@@ -161,17 +161,22 @@ void yajl_buf_clear(yajl_buf buf)
161161

162162
const unsigned char * yajl_buf_data(yajl_buf buf)
163163
{
164+
assert(buf);
165+
assert(!yajl_buf_err(buf));
164166
return buf->data;
165167
}
166168

167169
unsigned int yajl_buf_len(yajl_buf buf)
168170
{
171+
assert(buf);
172+
assert(!yajl_buf_err(buf));
169173
return buf->used;
170174
}
171175

172176
void
173177
yajl_buf_truncate(yajl_buf buf, unsigned int len)
174178
{
179+
assert(buf);
175180
assert(len <= buf->used);
176181
buf->used = len;
177182
}

ext/yajl/yajl_buf.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
* call overhead. YMMV.
4444
*/
4545

46+
typedef enum {
47+
yajl_buf_ok = 0,
48+
yajl_buf_alloc_failed,
49+
yajl_buf_overflow
50+
} yajl_buf_state;
51+
4652
/**
4753
* yajl_buf is a buffer with exponential growth. the buffer ensures that
4854
* you are always null padded.
@@ -77,4 +83,7 @@ unsigned int yajl_buf_len(yajl_buf buf);
7783
YAJL_API
7884
void yajl_buf_truncate(yajl_buf buf, unsigned int len);
7985

86+
/* get the state of buffer */
87+
yajl_buf_state yajl_buf_err(yajl_buf buf);
88+
8089
#endif

0 commit comments

Comments
 (0)