summaryrefslogtreecommitdiffstats
path: root/jv.c
diff options
context:
space:
mode:
authorNicolas Williams <nico@cryptonector.com>2013-12-24 12:24:14 -0600
committerNicolas Williams <nico@cryptonector.com>2014-07-07 19:33:18 -0500
commitb963ebaf1d0dc1ef5558a7dc4404e35cb4085fae (patch)
treec4e1e875e8818897747419435298fd8661b920b4 /jv.c
parenteb4537241406f3f333c0da7a2975e040c8fb807c (diff)
jv_invalid() shouldn't allocate
jv_invalid() should behave like jv_invalid_with_msg(jv_null()), and neither should allocate memory, because neither ought to need to.
Diffstat (limited to 'jv.c')
-rw-r--r--jv.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/jv.c b/jv.c
index 2da8ecb0..d7f967de 100644
--- a/jv.c
+++ b/jv.c
@@ -61,6 +61,7 @@ const char* jv_kind_name(jv_kind k) {
}
static const jv JV_NULL = {JV_KIND_NULL, 0, 0, 0, {0}};
+static const jv JV_INVALID = {JV_KIND_INVALID, 0, 0, 0, {0}};
static const jv JV_FALSE = {JV_KIND_FALSE, 0, 0, 0, {0}};
static const jv JV_TRUE = {JV_KIND_TRUE, 0, 0, 0, {0}};
@@ -90,6 +91,8 @@ typedef struct {
} jvp_invalid;
jv jv_invalid_with_msg(jv err) {
+ if (jv_get_kind(err) == JV_KIND_NULL)
+ return JV_INVALID;
jvp_invalid* i = jv_mem_alloc(sizeof(jvp_invalid));
i->refcnt = JV_REFCNT_INIT;
i->errmsg = err;
@@ -99,12 +102,16 @@ jv jv_invalid_with_msg(jv err) {
}
jv jv_invalid() {
- return jv_invalid_with_msg(jv_null());
+ return JV_INVALID;
}
jv jv_invalid_get_msg(jv inv) {
assert(jv_get_kind(inv) == JV_KIND_INVALID);
- jv x = jv_copy(((jvp_invalid*)inv.u.ptr)->errmsg);
+ jv x;
+ if (inv.u.ptr == 0)
+ x = jv_null();
+ else
+ x = jv_copy(((jvp_invalid*)inv.u.ptr)->errmsg);
jv_free(inv);
return x;
}
@@ -118,7 +125,7 @@ int jv_invalid_has_msg(jv inv) {
static void jvp_invalid_free(jv x) {
assert(jv_get_kind(x) == JV_KIND_INVALID);
- if (jvp_refcnt_dec(x.u.ptr)) {
+ if (x.u.ptr != 0 && jvp_refcnt_dec(x.u.ptr)) {
jv_free(((jvp_invalid*)x.u.ptr)->errmsg);
jv_mem_free(x.u.ptr);
}
@@ -1217,7 +1224,7 @@ jv jv_copy(jv j) {
if (jv_get_kind(j) == JV_KIND_ARRAY ||
jv_get_kind(j) == JV_KIND_STRING ||
jv_get_kind(j) == JV_KIND_OBJECT ||
- jv_get_kind(j) == JV_KIND_INVALID) {
+ (jv_get_kind(j) == JV_KIND_INVALID && j.u.ptr != 0)) {
jvp_refcnt_inc(j.u.ptr);
}
return j;