[asterisk-commits] dlee: branch dlee/jansson r378907 - in /team/dlee/jansson: include/asterisk/ ...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Jan 10 09:25:32 CST 2013


Author: dlee
Date: Thu Jan 10 09:25:27 2013
New Revision: 378907

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=378907
Log:
Fixed ast_json_dump_str to account for null terminator.
Added ast_json_load_str.

Modified:
    team/dlee/jansson/include/asterisk/json.h
    team/dlee/jansson/res/res_json.c
    team/dlee/jansson/tests/test_json.c

Modified: team/dlee/jansson/include/asterisk/json.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/jansson/include/asterisk/json.h?view=diff&rev=378907&r1=378906&r2=378907
==============================================================================
--- team/dlee/jansson/include/asterisk/json.h (original)
+++ team/dlee/jansson/include/asterisk/json.h Thu Jan 10 09:25:27 2013
@@ -453,6 +453,15 @@
 struct ast_json *ast_json_load_string(const char *input, struct ast_json_error *error);
 
 /*!
+ * \brief Parses null terminated \a input ast_str into a JSON object or array.
+ *
+ * \param[out] error Filled with information on error.
+ *
+ * \return Parsed JSON element, or \c NULL on error.
+ */
+struct ast_json *ast_json_load_str(const struct ast_str *input, struct ast_json_error *error);
+
+/*!
  * \brief Parses \a buffer with length \a buflen into a JSON object or array.
  *
  * \param[out] error Filled with information on error.

Modified: team/dlee/jansson/res/res_json.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/jansson/res/res_json.c?view=diff&rev=378907&r1=378906&r2=378907
==============================================================================
--- team/dlee/jansson/res/res_json.c (original)
+++ team/dlee/jansson/res/res_json.c Thu Jan 10 09:25:27 2013
@@ -354,7 +354,8 @@
 	 * allocation errors. Fortunately, it's not that hard.
 	 */
 	size_t remaining = ast_str_size(*dst) - ast_str_strlen(*dst);
-	while (remaining < size) {
+	/* Remaining needs to be big enough for buffer, plus null char */
+	while (remaining < size + 1) {
 		/* doubling the size of the buffer gives us 'amortized
 		 * constant' time.
 		 * See http://stackoverflow.com/a/249695/115478 for info.
@@ -426,6 +427,12 @@
 	}
 	return r;
 }
+
+struct ast_json *ast_json_load_str(const struct ast_str *input, struct ast_json_error *error)
+{
+	return ast_json_load_string(ast_str_buffer(input), error);
+}
+
 struct ast_json *ast_json_load_buf(const char *buffer, size_t buflen, struct ast_json_error *error)
 {
 	json_error_t jansson_error = {};

Modified: team/dlee/jansson/tests/test_json.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/jansson/tests/test_json.c?view=diff&rev=378907&r1=378906&r2=378907
==============================================================================
--- team/dlee/jansson/tests/test_json.c (original)
+++ team/dlee/jansson/tests/test_json.c Thu Jan 10 09:25:27 2013
@@ -599,12 +599,12 @@
 	/* dump_string NULL */
 	ast_test_check(res, NULL == ast_json_dump_string(NULL));
 
-	/* dump ast_str */
+	/* dump/load ast_str */
 	expected = ast_json_pack("{ s: i }", "one", 1);
 	astr = ast_str_create(1); /* should expand to hold output */
 	uut_res = ast_json_dump_str(expected, &astr);
 	ast_test_check(res, 0 == uut_res);
-	uut = ast_json_load_string(ast_str_buffer(astr), NULL);
+	uut = ast_json_load_str(astr, NULL);
 	ast_test_check(res, NULL != uut);
 	ast_test_check(res, ast_json_equal(expected, uut));
 	ast_free(astr);




More information about the asterisk-commits mailing list