diff --git a/ctables/ctable_search.c b/ctables/ctable_search.c index 8707cba..12b9b1e 100644 --- a/ctables/ctable_search.c +++ b/ctables/ctable_search.c @@ -618,6 +618,11 @@ ctable_SearchAction (Tcl_Interp *interp, CTable *ctable, CTableSearch *search, c case CTABLE_SEARCH_ACTION_ARRAY_WITH_NULLS: case CTABLE_SEARCH_ACTION_ARRAY: { int result = TCL_OK; + char *arrayName = Tcl_GetString(search->rowVarNameObj); + + // Clear array before filling it in. Ignore failure because it's + // OK for the array not to exist at this point. + Tcl_UnsetVar2(interp, arrayName, NULL, 0); if (search->nRetrieveFields < 0) { int i; @@ -2313,6 +2318,14 @@ ctable_SetupSearch (Tcl_Interp *interp, CTable *ctable, Tcl_Obj *CONST objv[], i } } + // If we're filling in an array, make sure the array name is not a variable + if (search->action == CTABLE_SEARCH_ACTION_ARRAY_WITH_NULLS || search->action == CTABLE_SEARCH_ACTION_ARRAY) { + char *arrayName = Tcl_GetString(search->rowVarNameObj); + if (Tcl_SetVar2(interp, arrayName, "...", "dummy", TCL_LEAVE_ERR_MSG) == NULL) { + goto errorReturn; + } + } + // If we have a code body, make sure we're not doing a write_tabsep, make // sure we have a row variable or a key variable, and that we're not // leaving the search action "none" diff --git a/ctables/tests/serious-tests-body.tcl b/ctables/tests/serious-tests-body.tcl index 60777c5..9b93ae6 100644 --- a/ctables/tests/serious-tests-body.tcl +++ b/ctables/tests/serious-tests-body.tcl @@ -388,10 +388,15 @@ puts "ok" puts -nonewline "testing 'search with -array'..." unset -nocomplain foo +set foo(probe2) dummy t search -compare {{= name {Brock Sampson}}} -array foo -fields {id dad} -code { + if [info exists foo(probe)] { + error "test array not cleared" + } if {[array names foo] != [list id]} { error "expected only 'id' element in test array" } + set foo(probe) dummy } puts "ok"