Hi Harald,

On 27.03.22 21:44, Harald Anlauf via Fortran wrote:
when assigning character pointers, we have a check for same length,
which however does not trigger for character pointers within a
structure constructor.

The attached patch extends the character checks slightly to fix
this loophole.  I've verified that NAG and Crayftn behave similarly,
while Intel does not detect the length difference.

Regtested on x86_64-pc-linux-gnu.

OK for mainline?

Thanks for the patch! LGTM and I think GCC 12 is still okay.

However, I have a nit:

--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -1375,11 +1375,22 @@ resolve_structure_cons (gfc_expr *expr, int init)
...
+           long len_a, len_b;
+           len_a = mpz_get_si (comp->ts.u.cl->length->value.integer);
+           len_b = mpz_get_si (cons->expr->ts.u.cl->length->value.integer);
+           gfc_error ("Unequal character lengths (%ld/%ld) for pointer "
+                      "component %qs in constructor at %L",
+                      len_a, len_b, comp->name, &cons->expr->where);

'long' might be int32_t instead of int64_t (e.g. on Windows, I think both
MinGW32 and MinGW64, but I am not quite sure). Thus, I wonder whether it
makes more sense to use:

  HOST_WIDE_INT, gfc_mpz_get_hwi() and '%wd'

I note that '%wd' (and '%lld') is only supported since last August
(commit https://gcc.gnu.org/r12-3044-g1b507b1e3c5 ), but now that it is,
I think we should use it at places where the value can be larger than INT_MAX.

I think at some point, we should also check the rest of the code and
change those mpz_get_si to gfc_mpz_get_hwi which can exceed INT_MAX.
Likewise, some of the %ld/%lu or %lld/%llu code should be also converted to 
%wd/%wu.

Tobias

PS: For using HWI with 'sprintf' instead of diagnostic's error/warning,
HOST_WIDE_INT_PRINT_DEC exists and has to be used.

-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 
München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas 
Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht 
München, HRB 106955

Reply via email to