better bson checking in query API
authoradam <adamansky@gmail.com>
Sat, 10 Nov 2012 12:17:30 +0000 (19:17 +0700)
committeradam <adamansky@gmail.com>
Sat, 10 Nov 2012 12:17:30 +0000 (19:17 +0700)
tcejdb/ejdb.c
tcejdb/ejdb.h

index 759a036..9e459da 100644 (file)
@@ -270,7 +270,11 @@ finish:
 
 /* Save/Update BSON into 'coll' */
 EJDB_EXPORT bool ejdbsavebson(EJCOLL *jcoll, bson *bs, bson_oid_t *oid) {
-    assert(jcoll && bs);
+    assert(jcoll);
+    if (!bs || bs->err || !bs->finished) {
+        _ejdbsetecode(jcoll->jb, JBEINVALIDBSON, __FILE__, __LINE__, __func__);
+        return false;
+    }
     if (!JBISOPEN(jcoll->jb)) {
         _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
         return false;
@@ -382,6 +386,11 @@ finish:
 }
 
 EJDB_EXPORT EJQ* ejdbcreatequery(EJDB *jb, bson *qobj, bson *orqobjs, int orqobjsnum, bson *hints) {
+    assert(jb);
+    if (!qobj || qobj->err || !qobj->finished) {
+        _ejdbsetecode(jb, JBEINVALIDBSON, __FILE__, __LINE__, __func__);
+        return NULL;
+    }
     EJQ *q;
     TCCALLOC(q, 1, sizeof (*q));
     if (qobj) {
@@ -405,6 +414,10 @@ EJDB_EXPORT EJQ* ejdbcreatequery(EJDB *jb, bson *qobj, bson *orqobjs, int orqobj
         }
     }
     if (hints) {
+        if (hints->err || !hints->finished) {
+            _ejdbsetecode(jb, JBEINVALIDBSON, __FILE__, __LINE__, __func__);
+            return NULL;
+        }
         q->hints = bson_create();
         if (bson_copy(q->hints, hints)) {
             goto error;
index d076d9f..181c8fc 100644 (file)
@@ -158,7 +158,7 @@ EJDB_EXPORT EJCOLL* ejdbcreatecoll(EJDB *jb, const char* colname, EJCOLLOPTS *op
  * @param unlink It true the collection db file and all of its index files will be removed.
  * @return If successful return true, otherwise return false.
  */
-EJDB_EXPORT bool ejdbrmcoll(EJDB *jb, const charcolname, bool unlinkfile);
+EJDB_EXPORT bool ejdbrmcoll(EJDB *jb, const char *colname, bool unlinkfile);
 
 /**
  * Persist BSON object in the collection.
@@ -169,7 +169,7 @@ EJDB_EXPORT bool ejdbrmcoll(EJDB *jb, const char* colname, bool unlinkfile);
  * @param bson BSON object id pointer.
  * @return If successful return true, otherwise return false.
  */
-EJDB_EXPORT bool ejdbsavebson(EJCOLL* coll, bson* bs, bson_oid_t* oid);
+EJDB_EXPORT bool ejdbsavebson(EJCOLL *coll, bson *bs, bson_oid_t *oid);
 
 /**
  * Remove BSON object from collection.
@@ -179,7 +179,7 @@ EJDB_EXPORT bool ejdbsavebson(EJCOLL* coll, bson* bs, bson_oid_t* oid);
  * @param oid BSON object id pointer.
  * @return
  */
-EJDB_EXPORT bool ejdbrmbson(EJCOLL* coll, bson_oid_t* oid);
+EJDB_EXPORT bool ejdbrmbson(EJCOLL *coll, bson_oid_t *oid);
 
 /**
  * Load BSON object with specified 'oid'.
@@ -188,7 +188,7 @@ EJDB_EXPORT bool ejdbrmbson(EJCOLL* coll, bson_oid_t* oid);
  * @param oid
  * @return BSON object if exists otherwise return NULL.
  */
-EJDB_EXPORT bson* ejdbloadbson(EJCOLL* coll, const bson_oid_t* oid);
+EJDB_EXPORT bson* ejdbloadbson(EJCOLL *coll, const bson_oid_t *oid);
 
 /**
  * Create query object.