Dear all, the attached, simple and obvious patch fixes the invalid read detected with valgrind for compiling the testcase in the PR.
The problem was in the logic checking for the prefix "__", which did not properly handle the case when the name to check was empty, which caused the invalid read. However, looking twice at the logic seems to reveal that it would handle the case of only one '_' in the first two positions of the name different from what the comment in the code said: only internal symbols with prefix "__" were meant. So the fix should be obvious. Or do I miss something? Regtested on x86_64-pc-linux-gnu. OK for mainline? Thanks, Harald
From 83e75d259dab4d84773571af9218b33b60cd67bc Mon Sep 17 00:00:00 2001 From: Harald Anlauf <[email protected]> Date: Fri, 23 Oct 2026 21:27:01 +0200 Subject: [PATCH] fortran: Fix invalid read with 'use, only:' [PR128067] PR fortran/128067 gcc/fortran/ChangeLog: * module.cc (load_needed): Prevent scanning of empty name for prefix "__". --- gcc/fortran/module.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gcc/fortran/module.cc b/gcc/fortran/module.cc index 70d231dcf6a..634af39659c 100644 --- a/gcc/fortran/module.cc +++ b/gcc/fortran/module.cc @@ -5685,7 +5685,9 @@ load_needed (pointer_info *p) but not used warnings; don't mark internal symbols such as __vtab, __def_init etc. Only mark them if they have been explicitly loaded. */ - if (only_flag && sym->name[0] != '_' && sym->name[1] != '_') + if (only_flag + && sym->name[0] != '\0' + && !(sym->name[0] == '_' && sym->name[1] == '_')) { gfc_use_rename *u; -- 2.51.0
