Description: fix the unconditional usage of the PATH_MAX constant.
Origin:      vendor
Author:      Pino Toscano <pino@debian.org>
Last-Update: 2012-10-06

--- a/estseek.c
+++ b/estseek.c
@@ -1486,14 +1486,14 @@ static void showtop(void){
 static void expandquery(const char *word, CBLIST *result){
   CBLIST *words;
   const char *tmpdir;
-  char oname[PATH_MAX], cmd[PATH_MAX], *ebuf;
+  char *oname, *cmd, *ebuf;
   int i;
   cblistpush(result, word, -1);
   tmpdir = getenv("TMP");
   if(!tmpdir) tmpdir = getenv("TEMP");
   if(!tmpdir) tmpdir = ESTPATHSTR "tmp";
-  sprintf(oname, "%s%c%s.%08d", tmpdir, ESTPATHCHR, g_scriptname, (int)getpid());
-  sprintf(cmd, "%s > %s", g_qxpndcmd, oname);
+  oname = cbsprintf("%s%c%s.%08d", tmpdir, ESTPATHCHR, g_scriptname, (int)getpid());
+  cmd = cbsprintf("%s > %s", g_qxpndcmd, oname);
   ebuf = cbsprintf("ESTWORD=%s", word);
   putenv(ebuf);
   system(cmd);
@@ -1506,6 +1506,8 @@ static void expandquery(const char *word
     cblistclose(words);
   }
   unlink(oname);
+  cbfree(cmd);
+  cbfree(oname);
 }
 
 
--- a/estscout.c
+++ b/estscout.c
@@ -425,21 +425,29 @@ static void showresult(void){
   CBMAP *rmap;
   CBLIST *list;
   const char *rp;
-  char tmppath[PATH_MAX], *expr;
-  int i, j, num, hnum, max;
+  char *tmppath, *expr;
+  int i, j, num, hnum, max, l;
   struct stat sbuf;
   time_t now;
+  size_t tmppathlen;
   printf("Content-Type: text/plain\r\n");
   printf("Cache-Control: no-cache, must-revalidate, no-transform\r\n");
   printf("Pragma: no-cache\r\n");
   printf("X-Run-Count: %d\r\n", g_runcnt);
   printf("\r\n");
+  tmppath = NULL;
   now = time(NULL);
+  tmppathlen = 0;
   if(*g_tmpdir != '\0' && *g_tmpdir != '@' && g_cclife >= 0 &&
      (now + g_runcnt) % CCPURGEFREQ == 1 && (list = cbdirlist(g_tmpdir)) != NULL){
     for(i = 0; i < cblistnum(list); i++){
       rp = cblistval(list, i, NULL);
       if(!cbstrbwmatch(rp, CACHESUFFIX)) continue;
+      l = strlen(g_tmpdir) + 1 + strlen(rp) + 1;
+      if (l > tmppathlen){
+        tmppath = cbrealloc(tmppath, l);
+        tmppathlen = l;
+      }
       sprintf(tmppath, "%s%c%s", g_tmpdir, ESTPATHCHR, rp);
       if(stat(tmppath, &sbuf) != -1 && now - sbuf.st_mtime > g_cclife) unlink(tmppath);
     }
@@ -539,6 +547,7 @@ static void showresult(void){
   }
   free(elems);
   cbmapclose(rmap);
+  cbfree(tmppath);
 }
 
 
@@ -553,17 +562,20 @@ static void *procsearch(void *targ){
   CBMAP *rmap, *hints, *umap;
   CBLIST *list;
   const char *rp, *uri;
-  char tmppath[PATH_MAX], *value, *expr, *ord, *enc, *pv, *mp, numbuf[NUMBUFSIZ];
+  char *tmppath, *value, *expr, *ord, *enc, *pv, *mp, numbuf[NUMBUFSIZ];
   const int *scores;
-  int i, *res, rnum, snum, check, len, num;
+  int i, *res, rnum, snum, check, len, num, l;
   struct stat sbuf;
   double etime;
   time_t now;
+  size_t tmppathlen;
   argp = (TARGSRCH *)targ;
   db = argp->db;
   cond = argp->cond;
   rmap = argp->rmap;
+  tmppath = NULL;
   now = argp->now;
+  tmppathlen = 0;
   if(!db || (!est_cond_phrase(cond) && !est_cond_attrs(cond))){
     argp->hnum = -1;
     return NULL;
@@ -574,11 +586,20 @@ static void *procsearch(void *targ){
     if(*g_tmpdir == '@'){
       num = dpinnerhash(enc, -1) % 0x100;
       if(g_cclife >= 0 && (now + g_runcnt) % (CCPURGEFREQ / 16) == 0){
+        /* start with 30 characters more, to save allocations below */
+        l = strlen(g_tmpdir + 1) + 1 + 2 + 1 + 30 + 1;
+        tmppath = cbrealloc(tmppath, l);
+        tmppathlen = l;
         sprintf(tmppath, "%s%c%02x", g_tmpdir + 1, ESTPATHCHR, num);
         if((list = cbdirlist(tmppath)) != NULL){
           for(i = 0; i < cblistnum(list); i++){
             rp = cblistval(list, i, NULL);
             if(!cbstrbwmatch(rp, CACHESUFFIX)) continue;
+            l = strlen(g_tmpdir + 1) + 1 + 2 + 1 + strlen(rp) + 1;
+            if (l > tmppathlen){
+              tmppath = cbrealloc(tmppath, l);
+              tmppathlen = l;
+            }
             sprintf(tmppath, "%s%c%02x%c%s",
                     g_tmpdir + 1, ESTPATHCHR, num, ESTPATHCHR, rp);
             if(stat(tmppath, &sbuf) != -1 && now - sbuf.st_mtime > g_cclife) unlink(tmppath);
@@ -586,16 +607,22 @@ static void *procsearch(void *targ){
           cblistclose(list);
         }
       }
+      l = strlen(g_tmpdir + 1) + 1 + 2 + 1 + strlen(enc) + strlen(CACHESUFFIX) + 1;
+      if (l > tmppathlen){
+        tmppath = cbrealloc(tmppath, l);
+        tmppathlen = l;
+      }
       sprintf(tmppath, "%s%c%02x%c%s%s",
               g_tmpdir + 1, ESTPATHCHR, num, ESTPATHCHR, enc, CACHESUFFIX);
     } else {
+      l = strlen(g_tmpdir) + 1 + strlen(enc) + strlen(CACHESUFFIX) + 1;
+      tmppath = cbrealloc(tmppath, l);
+      tmppathlen = l;
       sprintf(tmppath, "%s%c%s%s", g_tmpdir, ESTPATHCHR, enc, CACHESUFFIX);
     }
     free(enc);
-  } else {
-    *tmppath = '\0';
   }
-  if(*tmppath != '\0' && stat(tmppath, &sbuf) != -1){
+  if(tmppath != NULL && stat(tmppath, &sbuf) != -1){
     if((g_cclife < 0 || now - sbuf.st_mtime <= g_cclife) &&
        (value = cbreadfile(tmppath, &len)) != NULL){
       if(pthread_mutex_lock(&mymutex) == 0){
@@ -623,6 +650,7 @@ static void *procsearch(void *targ){
         }
       }
       g_cache = TRUE;
+      cbfree(tmppath);
       return NULL;
     } else {
       unlink(tmppath);
@@ -640,7 +668,7 @@ static void *procsearch(void *targ){
   }
   check = (rp = est_cond_phrase(cond)) != NULL && *rp != '\0' && *rp != '[' && *rp != '*' ?
     g_scancheck : 0;
-  ofp = *tmppath != '\0' && etime >= CCGENMINTIME ? fopen(tmppath, "w") : NULL;
+  ofp = tmppath != NULL && etime >= CCGENMINTIME ? fopen(tmppath, "w") : NULL;
   num = (rp = cbmapget(hints, "", 0, NULL)) != NULL ? atoi(rp) : 0;
   if(rnum < est_cond_max(cond) && num < rnum) num = rnum;
   argp->hnum += num;
@@ -757,6 +785,7 @@ static void *procsearch(void *targ){
   if(ofp) fclose(ofp);
   free(res);
   cbmapclose(hints);
+  cbfree(tmppath);
   return NULL;
 }
 
--- a/estsupt.c
+++ b/estsupt.c
@@ -287,7 +287,7 @@ static const char *skiplabel(const char
 static void savecache(const char *key){
   int maxlen = 1024 * 1024 * 32;
   const char *rp;
-  char *buf, tmppath[PATH_MAX];
+  char *buf, *tmppath;
   int i, len, c;
   buf = NULL;
   len = 0;
@@ -308,10 +308,11 @@ static void savecache(const char *key){
     len = strlen(buf);
   }
   if(*g_tmpdir == '@'){
-    sprintf(tmppath, "%s%c%02x%c%s%s", g_tmpdir + 1, ESTPATHCHR,
-            dpinnerhash(key, -1) % 0x100, ESTPATHCHR, key, CACHESUFFIX);
+    tmppath = cbsprintf("%s%c%02x%c%s%s", g_tmpdir + 1, ESTPATHCHR,
+                        dpinnerhash(key, -1) % 0x100, ESTPATHCHR, key,
+                        CACHESUFFIX);
   } else {
-    sprintf(tmppath, "%s%c%s%s", g_tmpdir, ESTPATHCHR, key, CACHESUFFIX);
+    tmppath = cbsprintf("%s%c%s%s", g_tmpdir, ESTPATHCHR, key, CACHESUFFIX);
   }
   if(buf && cbwritefile(tmppath, buf, len)){
     printf("Content-Type: text/plain\r\n");
@@ -328,6 +329,7 @@ static void savecache(const char *key){
     printf("invalid parameter\n");
   }
   free(buf);
+  cbfree(tmppath);
 }
 
 
@@ -446,14 +448,16 @@ static char *myencode(const char *str){
 
 /* show the cache */
 static void showcache(void){
-  char tmppath[PATH_MAX], *str;
+  char *tmppath, *str;
   int len;
   struct stat sbuf;
   if(*g_tmpdir == '@'){
-    sprintf(tmppath, "%s%c%02x%c%s%s", g_tmpdir + 1, ESTPATHCHR,
-            dpinnerhash(p_cache, -1) % 0x100, ESTPATHCHR, p_cache, CACHESUFFIX);
+    tmppath = cbsprintf("%s%c%02x%c%s%s", g_tmpdir + 1, ESTPATHCHR,
+                        dpinnerhash(p_cache, -1) % 0x100, ESTPATHCHR, p_cache,
+                        CACHESUFFIX);
   } else {
-    sprintf(tmppath, "%s%c%s%s", g_tmpdir, ESTPATHCHR, p_cache, CACHESUFFIX);
+    tmppath = cbsprintf("%s%c%s%s", g_tmpdir, ESTPATHCHR, p_cache,
+                        CACHESUFFIX);
   }
   if(stat(tmppath, &sbuf) != -1){
     if(g_cclife < 0 || time(NULL) - sbuf.st_mtime <= g_cclife){
@@ -465,6 +469,7 @@ static void showcache(void){
         printf("\r\n");
         fwrite(str, 1, len, stdout);
         free(str);
+        cbfree(tmppath);
         return;
       }
     } else {
@@ -477,6 +482,7 @@ static void showcache(void){
   printf("Pragma: no-cache\r\n");
   printf("\r\n");
   printf("the cache was not found\n");
+  cbfree(tmppath);
 }
 
 
@@ -489,21 +495,29 @@ static void showresult(void){
   CBLIST *list;
   CBDATUM *obuf;
   const char *rp;
-  char tmppath[PATH_MAX], *str;
-  int i, len, code, hnum, num;
+  char *tmppath, *str;
+  int i, len, code, hnum, num, l;
   struct stat sbuf;
   time_t now;
+  size_t tmppathlen;
   printf("Content-Type: text/plain\r\n");
   printf("Cache-Control: no-cache, must-revalidate, no-transform\r\n");
   printf("Pragma: no-cache\r\n");
   printf("X-Run-Count: %d\r\n", g_runcnt);
   printf("\r\n");
+  tmppath = NULL;
   now = time(NULL);
+  tmppathlen = 0;
   if(*g_tmpdir != '\0' && *g_tmpdir != '@' && g_cclife >= 0 &&
      (now + g_runcnt) % CCPURGEFREQ == 0 && (list = cbdirlist(g_tmpdir)) != NULL){
     for(i = 0; i < cblistnum(list); i++){
       rp = cblistval(list, i, NULL);
       if(!cbstrbwmatch(rp, CACHESUFFIX)) continue;
+      l = strlen(g_tmpdir) + 1 + strlen(rp) + 1;
+      if (l > tmppathlen){
+        tmppath = cbrealloc(tmppath, l);
+        tmppathlen = l;
+      }
       sprintf(tmppath, "%s%c%s", g_tmpdir, ESTPATHCHR, rp);
       if(stat(tmppath, &sbuf) != -1 && now - sbuf.st_mtime > g_cclife) unlink(tmppath);
     }
@@ -513,11 +527,20 @@ static void showresult(void){
     if(*g_tmpdir == '@'){
       num = dpinnerhash(p_querykey, -1) % 0x100;
       if(g_cclife >= 0 && (now + g_runcnt) % (CCPURGEFREQ / 16) == 0){
+        /* start with 30 characters more, to save allocations below */
+        l = strlen(g_tmpdir + 1) + 1 + 2 + 1 + 30 + 1;
+        tmppath = cbrealloc(tmppath, l);
+        tmppathlen = l;
         sprintf(tmppath, "%s%c%02x", g_tmpdir + 1, ESTPATHCHR, num);
         if((list = cbdirlist(tmppath)) != NULL){
           for(i = 0; i < cblistnum(list); i++){
             rp = cblistval(list, i, NULL);
             if(!cbstrbwmatch(rp, CACHESUFFIX)) continue;
+            l = strlen(g_tmpdir + 1) + 1 + 2 + 1 + strlen(rp) + 1;
+            if (l > tmppathlen){
+              tmppath = cbrealloc(tmppath, l);
+              tmppathlen = l;
+            }
             sprintf(tmppath, "%s%c%02x%c%s",
                     g_tmpdir + 1, ESTPATHCHR, num, ESTPATHCHR, rp);
             if(stat(tmppath, &sbuf) != -1 && now - sbuf.st_mtime > g_cclife) unlink(tmppath);
@@ -525,15 +548,21 @@ static void showresult(void){
           cblistclose(list);
         }
       }
+      l = strlen(g_tmpdir + 1) + 1 + 2 + 1 + strlen(p_querykey) + strlen(CACHESUFFIX) + 1;
+      if (l > tmppathlen){
+        tmppath = cbrealloc(tmppath, l);
+        tmppathlen = l;
+      }
       sprintf(tmppath, "%s%c%02x%c%s%s",
-              g_tmpdir + 1, ESTPATHCHR, num, ESTPATHCHR, p_querykey, CACHESUFFIX);
+		      g_tmpdir + 1, ESTPATHCHR, num, ESTPATHCHR, p_querykey, CACHESUFFIX);
     } else {
+      l = strlen(g_tmpdir) + 1 + strlen(p_querykey) + strlen(CACHESUFFIX) + 1;
+      tmppath = cbrealloc(tmppath, l);
+      tmppathlen = l;
       sprintf(tmppath, "%s%c%s%s", g_tmpdir, ESTPATHCHR, p_querykey, CACHESUFFIX);
     }
-  } else {
-    *tmppath = '\0';
   }
-  if(*tmppath != '\0' && stat(tmppath, &sbuf) != -1){
+  if(tmppath != NULL && stat(tmppath, &sbuf) != -1){
     if((g_cclife < 0 || now - sbuf.st_mtime <= g_cclife) &&
        (str = cbreadfile(tmppath, &len)) != NULL){
       g_hnum = atoi(str);
@@ -547,6 +576,7 @@ static void showresult(void){
       }
       free(str);
       g_cache = TRUE;
+      cbfree(tmppath);
       return;
     } else {
       unlink(tmppath);
@@ -559,7 +589,7 @@ static void showresult(void){
        code == 200){
       g_hnum = atoi(cbdatumptr(obuf));
       fwrite(cbdatumptr(obuf), 1, cbdatumsize(obuf), stdout);
-      if(*tmppath != '\0') cbwritefile(tmppath, cbdatumptr(obuf), cbdatumsize(obuf));
+      if(tmppath != NULL) cbwritefile(tmppath, cbdatumptr(obuf), cbdatumsize(obuf));
       cbdatumclose(obuf);
       free(str);
       return;
@@ -616,6 +646,7 @@ static void showresult(void){
   cbdatumclose(obuf);
   free(elems);
   cbmapclose(rmap);
+  cbfree(tmppath);
 }
 
 
