From d71b83bd7c318c09e476ce6b57c57f2cfd5c6622 Mon Sep 17 00:00:00 2001 From: Lauri Jakku Date: Thu, 19 Dec 2019 21:12:04 +0200 Subject: [PATCH] Revert "testcases passed (printbuffer default size 32/128)" This reverts commit df95988748069cfa4ba8d166f65664aa74ac4df0. --- json_object.c | 91 ++++++++++++------------------------------ printbuf.c | 84 ++++++++------------------------------ test_printbuf.expected | 33 --------------- 3 files changed, 42 insertions(+), 166 deletions(-) delete mode 100644 test_printbuf.expected diff --git a/json_object.c b/json_object.c index aa72a22..74d5266 100644 --- a/json_object.c +++ b/json_object.c @@ -149,9 +149,9 @@ static int json_escape_str(struct printbuf *pb, const char *str, int len, int fl str + start_offset, pos - start_offset); snprintf(sbuf, sizeof(sbuf), - "\\u00%c%c", - json_hex_chars[c >> 4], - json_hex_chars[c & 0xf]); + "\\u00%c%c", + json_hex_chars[c >> 4], + json_hex_chars[c & 0xf]); printbuf_memappend_fast(pb, sbuf, (int) sizeof(sbuf) - 1); start_offset = ++pos; } else @@ -220,16 +220,14 @@ static void json_object_generic_delete(struct json_object* jso) lh_table_delete(json_object_table, jso); #endif /* REFCOUNT_DEBUG */ printbuf_free(jso->_pb); - jso->_pb = NULL; free(jso); - jso= NULL; } static struct json_object* json_object_new(enum json_type o_type) { struct json_object *jso; - jso = (struct json_object*)calloc(1, sizeof(struct json_object)); + jso = (struct json_object*)calloc(sizeof(struct json_object), 1); if (!jso) return NULL; jso->o_type = o_type; @@ -327,30 +325,24 @@ const char* json_object_to_json_string_length(struct json_object *jso, int flags const char *r = NULL; size_t s = 0; - /* Failure case set first */ - s = 4; - r = "null"; - - if (jso) + if (!jso) + { + s = 4; + r = "null"; + } + else if ((jso->_pb) || (jso->_pb = printbuf_new())) { - if ( jso->_pb == NULL ) - jso->_pb = printbuf_new(); + printbuf_reset(jso->_pb); - if ( jso->_pb != NULL ) + if(jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0) { - printbuf_reset(jso->_pb); - - if(jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0) - { - s = (size_t)jso->_pb->bpos; - r = jso->_pb->buf; - } + s = (size_t)jso->_pb->bpos; + r = jso->_pb->buf; } } if (length) *length = s; - return r; } @@ -408,12 +400,10 @@ static int json_object_object_to_json_string(struct json_object* jso, indent(pb, level+1, flags); printbuf_strappend(pb, "\""); json_escape_str(pb, iter.key, strlen(iter.key), flags); - if (flags & JSON_C_TO_STRING_SPACED) printbuf_strappend(pb, "\": "); else printbuf_strappend(pb, "\":"); - if(iter.val == NULL) printbuf_strappend(pb, "null"); else @@ -477,19 +467,6 @@ struct lh_table* json_object_get_object(const struct json_object *jso) } } -static char *stringdup(const char *s) -{ - size_t len = strnlen (s, 1024) + 1; - char *new = malloc (len); - - if (new == NULL) - return NULL; - - new[len] = 0; - return (char *) memcpy (new, s, len); -} - - int json_object_object_add_ex(struct json_object* jso, const char *const key, struct json_object *const val, @@ -504,9 +481,9 @@ int json_object_object_add_ex(struct json_object* jso, // We lookup the entry and replace the value, rather than just deleting // and re-adding it, so the existing key remains valid. hash = lh_get_hash(jso->o.c_object, (const void *)key); - existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? - NULL : - lh_table_lookup_entry_w_hash(jso->o.c_object, (const void *)key, hash); + existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? NULL : + lh_table_lookup_entry_w_hash(jso->o.c_object, + (const void *)key, hash); // The caller must avoid creating loops in the object tree, but do a // quick check anyway to make sure we're not creating a trivial loop. @@ -515,26 +492,17 @@ int json_object_object_add_ex(struct json_object* jso, if (!existing_entry) { - char *key_dup = stringdup(key); - - /* key duplicate must be done first */ - const void *const k = ((opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ? (const void *)key : (const void *)key_dup); - - /* key duplicate must be freed here if constant */ - if (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) - free(key_dup); - + const void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ? + (const void *)key : strdup(key); if (k == NULL) return -1; - return lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts); - } else { - existing_value = (json_object *) lh_entry_v(existing_entry); - if (existing_value) - json_object_put(existing_value); - existing_entry->v = val; - return 0; } + existing_value = (json_object *) lh_entry_v(existing_entry); + if (existing_value) + json_object_put(existing_value); + existing_entry->v = val; + return 0; } int json_object_object_add(struct json_object* jso, const char *key, @@ -1523,15 +1491,7 @@ int json_object_deep_copy(struct json_object *src, struct json_object **dst, jso int rc; /* Check if arguments are sane ; *dst must not point to a non-NULL object */ - if (!src) { - errno = EINVAL; - return -1; - } - if (!dst) { - errno = EINVAL; - return -1; - } - if (*dst) { + if (!src || !dst || *dst) { errno = EINVAL; return -1; } @@ -1541,7 +1501,6 @@ int json_object_deep_copy(struct json_object *src, struct json_object **dst, jso rc = json_object_deep_copy_recursive(src, NULL, NULL, -1, dst, shallow_copy); if (rc < 0) { - json_object_put(*dst); *dst = NULL; } diff --git a/printbuf.c b/printbuf.c index a9f1dc1..6c77b5d 100644 --- a/printbuf.c +++ b/printbuf.c @@ -30,12 +30,6 @@ #include "snprintf_compat.h" #include "vasprintf_compat.h" -/* Default starting size of buffer and - * sprintbuf buffer - * */ -#define PRINTBUF_DEFAULT_SIZE (32) -#define PRINTBUF_DEFAULT_SIZE_BUF (128) - static int printbuf_extend(struct printbuf *p, int min_size); struct printbuf* printbuf_new(void) @@ -44,10 +38,9 @@ struct printbuf* printbuf_new(void) p = (struct printbuf*)calloc(1, sizeof(struct printbuf)); if(!p) return NULL; - p->size = PRINTBUF_DEFAULT_SIZE; + p->size = 32; p->bpos = 0; - p->buf = (char*)calloc(1, p->size); - if(p->buf == NULL) { + if(!(p->buf = (char*)malloc(p->size))) { free(p); return NULL; } @@ -66,60 +59,32 @@ struct printbuf* printbuf_new(void) */ static int printbuf_extend(struct printbuf *p, int min_size) { -#define PRINTBUF_EXTEND_BY_BYTES_MIN (8) - char *t = NULL; + char *t; int new_size; - if( - (p != NULL) && - (p->buf != NULL) && - (p->size >= min_size) - ) + if (p->size >= min_size) return 0; new_size = p->size * 2; - - if (new_size < (min_size + PRINTBUF_EXTEND_BY_BYTES_MIN)) - new_size = min_size + PRINTBUF_EXTEND_BY_BYTES_MIN; - + if (new_size < min_size + 8) + new_size = min_size + 8; #ifdef PRINTBUF_DEBUG MC_DEBUG("printbuf_memappend: realloc " "bpos=%d min_size=%d old_size=%d new_size=%d\n", p->bpos, min_size, p->size, new_size); #endif /* PRINTBUF_DEBUG */ - - if (p != NULL) - { - t = (char*)calloc(1, new_size); - if ( (t != NULL) && - (p->buf != NULL)) - { - memcpy(t, p->buf, p->size); - } - } - - if (t == NULL) + if(!(t = (char*)realloc(p->buf, new_size))) return -1; - p->size = new_size; p->buf = t; - return 0; } int printbuf_memappend(struct printbuf *p, const char *buf, int size) { - - if ( (p->size > 0) && (p->buf == NULL)) { - int size_wanted = p->size; - p->size = 0; - if (printbuf_extend(p, size_wanted) < 0) - return -1; - } - if (p->size <= p->bpos + size + 1) { - if (printbuf_extend(p, p->bpos + size + 1) < 0) - return -2; + if (printbuf_extend(p, p->bpos + size + 1) < 0) + return -1; } memcpy(p->buf + p->bpos, buf, size); p->bpos += size; @@ -151,20 +116,20 @@ int sprintbuf(struct printbuf *p, const char *msg, ...) { va_list ap; char *t; - long int size; - char buf[PRINTBUF_DEFAULT_SIZE_BUF]; + int size; + char buf[128]; /* user stack buffer first */ va_start(ap, msg); - size = (long int)vsnprintf(buf, sizeof(buf), msg, ap); + size = vsnprintf(buf, 128, msg, ap); va_end(ap); /* if string is greater than stack buffer, then use dynamic string with vasprintf. Note: some implementation of vsnprintf return -1 if output is truncated whereas some return the number of bytes that would have been written - this code handles both cases. */ - if(size == -1 || size > (long int)sizeof(buf)) { + if(size == -1 || size > 127) { va_start(ap, msg); - if((size = (long int)vasprintf(&t, msg, ap)) < 0) { va_end(ap); return -1; } + if((size = vasprintf(&t, msg, ap)) < 0) { va_end(ap); return -1; } va_end(ap); printbuf_memappend(p, t, size); free(t); @@ -177,29 +142,14 @@ int sprintbuf(struct printbuf *p, const char *msg, ...) void printbuf_reset(struct printbuf *p) { - if (p != NULL) - { - if ( (p->size > 0) && - (p->buf != NULL) - ) - { - p->buf[0] = '\0'; - } - - p->bpos = 0; - } + p->buf[0] = '\0'; + p->bpos = 0; } void printbuf_free(struct printbuf *p) { if(p) { - - if (p->buf != NULL) - free(p->buf); - - p->buf = NULL; - + free(p->buf); free(p); - p = NULL; } } diff --git a/test_printbuf.expected b/test_printbuf.expected deleted file mode 100644 index 6464412..0000000 --- a/test_printbuf.expected +++ /dev/null @@ -1,33 +0,0 @@ -test_basic_printbuf_memset: starting test -Buffer contents:blue:1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -test_basic_printbuf_memset: end test -======================================== -test_printbuf_memset_length: starting test -Buffer length: 0 -Buffer length: 12 -Buffer length: 18 -Buffer length: 76 -Buffer length: 76 -Buffer length: 77 -test_printbuf_memset_length: end test -======================================== -test_printbuf_memappend: starting test -Buffer length: 0 -Appended 32 bytes for resize: [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] -Partial append: 3, [blu] -With embedded \0 character: 4, [ab] -Append to just before resize: 31, [XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX] -Append to just after resize: 32, [XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX] -Buffer size after printbuf_strappend(): 16, [XXXXXXXXXXXXXXXX] -test_printbuf_memappend: end test -======================================== -test_sprintbuf: starting test -Buffer length: 0 -sprintbuf to just after resize(31+1): 32, [XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX], strlen(buf)=32 -5, [plain] -6, [plain1] -16, [plain12147483647] -27, [plain12147483647-2147483648] -29, [plain12147483647-2147483648%s] -test_sprintbuf: end test -========================================