From 9cae7ea19c7c744a1ed2bd997571ec1b516a0d32 Mon Sep 17 00:00:00 2001 From: Claude Sonnet 5 Date: Mon, 17 Aug 2026 19:26:43 +0000 Subject: 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 --- sourcecodes/add_evd.php | 30 ++++++----- sourcecodes/add_inv.php | 62 +++++++++++++--------- sourcecodes/bn_file_load_gom.php | 4 +- sourcecodes/bn_genenet.php | 6 ++- sourcecodes/create_tiers_gom_part1.php | 12 +++-- sourcecodes/example.php | 26 +++++---- sourcecodes/execute_bn_gom.php | 34 +++++++----- sourcecodes/graphviz_structure.php | 36 ++++++++----- sourcecodes/mat_structure.php | 14 ++--- sourcecodes/modified_network.php | 30 ++++++----- sourcecodes/modify_structure_learning.php | 22 +++++--- sourcecodes/net_structure.php | 10 ++-- sourcecodes/remove_variables.php | 12 +++-- sourcecodes/remove_variables_processing.php | 22 +++++--- .../remove_variables_processing_default.php | 22 +++++--- sourcecodes/test_set_predictions.php | 4 +- sourcecodes/tier_description_processing_gom.php | 48 +++++++++++------ sourcecodes/upload_structure_file.php | 4 +- 18 files changed, 252 insertions(+), 146 deletions(-) diff --git a/sourcecodes/add_evd.php b/sourcecodes/add_evd.php index 008844b8..21406efd 100644 --- a/sourcecodes/add_evd.php +++ b/sourcecodes/add_evd.php @@ -115,16 +115,17 @@ if($dt!=1) - $ft=$dir."$keyval"."var.txt"; - $f1=fopen("$ft","w"); + $ft=$dir."$keyval"."var.txt"; + $f1=fopen("$ft","w"); $ft=$dir."$keyval"."vardata.txt"; - $f2=fopen("$ft","w"); + $f2=fopen("$ft","w"); $ft=$dir."$keyval"."varname.txt"; $f3=fopen("$ft","w"); $pos = strpos($fpvarname, $sym); +if ($f1 !== false && $f2 !== false && $f3 !== false) { if($fpvarname=="") { fwrite($f1,$vriable); @@ -132,11 +133,11 @@ if($fpvarname=="") fwrite($f3,$sym); } -else +else { - if ($pos === false)// append at buttom as new records + if ($pos === false)// append at buttom as new records { - + $fpvar=$fpvar."\t".$vriable; fwrite($f1,$fpvar); @@ -147,7 +148,7 @@ else fwrite($f3,$fpvarname); } - else + else { $cld=explode("\t",$fpvarname); $j=0; @@ -158,12 +159,12 @@ else if($cc==$sym) { $inx=$j; - } + } $j++; - + } - + fwrite($f1,$fpvar); fwrite($f3,$fpvarname); @@ -178,17 +179,18 @@ else $inx=$j; fprintf($f2,"%s\t",$textdata); } - else - { + else + { fprintf($f2,"%s\t",$cc); } - + $j++; - + } } +} } diff --git a/sourcecodes/add_inv.php b/sourcecodes/add_inv.php index 26d089ed..61845a46 100644 --- a/sourcecodes/add_inv.php +++ b/sourcecodes/add_inv.php @@ -107,16 +107,17 @@ if($dt!=1) $textdata=discretemap($textdata,$sym,$dmapdata); - $ft=$dir.$keyval."var.txt"; - $f1=fopen("$ft","w"); + $ft=$dir.$keyval."var.txt"; + $f1=fopen("$ft","w"); $ft=$dir.$keyval."vardata.txt"; - $f2=fopen("$ft","w"); + $f2=fopen("$ft","w"); $ft=$dir.$keyval."varname.txt"; $f3=fopen("$ft","w"); $pos = strpos($fpvarname, $sym); +if ($f1 !== false && $f2 !== false && $f3 !== false) { if($fpvarname=="") { fwrite($f1,$vriable); @@ -124,11 +125,11 @@ if($fpvarname=="") fwrite($f3,$sym); } -else +else { - if ($pos === false)// append at buttom as new records + if ($pos === false)// append at buttom as new records { - + $fpvar=$fpvar."\t".$vriable; fwrite($f1,$fpvar); @@ -139,7 +140,7 @@ else fwrite($f3,$fpvarname); } - else + else { $cld=explode("\t",$fpvarname); $j=0; @@ -150,12 +151,12 @@ else if($cc==$sym) { $inx=$j; - } + } $j++; - + } - + fwrite($f1,$fpvar); fwrite($f3,$fpvarname); @@ -170,18 +171,19 @@ else $inx=$j; fprintf($f2,"%s\t",$textdata); } - else - { + else + { fprintf($f2,"%s\t",$cc); } - + $j++; - + } } } +} include("mat_structure.php"); $keyval = valid_keyval($keyval); @@ -222,17 +224,21 @@ $n=count($dataname); $initialstring="digraph G {\n"."size=\"6,8\"; ratio = fill;\n"."node [shape=square,width=1.5];\n"; $endstring="}"; -fwrite($fout,"$initialstring"); +if ($fout !== false) { + fwrite($fout,"$initialstring"); +} //$g_file_name="./data/".$keyval."grviz_name_file_new.txt"; $g_file_name=$dir.$keyval."grviz_name_file_new.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"); + } } @@ -249,17 +255,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); + } + - } } - - -} -fwrite($fout,"$endstring"); + + +} +if ($fout !== false) { + fwrite($fout,"$endstring"); +} ?>