Fortran: Fix issue with using snprintf function.

The previous patch used snprintf to set the message
string. The message string is not a formatted string
and the snprintf will interpret '%' related characters
as format specifiers when there are no associated
output variables. A segfault ensues.

This change replaces snprintf with a fortran string copy
function and null terminates the message string.

	PR libfortran/105456

libgfortran/ChangeLog:

	* io/list_read.c (list_formatted_read_scalar): Use fstrcpy
	from libgfortran/runtime/string.c to replace snprintf.
	(nml_read_obj): Likewise.
	* io/transfer.c (unformatted_read): Likewise.
	(unformatted_write): Likewise.
	(formatted_transfer_scalar_read): Likewise.
	(formatted_transfer_scalar_write): Likewise.
	* io/write.c (list_formatted_write_scalar): Likewise.
	(nml_write_obj): Likewise.

gcc/testsuite/ChangeLog:

	* gfortran.dg/pr105456.f90: Revise using '%' characters
	in users error message.
This commit is contained in:
Jerry DeLisle 2024-03-06 19:46:04 -08:00
parent 8b483cd552
commit 03932d3203
4 changed files with 26 additions and 18 deletions

View File

@ -19,7 +19,7 @@ contains
character :: ch character :: ch
read (unit,fmt='(A1)', advance="no", iostat=piostat, iomsg=piomsg) ch read (unit,fmt='(A1)', advance="no", iostat=piostat, iomsg=piomsg) ch
piostat = 42 piostat = 42
piomsg="The users message" piomsg="The users message containing % and %% and %s and other stuff"
dtv%ch = ch dtv%ch = ch
end subroutine read_formatted end subroutine read_formatted
end module sk1 end module sk1
@ -35,4 +35,4 @@ program skip1
write (*,'(10(A))') "Read: '",x%ch,"'" write (*,'(10(A))') "Read: '",x%ch,"'"
end program skip1 end program skip1
! { dg-output ".*(unit = 10, file = .*)" } ! { dg-output ".*(unit = 10, file = .*)" }
! { dg-output "Fortran runtime error: The users message" } ! { dg-output "Fortran runtime error: The users message containing % and %% and %s and other stuff" }

View File

@ -2268,9 +2268,10 @@ list_formatted_read_scalar (st_parameter_dt *dtp, bt type, void *p,
!(dtp->common.flags & IOPARM_HAS_IOSTAT)) !(dtp->common.flags & IOPARM_HAS_IOSTAT))
{ {
char message[IOMSG_LEN + 1]; char message[IOMSG_LEN + 1];
child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg) + 1; child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg);
free_line (dtp); free_line (dtp);
snprintf (message, child_iomsg_len, child_iomsg); fstrcpy (message, child_iomsg_len, child_iomsg, child_iomsg_len);
message[child_iomsg_len] = '\0';
generate_error (&dtp->common, dtp->u.p.child_saved_iostat, generate_error (&dtp->common, dtp->u.p.child_saved_iostat,
message); message);
} }
@ -3082,8 +3083,9 @@ nml_read_obj (st_parameter_dt *dtp, namelist_info *nl, index_type offset,
!(dtp->common.flags & IOPARM_HAS_IOSTAT)) !(dtp->common.flags & IOPARM_HAS_IOSTAT))
{ {
char message[IOMSG_LEN + 1]; char message[IOMSG_LEN + 1];
child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg) + 1; child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg);
snprintf (message, child_iomsg_len, child_iomsg); fstrcpy (message, child_iomsg_len, child_iomsg, child_iomsg_len);
message[child_iomsg_len] = '\0';
generate_error (&dtp->common, dtp->u.p.child_saved_iostat, generate_error (&dtp->common, dtp->u.p.child_saved_iostat,
message); message);
goto nml_err_ret; goto nml_err_ret;

View File

@ -1128,8 +1128,9 @@ unformatted_read (st_parameter_dt *dtp, bt type,
!(dtp->common.flags & IOPARM_HAS_IOSTAT)) !(dtp->common.flags & IOPARM_HAS_IOSTAT))
{ {
char message[IOMSG_LEN + 1]; char message[IOMSG_LEN + 1];
child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg) + 1; child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg);
snprintf (message, child_iomsg_len, child_iomsg); fstrcpy (message, child_iomsg_len, child_iomsg, child_iomsg_len);
message[child_iomsg_len] = '\0';
generate_error (&dtp->common, dtp->u.p.child_saved_iostat, generate_error (&dtp->common, dtp->u.p.child_saved_iostat,
message); message);
} }
@ -1271,8 +1272,9 @@ unformatted_write (st_parameter_dt *dtp, bt type,
!(dtp->common.flags & IOPARM_HAS_IOSTAT)) !(dtp->common.flags & IOPARM_HAS_IOSTAT))
{ {
char message[IOMSG_LEN + 1]; char message[IOMSG_LEN + 1];
child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg) + 1; child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg);
snprintf (message, child_iomsg_len, child_iomsg); fstrcpy (message, child_iomsg_len, child_iomsg, child_iomsg_len);
message[child_iomsg_len] = '\0';
generate_error (&dtp->common, dtp->u.p.child_saved_iostat, generate_error (&dtp->common, dtp->u.p.child_saved_iostat,
message); message);
} }
@ -1763,8 +1765,9 @@ formatted_transfer_scalar_read (st_parameter_dt *dtp, bt type, void *p, int kind
!(dtp->common.flags & IOPARM_HAS_IOSTAT)) !(dtp->common.flags & IOPARM_HAS_IOSTAT))
{ {
char message[IOMSG_LEN + 1]; char message[IOMSG_LEN + 1];
child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg) + 1; child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg);
snprintf (message, child_iomsg_len, child_iomsg); fstrcpy (message, child_iomsg_len, child_iomsg, child_iomsg_len);
message[child_iomsg_len] = '\0';
generate_error (&dtp->common, dtp->u.p.child_saved_iostat, generate_error (&dtp->common, dtp->u.p.child_saved_iostat,
message); message);
} }
@ -2259,8 +2262,9 @@ formatted_transfer_scalar_write (st_parameter_dt *dtp, bt type, void *p, int kin
!(dtp->common.flags & IOPARM_HAS_IOSTAT)) !(dtp->common.flags & IOPARM_HAS_IOSTAT))
{ {
char message[IOMSG_LEN + 1]; char message[IOMSG_LEN + 1];
child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg) + 1; child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg);
snprintf (message, child_iomsg_len, child_iomsg); fstrcpy (message, child_iomsg_len, child_iomsg, child_iomsg_len);
message[child_iomsg_len] = '\0';
generate_error (&dtp->common, dtp->u.p.child_saved_iostat, generate_error (&dtp->common, dtp->u.p.child_saved_iostat,
message); message);
} }

View File

@ -1999,8 +1999,9 @@ list_formatted_write_scalar (st_parameter_dt *dtp, bt type, void *p, int kind,
!(dtp->common.flags & IOPARM_HAS_IOSTAT)) !(dtp->common.flags & IOPARM_HAS_IOSTAT))
{ {
char message[IOMSG_LEN + 1]; char message[IOMSG_LEN + 1];
child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg) + 1; child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg);
snprintf (message, child_iomsg_len, child_iomsg); fstrcpy (message, child_iomsg_len, child_iomsg, child_iomsg_len);
message[child_iomsg_len] = '\0';
generate_error (&dtp->common, dtp->u.p.child_saved_iostat, generate_error (&dtp->common, dtp->u.p.child_saved_iostat,
message); message);
} }
@ -2352,8 +2353,9 @@ nml_write_obj (st_parameter_dt *dtp, namelist_info *obj, index_type offset,
char message[IOMSG_LEN + 1]; char message[IOMSG_LEN + 1];
/* Trim trailing spaces from the message. */ /* Trim trailing spaces from the message. */
child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg) + 1; child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg);
snprintf (message, child_iomsg_len, child_iomsg); fstrcpy (message, child_iomsg_len, child_iomsg, child_iomsg_len);
message[child_iomsg_len] = '\0';
generate_error (&dtp->common, dtp->u.p.child_saved_iostat, generate_error (&dtp->common, dtp->u.p.child_saved_iostat,
message); message);
} }