public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [Patch] Fortran: Fix 'name' bound size [PR99688]
@ 2021-03-21 16:08 Tobias Burnus
  2021-03-22  0:25 ` Jerry DeLisle
  0 siblings, 1 reply; 2+ messages in thread
From: Tobias Burnus @ 2021-03-21 16:08 UTC (permalink / raw)
  To: gcc-patches, fortran, Harald Anlauf

[-- Attachment #1: Type: text/plain, Size: 1058 bytes --]

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);


[-- Attachment #2: namelen.diff --]
[-- Type: text/x-patch, Size: 1722 bytes --]

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;

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Patch] Fortran: Fix 'name' bound size [PR99688]
  2021-03-21 16:08 [Patch] Fortran: Fix 'name' bound size [PR99688] Tobias Burnus
@ 2021-03-22  0:25 ` Jerry DeLisle
  0 siblings, 0 replies; 2+ messages in thread
From: Jerry DeLisle @ 2021-03-22  0:25 UTC (permalink / raw)
  To: Tobias Burnus, gcc-patches, fortran, Harald Anlauf

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);
>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-03-22  0:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-21 16:08 [Patch] Fortran: Fix 'name' bound size [PR99688] Tobias Burnus
2021-03-22  0:25 ` Jerry DeLisle

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).