[Opensrf-commits] r1165 - trunk/src/libopensrf
svn at svn.open-ils.org
svn at svn.open-ils.org
Tue Dec 11 07:54:25 EST 2007
Author: miker
Date: 2007-12-11 07:33:57 -0500 (Tue, 11 Dec 2007)
New Revision: 1165
Modified:
trunk/src/libopensrf/osrf_json_parser.c
Log:
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.
Modified: trunk/src/libopensrf/osrf_json_parser.c
===================================================================
--- trunk/src/libopensrf/osrf_json_parser.c 2007-12-10 02:35:36 UTC (rev 1164)
+++ trunk/src/libopensrf/osrf_json_parser.c 2007-12-11 12:33:57 UTC (rev 1165)
@@ -91,14 +91,25 @@
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 );
More information about the opensrf-commits
mailing list