summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGreg Olszewski <gno@gropeep.org>2010-10-17 14:33:50 -0700
committerLloyd Hilaiel <lloyd@hilaiel.com>2011-04-15 10:12:41 -0600
commit0d9e3589bc8d59ce963937587f634c8c9cc61643 (patch)
treeede33e7007e80650b794f2ccffffc57ade4b51dc
parent4adbfa2c4e5068ff255a342c8a38b35ad864235a (diff)
downloadyajl-0d9e3589bc8d59ce963937587f634c8c9cc61643.tar.gz
Don't generate numbers for keys. closes #13
Check malloc return values where convenient. Check for underflow during generation. Currently, starting a generation with map close or array close segfaults. Check that the indentation configuration provided produces valid json when used. (Merged from gno, closes #21.) Signed-off-by: Lloyd Hilaiel <lloyd@hilaiel.com>
-rw-r--r--src/yajl_gen.c36
1 files changed, 30 insertions, 6 deletions
diff --git a/src/yajl_gen.c b/src/yajl_gen.c
index 548d948..a7b8c5a 100644
--- a/src/yajl_gen.c
+++ b/src/yajl_gen.c
@@ -90,13 +90,32 @@ yajl_gen_alloc2(const yajl_print_t callback,
}
g = (yajl_gen) YA_MALLOC(afs, sizeof(struct yajl_gen_t));
+ if (!g) return NULL;
+
memset((void *) g, 0, sizeof(struct yajl_gen_t));
/* copy in pointers to allocation routines */
memcpy((void *) &(g->alloc), (void *) afs, sizeof(yajl_alloc_funcs));
if (config) {
+ char *indent = config->indentString;
g->pretty = config->beautify;
- g->indentString = config->indentString ? config->indentString : " ";
+ g->indentString = config->indentString;
+ if (indent) {
+ for (indent; *indent; indent++) {
+ if (*indent != '\n'
+ && *indent != '\v'
+ && *indent != '\f'
+ && *indent != '\t'
+ && *indent != '\r'
+ && *indent != ' ') {
+ g->indentString = NULL;
+ break;
+ }
+ }
+ }
+ if (!g->indentString) {
+ g->indentString = " ";
+ }
}
if (callback) {
@@ -138,9 +157,10 @@ yajl_gen_free(yajl_gen g)
}
#define ENSURE_NOT_KEY \
- if (g->state[g->depth] == yajl_gen_map_key) { \
- return yajl_gen_keys_must_be_strings; \
- } \
+ if (g->state[g->depth] == yajl_gen_map_key || \
+ g->state[g->depth] == yajl_gen_map_start) { \
+ return yajl_gen_keys_must_be_strings; \
+ } \
/* check that we're not complete, or in error state. in a valid state
* to be generating */
@@ -154,6 +174,9 @@ yajl_gen_free(yajl_gen g)
#define INCREMENT_DEPTH \
if (++(g->depth) >= YAJL_MAX_DEPTH) return yajl_max_depth_exceeded;
+#define DECREMENT_DEPTH \
+ if (--(g->depth) >= YAJL_MAX_DEPTH) return yajl_gen_error;
+
#define APPENDED_ATOM \
switch (g->state[g->depth]) { \
case yajl_gen_start: \
@@ -271,7 +294,8 @@ yajl_gen_status
yajl_gen_map_close(yajl_gen g)
{
ENSURE_VALID_STATE;
- (g->depth)--;
+ DECREMENT_DEPTH;
+
if (g->pretty) g->print(g->ctx, "\n", 1);
APPENDED_ATOM;
INSERT_WHITESPACE;
@@ -296,8 +320,8 @@ yajl_gen_status
yajl_gen_array_close(yajl_gen g)
{
ENSURE_VALID_STATE;
+ DECREMENT_DEPTH;
if (g->pretty) g->print(g->ctx, "\n", 1);
- (g->depth)--;
APPENDED_ATOM;
INSERT_WHITESPACE;
g->print(g->ctx, "]", 1);