From 3e4978b72bb8ffabc34006168eba06ec835bcf8d Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 17 Oct 2002 14:53:32 +0000 Subject: [PATCH] Fix free-slot search in PgSetResultId so it actually works. --- src/interfaces/libpgtcl/pgtclId.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/interfaces/libpgtcl/pgtclId.c b/src/interfaces/libpgtcl/pgtclId.c index f3465069b1..cc79339c6b 100644 --- a/src/interfaces/libpgtcl/pgtclId.c +++ b/src/interfaces/libpgtcl/pgtclId.c @@ -13,7 +13,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtclId.c,v 1.36 2002/09/23 01:43:23 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtclId.c,v 1.37 2002/10/17 14:53:32 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -340,30 +340,34 @@ PgSetResultId(Tcl_Interp *interp, char *connid_c, PGresult *res) return TCL_ERROR; connid = (Pg_ConnectionId *) Tcl_GetChannelInstanceData(conn_chan); - for (resid = connid->res_last + 1; resid != connid->res_last; resid++) + /* search, starting at slot after the last one used */ + resid = connid->res_last; + for (;;) { - if (resid == connid->res_max) - { + /* advance, with wraparound */ + if (++resid >= connid->res_max) resid = 0; - break; - } + /* this slot empty? */ if (!connid->results[resid]) { connid->res_last = resid; - break; + break; /* success exit */ } + /* checked all slots? */ + if (resid == connid->res_last) + break; /* failure exit */ } if (connid->results[resid]) { - if (connid->res_max == connid->res_hardmax) + /* no free slot found, so try to enlarge array */ + if (connid->res_max >= connid->res_hardmax) { Tcl_SetResult(interp, "hard limit on result handles reached", TCL_STATIC); return TCL_ERROR; } - connid->res_last = connid->res_max; - resid = connid->res_max; + connid->res_last = resid = connid->res_max; connid->res_max *= 2; if (connid->res_max > connid->res_hardmax) connid->res_max = connid->res_hardmax; -- 2.40.0