create_character_initializer (gfc_expr *init, gfc_typespec *ts,
gfc_ref *ref, gfc_expr *rvalue)
{
- int len, start, end;
+ int len, start, end, tlen;
gfc_char_t *dest;
bool alloced_init = false;
else
len = rvalue->value.character.length;
- if (len > end - start)
+ tlen = end - start;
+ if (len > tlen)
{
- gfc_warning_now (0, "Initialization string starting at %L was "
- "truncated to fit the variable (%d/%d)",
- &rvalue->where, end - start, len);
- len = end - start;
+ if (tlen < 0)
+ {
+ gfc_warning_now (0, "Unused initialization string at %L because "
+ "variable has zero length", &rvalue->where);
+ len = 0;
+ }
+ else
+ {
+ gfc_warning_now (0, "Initialization string at %L was truncated to "
+ "fit the variable (%d/%d)", &rvalue->where,
+ tlen, len);
+ len = tlen;
+ }
}
if (rvalue->ts.type == BT_HOLLERITH)
len * sizeof (gfc_char_t));
/* Pad with spaces. Substrings will already be blanked. */
- if (len < end - start && ref == NULL)
+ if (len < tlen && ref == NULL)
gfc_wide_memset (&dest[start + len], ' ', end - (start + len));
if (rvalue->ts.type == BT_HOLLERITH)
--- /dev/null
+! { dg-do compile }
+! PR fortran/67939
+! Original code by Gerhard Steinmetz
+! gerhard dot steinmetz dot fortran at t-online dot de
+!
+program p
+ character(100) :: x
+ data x(998:99) /'ab'/ ! { dg-warning "Unused initialization string" }
+ call a
+end
+
+subroutine a
+ character(2) :: x
+ data x(:-1) /'ab'/ ! { dg-warning "Unused initialization string" }
+end subroutine a
+
+subroutine b
+ character(8) :: x
+ data x(3:1) /'abc'/ ! { dg-warning "Unused initialization string" }
+end subroutine b
+