6425a939b8a452d5187fb3cc68c059373428face
jcasper
  Wed Jul 24 16:03:50 2024 -0700
RNA structure picture generation should realize when the postscript file
it wants to use doesn't exist, refs #34156

diff --git src/hg/hgGene/rnaStructure.c src/hg/hgGene/rnaStructure.c
index 105cf61..69a6324 100644
--- src/hg/hgGene/rnaStructure.c
+++ src/hg/hgGene/rnaStructure.c
@@ -215,30 +215,58 @@
 
 if (fold == NULL)
     {
     warn("Couldn't load %s from %s", geneId, table);
     return;
     }
 if (sameString(how, "text"))
     {
     hPrintf("<TT><PRE>");
     hPrintf("%s\n%s (%1.2f)\n", fold->seq, fold->fold, fold->energy);
     hPrintf("</PRE></TT>");
     }
 else if (sameString(how, "picture"))
     {
     char *psFile = cartString(cart, hggMrnaFoldPs);
+
+    // Sanity check the psFile variable
+    // It should point to a .ps file that exists in the trash/foldUtr5 or foldUtr3 directory.
+    // That means it should match trashDir()/foldUtr[35]/foldUtr[35]_.*\.ps as a regex,
+    // and that the file should exist.
+
+    if (!endsWith(psFile, ".ps"))
+        {
+        warn("Invalid file provided for creating of RNA structure pdf/image");
+        return;
+        }
+    char sanCheck[4096];
+    safef(sanCheck, sizeof(sanCheck), "%s/foldUtr5/foldUtr5_", trashDir());
+    if (!startsWith(sanCheck, psFile))
+        {
+        safef(sanCheck, sizeof(sanCheck), "%s/foldUtr3/foldUtr3_", trashDir());
+        if (!startsWith(sanCheck, psFile))
+            {
+            warn("Invalid file provided for creating of RNA structure pdf/image");
+            return;
+            }
+        }
+    if (!fileExists(psFile))
+        {
+        warn("Indicated postscript file for RNA structure pdf/image generation does not exist.");
+        return;
+        }
+
     char *rootName = cloneString(psFile);
     char pngName[256];
     char pdfName[256];
 
     chopSuffix(rootName);
     safef(pngName, sizeof(pngName), "%s.png", rootName);
     safef(pdfName, sizeof(pngName), "%s.pdf", rootName);
     hPrintf("<H2>%s (%s) %s energy %1.2f</H2>\n", 
     	geneName, geneId, table, fold->energy);
     if (!fileExists(pdfName))
         {
         char *command[] = { "ps2pdf", psFile, pdfName, NULL};
         struct pipeline *pl = pipelineOpen1(command, pipelineWrite | pipelineNoAbort, "/dev/null", NULL, 0);
         int sysRet = pipelineWait(pl);
         if (sysRet != 0)