[OPEN-ILS-DEV] PATCH: srfsh.c (const-correctness)

Scott McKellar mck9 at swbell.net
Sat Sep 22 17:27:56 EDT 2007


This patch sprinkles a few const qualifiers over srfsh.c, and also
fixes a couple of things I stumbled across along the way.

The main purpose is to treat jsonObjects in a more const-correct 
manner.  I introduced a new jsonObjectGetKeyConst function, and I
prepared for the day when jsonObjectGetString() returns a const 
pointer instead of a non-const pointer.

This patch relies on a previous patch that defines the new
jsonObjectGetKeyConst function.

1. In handle_login() I changed hash to a const pointer, so that we 
can assign the return value of jsonObjectGetString() to it.

2. Later in the same function, I rearranged the code a bit to ensure
that we always free a prior value of login_session before giving it
a new value.  The original code potentially leaks memory.

3. In the same area I changed authtoken to a const pointer.

4. In send_request() I eliminated an unnecessary test for the
non-nullness of omsg->_result_content, because we had just tested
it a few lines ago.

5. Also in send_request(): I introduced a layer of strdup() when
we get content from jsonObjectGetString().  This change concerns
more than just const-correctness.

The problem with the original code is that we get content sometimes
from jsonFormatString() and sometimes from jsonObjectGetString().
The former returns a malloc'ed buffer, which we need to free.  The
latter returns a pointer to a buffer owned by a jsonObject, and we
should not free it.  As it happens, we do free it.  If we got the
string from jsonObjectGetString(), then we leave the jsonObject with
an invalid pointer inside it.  If we free the jsonObject later, we'll
try to free that same buffer a second time.  Oops.

Another issue is that jsonObjectGetString() can return NULL, which
we should handle more carefully.

These problems occur in two different places in the same function.
I fixed them both the same way, with some differences in the details.

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: srfsh_c_10.patch
Type: text/x-patch
Size: 3546 bytes
Desc: 3647999307-srfsh_c_10.patch
Url : http://list.georgialibraries.org/pipermail/open-ils-dev/attachments/20070922/78b60a04/srfsh_c_10.bin


More information about the Open-ils-dev mailing list