diff mbox series

Fortran: Fix 'name' bound size [PR99688]

Message ID a0b37568-f8fa-bee7-6489-1e171d95a942@net-b.de
State New
Headers show
Series Fortran: Fix 'name' bound size [PR99688] | expand

Commit Message

Tobias Burnus March 21, 2021, 4:08 p.m. UTC
The gfc_match_select_rank issue showed up in the testsuite
and was found by Martin's asan-bootstrapped GCC. First
analysis by Harald – thanks to both!

However, I think the other variables I fixed were also
prone to overflows; see below for its usage.

OK for mainline? Other branches?

Tobias

PS: Other pending Fortran patches – please review!
[Patch] Fortran: Fix intrinsic null() handling [PR99651]
[Patch] Fortran: Fix func decl mismatch [PR93660]

PPS: Usage of 'name' in the patched functions:

resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
   char name[GFC_MAX_SYMBOL_LEN];
...
         sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);


select_intrinsic_set_tmp (gfc_typespec *ts)
{
   char name[GFC_MAX_SYMBOL_LEN];
...
         sprintf (name, "__tmp_class_%s", ts->u.derived->name);


gfc_match_select_type (void)
...
   char name[GFC_MAX_SYMBOL_LEN];
...
   m = gfc_match (" %n => %e", name, &expr2);

gfc_match_select_rank (void)
...
   char name[GFC_MAX_SYMBOL_LEN];
...
   m = gfc_match (" %n => %e", name, &expr2);

Comments

Jerry DeLisle March 22, 2021, 12:25 a.m. UTC | #1
Hi Tobias and others,

Reembering some of my own history, we always have used a +1 on 
'LENGTH_MAX' items to allow the C null termination on strings or buffers 
for obvious reasons.

Certainly OK for trunk and backports.

Regards,

Jerry

On 3/21/21 9:08 AM, Tobias Burnus wrote:
> The gfc_match_select_rank issue showed up in the testsuite
> and was found by Martin's asan-bootstrapped GCC. First
> analysis by Harald – thanks to both!
>
> However, I think the other variables I fixed were also
> prone to overflows; see below for its usage.
>
> OK for mainline? Other branches?
>
> Tobias
>
> PS: Other pending Fortran patches – please review!
> [Patch] Fortran: Fix intrinsic null() handling [PR99651]
> [Patch] Fortran: Fix func decl mismatch [PR93660]
>
> PPS: Usage of 'name' in the patched functions:
>
> resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
>   char name[GFC_MAX_SYMBOL_LEN];
> ...
>         sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
>
>
> select_intrinsic_set_tmp (gfc_typespec *ts)
> {
>   char name[GFC_MAX_SYMBOL_LEN];
> ...
>         sprintf (name, "__tmp_class_%s", ts->u.derived->name);
>
>
> gfc_match_select_type (void)
> ...
>   char name[GFC_MAX_SYMBOL_LEN];
> ...
>   m = gfc_match (" %n => %e", name, &expr2);
>
> gfc_match_select_rank (void)
> ...
>   char name[GFC_MAX_SYMBOL_LEN];
> ...
>   m = gfc_match (" %n => %e", name, &expr2);
>
diff mbox series

Patch

Fortran: Fix 'name' bound size [PR99688]

gcc/fortran/ChangeLog:

	PR fortran/99688
	* match.c (select_type_set_tmp, gfc_match_select_type,
	(gfc_match_select_rank): Fix 'name' buffersize to avoid out of bounds.
	* resolve.c (resolve_select_type): Likewise.

/lhome/tburnus/repos/gcc/gcc/fortran
diff --git a/gcc/fortran/match.c b/gcc/fortran/match.c
index 4d5890fd523..29462013038 100644
--- a/gcc/fortran/match.c
+++ b/gcc/fortran/match.c
@@ -6330,7 +6332,7 @@  select_intrinsic_set_tmp (gfc_typespec *ts)
 static void
 select_type_set_tmp (gfc_typespec *ts)
 {
-  char name[GFC_MAX_SYMBOL_LEN];
+  char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
   gfc_symtree *tmp = NULL;
   gfc_symbol *selector = select_type_stack->selector;
   gfc_symbol *sym;
@@ -6409,7 +6411,7 @@  gfc_match_select_type (void)
 {
   gfc_expr *expr1, *expr2 = NULL;
   match m;
-  char name[GFC_MAX_SYMBOL_LEN];
+  char name[GFC_MAX_SYMBOL_LEN + 1];
   bool class_array;
   gfc_symbol *sym;
   gfc_namespace *ns = gfc_current_ns;
@@ -6634,7 +6636,7 @@  gfc_match_select_rank (void)
 {
   gfc_expr *expr1, *expr2 = NULL;
   match m;
-  char name[GFC_MAX_SYMBOL_LEN];
+  char name[GFC_MAX_SYMBOL_LEN + 1];
   gfc_symbol *sym, *sym2;
   gfc_namespace *ns = gfc_current_ns;
   gfc_array_spec *as = NULL;
diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
index 32015c21efc..715fecd4b3a 100644
--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -9246,7 +9246,7 @@  resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
   gfc_code *class_is = NULL, *default_case = NULL;
   gfc_case *c;
   gfc_symtree *st;
-  char name[GFC_MAX_SYMBOL_LEN];
+  char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
   gfc_namespace *ns;
   int error = 0;
   int rank = 0;