Description: Modify tmpfile logic
 Leptonica has a problem with tmpfiles, which is under review
 with upstream. In the meantime, this patch will help Tesseract
 users who are currently running into trouble. Tesseract is a
 major customer of Leptonica.
 .
 leptonlib (1.73-2) unstable; urgency=medium
 .
   * Initial triage for tmpfile bug #830660
Author: Jeff Breidenbach <jab@debian.org>

---
The information above should follow the Patch Tagging Guidelines, please
checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
are templates for supplementary fields that you might want to add:

Origin: Jeff Breidenbach <jab@debian.org>
Bug-Debian: http://bugs.debian.org/830660

--- leptonlib-1.73.orig/src/utils.c
+++ leptonlib-1.73/src/utils.c
@@ -2967,6 +2967,9 @@ size_t  len;
  *          (b) multiple threads from a single process call this function, or
  *          (c) there is the possibility of an attack where the intruder
  *              is logged onto the server and might try to guess filenames.
+ * IMPORTANT: This function has been modified as a temporary Debian
+ * specific workaround for Debian Bug #830660, while upstream works on
+ * an official solution. All input parameters are disabled.
  */
 char *
 genTempFilename(const char  *dir,
@@ -2974,43 +2977,24 @@ genTempFilename(const char  *dir,
                 l_int32      usetime,
                 l_int32      usepid)
 {
-char     buf[256];
-char    *newpath;
-l_int32  i, buflen, usec, pid, emptytail;
-
+char    *pathout;
+l_int32  size;
     PROCNAME("genTempFilename");
-
-    if (!dir)
-        return (char *)ERROR_PTR("dir not defined", procName, NULL);
-    if (dir && strlen(dir) == 1 && dir[0] == '/')
-        return (char *)ERROR_PTR("dir == '/' not permitted", procName, NULL);
-    if (tail && strlen(tail) > 0 && stringFindSubstr(tail, "/", NULL))
-        return (char *)ERROR_PTR("tail can't contain '/'", procName, NULL);
-    emptytail = tail && (strlen(tail) == 0);
-    if (!usetime && !usepid && (!tail || emptytail))
-        return (char *)ERROR_PTR("name can't be a directory", procName, NULL);
-
-    if (usepid) pid = getpid();
-    buflen = sizeof(buf);
-    for (i = 0; i < buflen; i++)
-        buf[i] = 0;
-    l_getCurrentTime(NULL, &usec);
-
-    newpath = genPathname(dir, NULL);
-    if (usetime && usepid)
-        snprintf(buf, buflen, "%s/%d_%d_", newpath, usec, pid);
-    else if (usetime)
-        snprintf(buf, buflen, "%s/%d_", newpath, usec);
-    else if (usepid)
-        snprintf(buf, buflen, "%s/%d_", newpath, pid);
-    else
-        snprintf(buf, buflen, "%s/", newpath);
-    LEPT_FREE(newpath);
-
-    return stringJoin(buf, tail);
+#ifdef _WIN32
+  #error "mkstemp is probably not supported on Windows"
+#endif  /*  _WIN32 */
+    char pattern[] = "/tmp/lept.XXXXXX";
+    int fd = mkstemp(pattern);
+    if (fd == -1) {
+      return (char *)ERROR_PTR("failed to get tempfile", procName, NULL);
+    }
+    close(fd);
+    if ((pathout = (char *)LEPT_CALLOC(sizeof(pattern), sizeof(char))) == NULL)
+      return (char *)ERROR_PTR("pathout not made", procName, NULL);
+    strncpy(pathout, pattern, sizeof(pattern) - 1);
+    return pathout;
 }
 
-
 /*!
  *  extractNumberFromFilename()
  *
