diff options
author | Chip Salzenberg <chip@pobox.com> | 2012-04-03 18:00:29 -0700 |
---|---|---|
committer | Chip Salzenberg <chip@pobox.com> | 2013-09-27 17:32:06 -0700 |
commit | 9c259c07aa53381df5819ef61627342c932d626f (patch) | |
tree | c13313ce1ae36aeaea0a441c8335bee67cc689a5 /src/memory.c | |
parent | e4d6a9f6f4f90aa7bb1b5e09d146ac8d2cb3cd1d (diff) | |
download | jansson-9c259c07aa53381df5819ef61627342c932d626f.zip jansson-9c259c07aa53381df5819ef61627342c932d626f.tar.gz jansson-9c259c07aa53381df5819ef61627342c932d626f.tar.bz2 |
Support \u0000 - add size_t string lengths to API, load and dump \u000, etc.
Also:
Steal strings during parsing for half the mallocs!
Change all input-caused assertions to errors. No crashes please, we're programmers.
Diffstat (limited to 'src/memory.c')
-rw-r--r-- | src/memory.c | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/src/memory.c b/src/memory.c index eb6cec5..e6da96e 100644 --- a/src/memory.c +++ b/src/memory.c @@ -12,6 +12,10 @@ #include "jansson.h" #include "jansson_private.h" +/* C89 allows these to be macros */ +#undef malloc +#undef free + /* memory function pointers */ static json_malloc_t do_malloc = malloc; static json_free_t do_free = free; @@ -34,18 +38,19 @@ void jsonp_free(void *ptr) char *jsonp_strdup(const char *str) { - char *new_str; - size_t len; + return jsonp_strndup(str, strlen(str)); +} - len = strlen(str); - if(len == (size_t)-1) - return NULL; +char *jsonp_strndup(const char *str, size_t len) +{ + char *new_str; new_str = jsonp_malloc(len + 1); if(!new_str) return NULL; - memcpy(new_str, str, len + 1); + memcpy(new_str, str, len); + new_str[len] = '\0'; return new_str; } |