[Tarantool-patches] [PATCH v2 2/2] json: print context in error mesages

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Fri Dec 20 02:19:37 MSK 2019


Hi! Thanks for the patch!

See my review fixes here and on top of the branch.

Please, review them, and if you are ok, then squash.
In that case it will be LGTM.

==============================================================================

diff --git a/third_party/lua-cjson/lua_cjson.c b/third_party/lua-cjson/lua_cjson.c
index 655d6550e..79b0253f0 100644
--- a/third_party/lua-cjson/lua_cjson.c
+++ b/third_party/lua-cjson/lua_cjson.c
@@ -847,26 +847,24 @@ static void fill_context(char *context, json_parse_t *json, int column_index)
 {
     assert(column_index >= 0);
     int length_before = column_index < CONTEXT_MAX_LENGTH_BEFORE ?
-        column_index : CONTEXT_MAX_LENGTH_BEFORE;
+                        column_index : CONTEXT_MAX_LENGTH_BEFORE;
     const char *src = json->cur_line_ptr + column_index - length_before;
-    int i = 0;
     /* Fill context before the arrow. */
-    for (; i < length_before; i++)
-        context[i] = src[i];
+    memcpy(context, src, length_before);
+    context += length_before;
+    src += length_before;
 
     /* Make the arrow. */
-    context[i] = ' ';
-    memset(context + i + 1, '>', CONTEXT_ARROW_LENGTH - 2);
-    context[i + CONTEXT_ARROW_LENGTH - 1] = ' ';
+    *(context++) = ' ';
+    memset(context, '>', CONTEXT_ARROW_LENGTH - 2);
+    context += CONTEXT_ARROW_LENGTH - 2;
+    *(context++) = ' ';
 
     /* Fill context after the arrow. */
-    for (int n = 0; n < CONTEXT_MAX_LENGTH_AFTER && src[i] != '\0' &&
-         src[i] != '\n'; i++) {
-        context[i + CONTEXT_ARROW_LENGTH] = src[i];
-        n++;
-    }
-    assert(i + CONTEXT_ARROW_LENGTH <= CONTEXT_MAX_LENGTH);
-    context[i + CONTEXT_ARROW_LENGTH] = '\0';
+    const char *end = context + CONTEXT_MAX_LENGTH_AFTER;
+    for (; context < end && *src != '\0' && *src != '\n'; ++src, ++context)
+        *context = *src;
+    *context = '\0';
 }
 
 /* This function does not return.
@@ -893,8 +891,7 @@ static void json_throw_parse_error(lua_State *l, json_parse_t *json,
 
     /* Note: token->column_index is 0 based, display starting from 1 */
     luaL_error(l, "Expected %s but found %s on line %d at character %d here "
-               "'%s'", exp, found, json->line_count, token->column_index + 1,
-               context);
+               "'%s'", exp, found, json->line_count, column_index + 1, context);
 }
 
 static inline void json_decode_ascend(json_parse_t *json)


More information about the Tarantool-patches mailing list