https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118571
--- Comment #7 from Jerry DeLisle <jvdelisle at gcc dot gnu.org> --- If no width is specified w_len comes in as zero so we have to handle that case. diff --git a/libgfortran/io/write.c b/libgfortran/io/write.c index 54312bf67e9..15a0dd5c3e9 100644 --- a/libgfortran/io/write.c +++ b/libgfortran/io/write.c @@ -178,7 +178,10 @@ write_utf8_char4 (st_parameter_dt *dtp, gfc_char4_t *source, } /* Now process the remaining characters, one at a time. */ - for (j = k; j < src_len; j++) + + w_len = w_len == 0 ? src_len : w_len; /* No width specified. */ + + for (j = k; j < (w_len < src_len ? w_len : src_len); j++) { c = source[j]; if (c < 0x80) There is probably a more readable way to do that logic. This patch illustrates the point. I will clean this all up and submit for approval.