Skip to content

Commit

Permalink
Make the cursor object bson method store the bson result in an existi…
Browse files Browse the repository at this point in the history
…ng bson command rather than creating a new one. Make the magic numbers in the clientData structures be odd such that using one as an address will cause a protection violation on most machines.

BUGZID:
  • Loading branch information
lehenbauer committed Feb 12, 2014
1 parent 070089f commit 67c9d5e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
34 changes: 34 additions & 0 deletions generic/bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,40 @@ mongotcl_cmdNameObjToBson (Tcl_Interp *interp, Tcl_Obj *commandNameObj, bson **b
return TCL_OK;
}


/*
*----------------------------------------------------------------------
*
* mongotcl_cmdNameObjSetBson --
*
* Take a command name and a pointer to a bson structure, find the Tcl
* command info structure, confirm it's bson, destroy the old bson
* structure there and point the command to the passed-in bson
* structure.
*
*----------------------------------------------------------------------
*/
int
mongotcl_cmdNameObjSetBson (Tcl_Interp *interp, Tcl_Obj *commandNameObj, bson *newBson) {
Tcl_CmdInfo cmdInfo;
mongotcl_bsonClientData *bd;

if (!Tcl_GetCommandInfo (interp, Tcl_GetString(commandNameObj), &cmdInfo)) {
return TCL_ERROR;
}

if (cmdInfo.objClientData == NULL || ((mongotcl_bsonClientData *)cmdInfo.objClientData)->bson_magic != MONGOTCL_BSON_MAGIC) {
Tcl_AppendResult (interp, "Error: '", Tcl_GetString (commandNameObj), "' is not a bson object", NULL);
return TCL_ERROR;
}

bd = (mongotcl_bsonClientData *)cmdInfo.objClientData;

bson_destroy(bd->bson);
bd->bson = newBson;
return TCL_OK;
}


/*
*----------------------------------------------------------------------
Expand Down
6 changes: 5 additions & 1 deletion generic/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,16 @@ mongotcl_cursorObjectObjCmd(ClientData cData, Tcl_Interp *interp, int objc, Tcl_
}

case OPT_CURSOR_BSON: {
char *bsonCommandObject;

if (objc != 3) {
Tcl_WrongNumArgs (interp, 2, objv, "bsonName");
return TCL_ERROR;
}

bsonCommandObject = Tcl_GetString (objv[2]);

return mongotcl_create_bson_command (interp, Tcl_GetString (objv[2]), mongo_cursor_bson (mc->cursor));
return mongotcl_cmdNameObjSetBson (interp, bsonCommandObject, mongo_cursor_bson (mc->cursor));
}

case OPT_CURSOR_NEXT: {
Expand Down
9 changes: 6 additions & 3 deletions generic/mongotcl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

#define MONGO_HAVE_STDINT

#define MONGOTCL_BSON_MAGIC 0xf33df00d
#define MONGOTCL_BSON_MAGIC 0xf33df007

#define MONGOTCL_MONGO_MAGIC 0xf33dd00d
#define MONGOTCL_MONGO_MAGIC 0xf33db007

#define MONGOTCL_CURSOR_MAGIC 0xf33dc00c
#define MONGOTCL_CURSOR_MAGIC 0xf33dc007

#include <mongo.h>

Expand All @@ -25,6 +25,9 @@
extern int
mongotcl_cmdNameObjToBson (Tcl_Interp *interp, Tcl_Obj *commandNameObj, bson **bson);

extern int
mongotcl_cmdNameObjSetBson (Tcl_Interp *interp, Tcl_Obj *commandNameObj, bson *newBson);

extern int
mongotcl_mongoObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objvp[]);

Expand Down

0 comments on commit 67c9d5e

Please sign in to comment.