about summary refs log tree commit diff
path: root/sourcecodes/graphviz_structure.php
diff options
context:
space:
mode:
authorClaude Sonnet 52026-08-17 19:26:43 +0000
committerFrederick Muriuki Muriithi2026-08-17 14:53:08 -0500
commit9cae7ea19c7c744a1ed2bd997571ec1b516a0d32 (patch)
treece73195519a319bb0dc72d75df4d77a8ac584141 /sourcecodes/graphviz_structure.php
parent4825da8e49c302392fe6018862dfcdd6f91c6192 (diff)
downloadBNW-9cae7ea19c7c744a1ed2bd997571ec1b516a0d32.tar.gz
Guard fopen() results with a false-check before fwrite/fprintf/fclose
On PHP 7.4, calling fwrite()/fprintf()/fclose() with a false handle
(a failed fopen(), e.g. from a bad path, missing directory, or
permissions issue) just emits a warning and no-ops. On PHP 8.0+, these
functions require a resource argument and throw an uncaught TypeError
instead, turning what used to be a silent degradation into a fatal
crash of the whole request.

This patch wraps every fopen()-then-write call site in the codebase
(18 files) with an `if ($handle !== false) { ... }` check, matching
the existing pattern used elsewhere in the codebase. Where the same
$fpvar handle is threaded through shared helper functions
(enter_ban_list/banlist/whitelist, duplicated across
remove_variables_processing.php, remove_variables_processing_default.php,
modify_structure_learning.php and tier_description_processing_gom.php),
the guard was added inside those functions too for consistency, even
though the call sites are currently dead/commented-out in three of the
four files — so the same landmine doesn't reappear if that code is
ever re-enabled.

Verified empirically against PHP 8.3.28: fopen() on an unwritable path
followed by fwrite() on the resulting `false` throws
  TypeError: fwrite(): Argument #1 ($stream) must be of type resource, false given
without the guard; with the guard, the call is skipped instead of
crashing. The normal (successful fopen) path was also re-run through
mat_structure.php's structure_change() and still produces identical
output to before the change.

Reviewed by: Frederick M Muriithi <fredmanglis@gmail.com>
Diffstat (limited to 'sourcecodes/graphviz_structure.php')
-rw-r--r--sourcecodes/graphviz_structure.php36
1 files changed, 23 insertions, 13 deletions
diff --git a/sourcecodes/graphviz_structure.php b/sourcecodes/graphviz_structure.php
index 1e866bc4..641a4854 100644
--- a/sourcecodes/graphviz_structure.php
+++ b/sourcecodes/graphviz_structure.php
@@ -40,7 +40,9 @@ $n=count($dataname);
 
 $initialstring="digraph G {\n"."size=\"6,8\";\n"."node [shape=square,width=1.5];\n";
 $endstring="}";
-fwrite($fout,"$initialstring");
+if ($fout !== false) {
+  fwrite($fout,"$initialstring");
+}
 //fwrite($fouttemp,"$initialstring_temp");
 
 
@@ -48,10 +50,12 @@ fwrite($fout,"$initialstring");
 $g_file_name="/tmp/bnw/".$keyval."grviz_name_file.txt";
 $grviz_name_file=fopen($g_file_name,"w");
 
-for($i=0;$i<$n;$i++)
-{
-      $row_name=trim($dataname[$i]);  
-      fwrite($grviz_name_file,"$row_name\n");
+if ($grviz_name_file !== false) {
+  for($i=0;$i<$n;$i++)
+  {
+        $row_name=trim($dataname[$i]);
+        fwrite($grviz_name_file,"$row_name\n");
+  }
 }
 
 for($i=1;$i<=$n;$i++)
@@ -67,17 +71,21 @@ for($i=1;$i<=$n;$i++)
 	   {
                $col_val=trim($col_arr[$j]);
                if($col_val==1)
-               { 
+               {
                   $col_name=trim($dataname[$j]);
-                  fprintf($fout,"%s -> %s;\n",$ii,$j);  
+                  if ($fout !== false) {
+                    fprintf($fout,"%s -> %s;\n",$ii,$j);
+                  }
                   //fprintf($fouttemp,"%s -> %s;\n",$row_name,$col_name);
-                 
+
 	        }
           }
           
      
-} 
-fwrite($fout,"$endstring");   
+}
+if ($fout !== false) {
+  fwrite($fout,"$endstring");
+}
 //fwrite($fouttemp,"$endstring");
  
 //shell_exec('/usr/bin/dot -Tpng -o /var/www/html/compbio/BNW/graphviz.jpg /var/www/html/compbio/BNW/graphviztemp.txt');
@@ -87,9 +95,11 @@ fwrite($fout,"$endstring");
 $file1="/tmp/bnw/".$keyval."run_initialstructure.sh";
 $initiallines=file_get_contents("/tmp/bnw/temp_shell_file_initial_structure");
 $all_lines="$initiallines"."$keyval\nfi\nexit";
-$fp = fopen($file1,"w"); 
-fwrite($fp, "$all_lines\n");
-fclose($fp);
+$fp = fopen($file1,"w");
+if ($fp !== false) {
+  fwrite($fp, "$all_lines\n");
+  fclose($fp);
+}
 //prepare and execute shell script for matlab with a write lock
 //$cmd="./runmat.sh $keyval";
 //system($cmd);