From a3172bd22d8765eb5db0797e5ccda11b150ef679 Mon Sep 17 00:00:00 2001 From: miker Date: Tue, 11 Dec 2007 12:33:57 +0000 Subject: [PATCH] Patch from Scott McKellar to correct some problems with _jsonParserError(), which constructs and issues a message about a parsing error: The problems arise in the course of extracting a fragment of JSON text to provide the context of the error. 1. The code starts by picking the beginning and end of the fragment to extract. In order to avoid beginning before the start of the string, the original code goes through a loop, incrementing an index until it is non-negative. A similar loop corrects for an ending beyond the end of the string. These loops do the job, but to my eyes they look silly. I replaced them by assigning the corrected values directly, when corrections are in order. 2. There is an off-by-two error in calculating the size of the buffer needed to hold the fragment. To begin with, we miscalculate the length of the fragment. If the fragment extends from character 30 through character 40, there are 11 characters in the fragment, not 10. Then we neglect to add another byte for a terminal nul. The result is that the last two characters in the intended fragment are not displayed. If the character in error is the last or the next to last character in the string, it doesn't get displayed as part of the fragment, leading to likely bafflement. I corrected both these errors, embiggening the buffer by two. 3. The original code copies the fragment into the buffer by calling snprintf(). Besides being needlessly inefficient, snprintf() is dangerous in this context. If the copied fragment contains a format specifier such as "%s" or "%d", sprintf goes off looking for a non-existent parameter, resulting in a mangled message or worse. I replaced the snprintf() with a memcpy() and a terminal nul. git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1165 9efc2488-bf62-4759-914b-345cdb29e865 --- src/libopensrf/osrf_json_parser.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/libopensrf/osrf_json_parser.c b/src/libopensrf/osrf_json_parser.c index d190b60..2d8bc8b 100644 --- a/src/libopensrf/osrf_json_parser.c +++ b/src/libopensrf/osrf_json_parser.c @@ -91,14 +91,25 @@ void jsonSetGlobalErrorHandler(void (*errorHandler) (const char*)) { int _jsonParserError( jsonParserContext* ctx, char* err, ... ) { if( ctx->handler->handleError ) { VA_LIST_TO_STRING(err); + + // Determine the beginning and ending points of a JSON + // fragment to display, from the vicinity of the error + int pre = ctx->index - 15; + if( pre < 0 ) pre = 0; int post= ctx->index + 15; - while( pre < 0 ) pre++; - while( post >= ctx->chunksize ) post--; - int l = post - pre; - char buf[l]; - snprintf(buf, sizeof(buf), ctx->chunk + pre); - ctx->handler->handleError( ctx->userData, + if( post >= ctx->chunksize ) post = ctx->chunksize - 1; + + // Copy the fragment into a buffer + + int len = post - pre + 1; // length of fragment + char buf[len + 1]; + memcpy( buf, ctx->chunk + pre, len ); + buf[ len ] = '\0'; + + // Issue an error message + + ctx->handler->handleError( ctx->userData, "*JSON Parser Error\n - char = %c\n " "- index = %d\n - near => %s\n - %s", ctx->chunk[ctx->index], ctx->index, buf, VA_BUF ); -- 2.43.2