about summary refs log tree commit diff
path: root/sourcecodes/tier_description_processing_gom.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/tier_description_processing_gom.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/tier_description_processing_gom.php')
-rw-r--r--sourcecodes/tier_description_processing_gom.php48
1 files changed, 33 insertions, 15 deletions
diff --git a/sourcecodes/tier_description_processing_gom.php b/sourcecodes/tier_description_processing_gom.php
index f6e9eee2..55d315cb 100644
--- a/sourcecodes/tier_description_processing_gom.php
+++ b/sourcecodes/tier_description_processing_gom.php
@@ -10,26 +10,34 @@ $tier=trim($_GET['tier']);
 $dir="/tmp/bnw/";
 $tf=$dir.$keyval."tier.txt";
 $fpvar = fopen("$tf","w");
-fwrite($fpvar,"$tier");
-fclose($fpvar);
+if ($fpvar !== false) {
+  fwrite($fpvar,"$tier");
+  fclose($fpvar);
+}
 
 $tierdesc=trim($_GET['tierdesc']);
 $tdf=$dir.$keyval."tierdesc.txt";
 $fpvard = fopen("$tdf","w");
-fwrite($fpvard,"$tierdesc");
-fclose($fpvard);
+if ($fpvard !== false) {
+  fwrite($fpvard,"$tierdesc");
+  fclose($fpvard);
+}
 
 $ban=trim($_GET['ban']);
 $banf=$dir.$keyval."ban.txt";
 $fpvarban = fopen("$banf","w");
-fwrite($fpvarban,"$ban");
-fclose($fpvarban);
+if ($fpvarban !== false) {
+  fwrite($fpvarban,"$ban");
+  fclose($fpvarban);
+}
 
 $white=trim($_GET['white']);
 $whitef=$dir.$keyval."white.txt";
 $fpvarwhite = fopen("$whitef","w");
-fwrite($fpvarwhite,"$white");
-fclose($fpvarwhite);
+if ($fpvarwhite !== false) {
+  fwrite($fpvarwhite,"$white");
+  fclose($fpvarwhite);
+}
 
 
 }
@@ -47,9 +55,11 @@ function enter_ban_list($t1,$s1,$t2,$s2,$tier_d,$fpvar)
             $data_val_2=$tier_d[$t2][$j];
             if($data_val_1!=$data_val_2)
             {
-               
-               fprintf($fpvar,"%s\t%s\n",$data_val_1,$data_val_2); 
-              
+
+               if ($fpvar !== false) {
+                 fprintf($fpvar,"%s\t%s\n",$data_val_1,$data_val_2);
+               }
+
 
             }
    
@@ -170,7 +180,9 @@ for ($i=0;$i<$n;$i+=2)
 
 	if($d1!="" && $d2!="")
 	{
-  		fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  		if ($fpvar !== false) {
+  		  fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  		}
   		$nn++;
 	}
 
@@ -195,7 +207,9 @@ $d2=trim($data[$ii]);
 
 if($d1!="" && $d2!="")
 {
-  fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  if ($fpvar !== false) {
+    fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  }
   $nn++;
 }
 
@@ -226,8 +240,12 @@ $Textwhite=$sid2.".txt";
 $fpb = fopen($Textban,"w");
 $fpw = fopen($Textwhite,"w");
 $datpost="From\tTo\n";
-fwrite($fpb,"$datpost");
-fwrite($fpw,"$datpost");
+if ($fpb !== false) {
+  fwrite($fpb,"$datpost");
+}
+if ($fpw !== false) {
+  fwrite($fpw,"$datpost");
+}
 
 describe_tier($fpb,$keyval);
 banlist($fpb,$ban);