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/modified_network.php | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'sourcecodes/modified_network.php') diff --git a/sourcecodes/modified_network.php b/sourcecodes/modified_network.php index 9f38335c..3be0d69c 100644 --- a/sourcecodes/modified_network.php +++ b/sourcecodes/modified_network.php @@ -31,20 +31,22 @@ if($_POST["My_key"]!="") $filename = "/tmp/bnw/".$old_key."modify_edge.txt"; $file = fopen($filename,'w'); -//fwrite($file,$json); -fwrite($file,$nnodes); -fwrite($file,"\n"); -fwrite($file,$node_labs); -fwrite($file,"\n"); -fwrite($file,$nedges); -fwrite($file,"\n"); -fwrite($file,$sources); -fwrite($file,"\n"); -fwrite($file,$targets); -fwrite($file,"\n"); -fwrite($file,$weights); -fwrite($file,"\n"); -fclose($file); +if ($file !== false) { + //fwrite($file,$json); + fwrite($file,$nnodes); + fwrite($file,"\n"); + fwrite($file,$node_labs); + fwrite($file,"\n"); + fwrite($file,$nedges); + fwrite($file,"\n"); + fwrite($file,$sources); + fwrite($file,"\n"); + fwrite($file,$targets); + fwrite($file,"\n"); + fwrite($file,$weights); + fwrite($file,"\n"); + fclose($file); +} shell_exec('./run_scripts/run_mod_edges '.$old_key.' '.$keyval); -- cgit 1.4.1