Thanks to berick for making it clear that ctx->session->userData wasn't initialized yet
authorLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Wed, 26 Oct 2011 20:05:56 +0000 (16:05 -0400)
committerLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Tue, 28 Feb 2012 16:40:09 +0000 (11:40 -0500)
Signed-off-by: Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Open-ILS/src/c-apps/oils_sql.c

index af3ba0a..e1869e9 100644 (file)
@@ -510,7 +510,7 @@ void userDataFree( void* blob ) {
        that it will free whatever else needs freeing.
 */
 static void sessionDataFree( char* key, void* item ) {
-       if( !strcmp( key, "xact_id" ) || !strcmp( key, "authkey" ) ) 
+       if( !strcmp( key, "xact_id" ) || !strcmp( key, "authkey" ) || !strncmp( key, "rs_size_", 8) 
                free( item );
        else if( !strcmp( key, "user_login" ) )
                jsonObjectFree( (jsonObject*) item );
@@ -523,6 +523,19 @@ static void pcacheFree( char* key, void* item ) {
 }
 
 /**
+       @brief Initialize session cache.
+       @param ctx Pointer to the method context.
+
+       Create a cache for the session by making the session's userData member point
+       to an osrfHash instance.
+*/
+static void initSessionCache( osrfMethodContext* ctx ) {
+       ctx->session->userData = osrfNewHash();
+       osrfHashSetCallback( (osrfHash*) ctx->session->userData, &sessionDataFree );
+       ctx->session->userDataFree = &userDataFree;
+}
+
+/**
        @brief Save a transaction id.
        @param ctx Pointer to the method context.
 
@@ -536,11 +549,8 @@ static void setXactId( osrfMethodContext* ctx ) {
 
                // If the session doesn't already have a hash, create one.  Make sure
                // that the application session frees the hash when it terminates.
-               if( NULL == cache ) {
-                       session->userData = cache = osrfNewHash();
-                       osrfHashSetCallback( cache, &sessionDataFree );
-                       ctx->session->userDataFree = &userDataFree;
-               }
+               if( NULL == cache )
+                       initSessionCache( ctx );
 
                // Save the transaction id in the hash, with the key "xact_id"
                osrfHashSet( cache, strdup( session->session_id ), "xact_id" );
@@ -586,11 +596,8 @@ static void setPermLocationCache( osrfMethodContext* ctx, const char* perm, osrf
 
                // If the session doesn't already have a hash, create one.  Make sure
                // that the application session frees the hash when it terminates.
-               if( NULL == cache ) {
-                       session->userData = cache = osrfNewHash();
-                       osrfHashSetCallback( cache, &sessionDataFree );
-                       ctx->session->userDataFree = &userDataFree;
-               }
+               if( NULL == cache )
+                       initSessionCache( ctx );
 
                osrfHash* pcache = osrfHashGet(cache, "pcache");
 
@@ -641,11 +648,8 @@ static void setUserLogin( osrfMethodContext* ctx, jsonObject* user_login ) {
 
                // If the session doesn't already have a hash, create one.  Make sure
                // that the application session frees the hash when it terminates.
-               if( NULL == cache ) {
-                       session->userData = cache = osrfNewHash();
-                       osrfHashSetCallback( cache, &sessionDataFree );
-                       ctx->session->userDataFree = &userDataFree;
-               }
+               if( NULL == cache )
+                       initSessionCache( ctx );
 
                if( user_login )
                        osrfHashSet( cache, user_login, "user_login" );
@@ -683,11 +687,8 @@ static void setAuthkey( osrfMethodContext* ctx, const char* authkey ) {
 
                // If the session doesn't already have a hash, create one.  Make sure
                // that the application session frees the hash when it terminates.
-               if( NULL == cache ) {
-                       session->userData = cache = osrfNewHash();
-                       osrfHashSetCallback( cache, &sessionDataFree );
-                       ctx->session->userDataFree = &userDataFree;
-               }
+               if( NULL == cache )
+                       initSessionCache( ctx );
 
                // Save the transaction id in the hash, with the key "xact_id"
                if( authkey && *authkey )
@@ -1431,7 +1432,7 @@ static int verifyObjectPCRUD ( osrfMethodContext* ctx, osrfHash *class, const js
        const char* method_type = osrfHashGet( method_metadata, "methodtype" );
 
        if (!rs_size) {
-               int *rs_size_from_hash = osrfHashGetFmt( (osrfHash *) ctx->session->userData, "req_%d_rs_size", ctx->request );
+               int *rs_size_from_hash = osrfHashGetFmt( (osrfHash *) ctx->session->userData, "rs_size_req_%d", ctx->request );
                if (rs_size_from_hash) {
                        rs_size = *rs_size_from_hash;
                        osrfLogDebug(OSRF_LOG_MARK, "used rs_size from request-scoped hash: %d", rs_size);
@@ -5549,6 +5550,9 @@ static jsonObject* doFieldmapperSearch( osrfMethodContext* ctx, osrfHash* class_
 
        char* pkey = osrfHashGet( class_meta, "primarykey" );
 
+       if (!ctx->session->userData)
+               initSessionCache( ctx );
+
        char *inside_verify = osrfHashGet( (osrfHash*) ctx->session->userData, "inside_verify" );
        int need_to_verify = 1;
 
@@ -5619,19 +5623,13 @@ static jsonObject* doFieldmapperSearch( osrfMethodContext* ctx, osrfHash* class_
 
        // 2. figure out one consistent rs_size for verifyObjectPCRUD to use
        // over the whole life of this request.  This means if we've already set
-       // up a req_%d_rs_size, do nothing.
-       int *rs_size = osrfHashGetFmt( (osrfHash *) ctx->session->userData, "req_%d_rs_size", ctx->request );
+       // up a rs_size_req_%d, do nothing.
+       int *rs_size = osrfHashGetFmt( (osrfHash *) ctx->session->userData, "rs_size_req_%d", ctx->request );
        if (!rs_size) { // pointer null, so value not set in hash
-               rs_size = (int *) safe_malloc( sizeof(int) );
+               rs_size = (int *) safe_malloc( sizeof(int) );   // will be freed by sessionDataFree()
                unsigned long long result_count = dbi_result_get_numrows( result );
                *rs_size = (int) result_count * (flesh_depth + 1);      // yes, we could lose some bits, but come on
-               osrfLogDebug( OSRF_LOG_MARK, "about to set rs_size to %d", *rs_size );
-               osrfHashSet( (osrfHash *) ctx->session->userData, rs_size, "req_%d_rs_size", ctx->request );
-               int *rs_size_test = osrfHashGetFmt( (osrfHash *) ctx->session->userData, "req_%d_rs_size", ctx->request );
-               if (rs_size_test)
-                       osrfLogDebug( OSRF_LOG_MARK, "immediately after set, get says rs_size is %d", *rs_size_test );
-               else
-                       osrfLogDebug( OSRF_LOG_MARK, "immediately after set, get says rs_size is NULL!" );
+               osrfHashSet( (osrfHash *) ctx->session->userData, rs_size, "rs_size_req_%d", ctx->request );
        }
 
        if( dbi_result_first_row( result )) {