[OPEN-ILS-DEV] PATCH: osrf_json_parser.c (bug fix in _jsonParserError)

Scott McKellar mck9 at swbell.net
Mon Dec 10 23:53:37 EST 2007


This patch corrects 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.

Scott McKellar
http://home.swbell.net/mck9/ct/

Developer's Certificate of Origin 1.1 By making a contribution to
this project, I certify that:

(a) The contribution was created in whole or in part by me and I
have the right to submit it under the open source license indicated
in the file; or

(b) The contribution is based upon previous work that, to the best
of my knowledge, is covered under an appropriate open source license
and I have the right under that license to submit that work with
modifications, whether created in whole or in part by me, under the
same open source license (unless I am permitted to submit under a
different license), as indicated in the file; or

(c) The contribution was provided directly to me by some other person
who certified (a), (b) or (c) and I have not modified it; and

(d) In the case of each of (a), (b), or (c), I understand and agree
that this project and the contribution are public and that a record
of the contribution (including all personal information I submit
with it, including my sign-off) is maintained indefinitely and may
be redistributed consistent with this project or the open source
license indicated in the file.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: osrf_json_parser_c_3.patch
Type: text/x-patch
Size: 1585 bytes
Desc: 3212144265-osrf_json_parser_c_3.patch
Url : http://list.georgialibraries.org/pipermail/open-ils-dev/attachments/20071210/33439fa9/osrf_json_parser_c_3.bin


More information about the Open-ils-dev mailing list