https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77505
Elizebeth Punnoose <elizebethp at gmail dot com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |elizebethp at gmail dot com --- Comment #3 from Elizebeth Punnoose <elizebethp at gmail dot com> --- The segmentation fault is reproducible on x86 & ARM64, with the top of trunk. I have now added a check in trans-array.c:trans_array_constructor() to check for character lengths less than zero, for character arrays. If the length is found to be negative, a warning message is printed that the length will be considered as LEN=0, and the length is set to zero. Index: trans-array.c =================================================================== --- trans-array.c (revision 241960) +++ trans-array.c (working copy) @@ -2226,6 +2226,8 @@ gfc_ss_info *ss_info; gfc_expr *expr; gfc_ss *s; + tree neg_len; + char *msg; /* Save the old values for nested checking. */ old_first_len = first_len; @@ -2271,6 +2273,19 @@ gfc_conv_expr_type (&length_se, expr->ts.u.cl->length, gfc_charlen_type_node); ss_info->string_length = length_se.expr; + + /* Check if the length is negative, if so print a warning + and consider the length as zero. */ + neg_len = fold_build2_loc (input_location, LT_EXPR, + boolean_type_node, + ss_info->string_length, build_int_cst (gfc_charlen_type_node, 0)); + msg = xasprintf ("Negative character length will be treated as LEN=0"); + gfc_trans_runtime_check (false, true, neg_len, &length_se.pre, where, msg); + free(msg); + ss_info->string_length = fold_build3_loc (input_location, COND_EXPR, gfc_charlen_type_node, + neg_len, gfc_index_zero_node, ss_info->string_length); + ss_info->string_length = gfc_evaluate_now (ss_info->string_length, &length_se.pre); + gfc_add_block_to_block (&outer_loop->pre, &length_se.pre); gfc_add_block_to_block (&outer_loop->post, &length_se.post); } The test suite has run clean with the above changes merged on top of trunk. Kindly review and provide your suggestions if any. I will post the patch on the gcc-patches mail list, if there is no objection.