Sanity check cstore limit/offset param values
authorBill Erickson <berick@esilibrary.com>
Thu, 16 Aug 2012 19:40:58 +0000 (15:40 -0400)
committerLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Wed, 22 Aug 2012 18:49:54 +0000 (14:49 -0400)
Certain cstore calls (direct / json_query) that support limit/offset
params called with a non-string / non-numeric value

e.g. { "limit": null }

result in a cstore segfault as it tries to call atoi(NULL) under the
covers.  This patch prevents this by verifying that the limit/offset
values are actual strings or numbers (i.e. return a value from
jsonObjectGetString) and not JSON_NULL, etc.

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Signed-off-by: Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Open-ILS/src/c-apps/oils_sql.c

index 902a4e0..c9c1618 100644 (file)
@@ -4910,12 +4910,16 @@ char* SELECT (
 
        if( limit ){
                const char* str = jsonObjectGetString( limit );
-               buffer_fadd( sql_buf, " LIMIT %d", atoi( str ));
+               if (str) { // limit could be JSON_NULL, etc.
+                       buffer_fadd( sql_buf, " LIMIT %d", atoi( str ));
+               }
        }
 
        if( offset ) {
                const char* str = jsonObjectGetString( offset );
-               buffer_fadd( sql_buf, " OFFSET %d", atoi( str ));
+               if (str) {
+                       buffer_fadd( sql_buf, " OFFSET %d", atoi( str ));
+               }
        }
 
        if( !(flags & SUBSELECT) )
@@ -5453,21 +5457,25 @@ static char* buildSELECT ( const jsonObject* search_hash, jsonObject* rest_of_qu
                const jsonObject* limit = jsonObjectGetKeyConst( rest_of_query, "limit" );
                if( limit ) {
                        const char* str = jsonObjectGetString( limit );
-                       buffer_fadd(
-                               sql_buf,
-                               " LIMIT %d",
-                               atoi(str)
-                       );
+                       if (str) {
+                               buffer_fadd(
+                                       sql_buf,
+                                       " LIMIT %d",
+                                       atoi(str)
+                               );
+                       }
                }
 
                const jsonObject* offset = jsonObjectGetKeyConst( rest_of_query, "offset" );
                if( offset ) {
                        const char* str = jsonObjectGetString( offset );
-                       buffer_fadd(
-                               sql_buf,
-                               " OFFSET %d",
-                               atoi( str )
-                       );
+                       if (str) {
+                               buffer_fadd(
+                                       sql_buf,
+                                       " OFFSET %d",
+                                       atoi( str )
+                               );
+                       }
                }
        }