--- /dev/null
+! { dg-do run }
+
+module nml_47
+ type :: mt
+ character(len=2) :: c012345678901234567890123456789012345678901234567890123456789h(2) = (/"aa","bb"/)
+ end type mt
+ type :: bt
+ integer :: i(2) = (/1,2/)
+ type(mt) :: m(2)
+ end type bt
+end module nml_47
+
+program namelist_47
+ use nml_47
+ type(bt) :: x(2)
+ character(140) :: teststring
+ namelist /mynml/ x
+
+ teststring = " x(2)%m%c012345678901234567890123456789012345678901234567890123456789h(:)(2:2) = 'z','z',"
+ call writenml (teststring)
+ teststring = " x(2)%m(2)%c012345678901234567890123456789012345678901234567890123456789h(:)(2) = 'z','z',"
+ call writenml (teststring)
+ teststring = " x(2)%m(2)%c012345678901234567890123456789012345678901234567890123456789h(:)(:3) = 'z','z',"
+ call writenml (teststring)
+ teststring = " x(2)%m(2)%c012345678901234567890123456789012345678901234567890123456789h(1:2)(k:) = 'z','z',"
+ call writenml (teststring)
+
+contains
+
+subroutine writenml (astring)
+ character(140), intent(in) :: astring
+ character(300) :: errmessage
+ integer :: ierror
+
+ open (10, status="scratch", delim='apostrophe')
+ write (10, '(A)') "&MYNML"
+ write (10, '(A)') astring
+ write (10, '(A)') "/"
+ rewind (10)
+ read (10, nml = mynml, iostat=ierror, iomsg=errmessage)
+ if (ierror == 0) call abort
+ print '(a)', trim(errmessage)
+ close (10)
+
+end subroutine writenml
+
+end program namelist_47
+! { dg-output "Multiple sub-objects with non-zero rank in namelist object x(\n|\r\n|\r)" }
+! { dg-output "Missing colon in substring qualifier for namelist variable x%m%c012345678901234567890123456789012345678901234567890123456789h(\n|\r\n|\r)" }
+! { dg-output "Substring out of range for namelist variable x%m%c012345678901234567890123456789012345678901234567890123456789h(\n|\r\n|\r)" }
+! { dg-output "Bad character in substring qualifier for namelist variable x%m%c012345678901234567890123456789012345678901234567890123456789h(\n|\r\n|\r)" }
+! { dg-final { cleanup-modules "nml_47" } }
#define MAX_REPEAT 200000000
+#ifndef HAVE_SNPRINTF
+# undef snprintf
+# define snprintf(str, size, ...) sprintf (str, __VA_ARGS__)
+#endif
/* Save a character to a string buffer, enlarging it as necessary. */
static void nml_match_name (char *name, int len)
static int nml_query (st_parameter_dt *dtp)
static int nml_get_obj_data (st_parameter_dt *dtp,
- namelist_info **prev_nl, char *)
+ namelist_info **prev_nl, char *, size_t)
calls:
static void nml_untouch_nodes (st_parameter_dt *dtp)
static namelist_info * find_nml_node (st_parameter_dt *dtp,
array_loop_spec * ls, int rank, char *)
static void nml_touch_nodes (namelist_info * nl)
static int nml_read_obj (namelist_info *nl, index_type offset,
- namelist_info **prev_nl, char *,
+ namelist_info **prev_nl, char *, size_t,
index_type clow, index_type chigh)
calls:
-itself- */
static try
nml_read_obj (st_parameter_dt *dtp, namelist_info * nl, index_type offset,
namelist_info **pprev_nl, char *nml_err_msg,
- index_type clow, index_type chigh)
+ size_t nml_err_msg_size, index_type clow, index_type chigh)
{
namelist_info * cmp;
char * obj_name;
{
if (nml_read_obj (dtp, cmp, (index_type)(pdata - nl->mem_pos),
- pprev_nl, nml_err_msg, clow, chigh)
- == FAILURE)
+ pprev_nl, nml_err_msg, nml_err_msg_size,
+ clow, chigh) == FAILURE)
{
free_mem (obj_name);
return FAILURE;
goto incr_idx;
default:
- sprintf (nml_err_msg, "Bad type for namelist object %s",
- nl->var_name);
+ snprintf (nml_err_msg, nml_err_msg_size,
+ "Bad type for namelist object %s", nl->var_name);
internal_error (&dtp->common, nml_err_msg);
goto nml_err_ret;
}
if (dtp->u.p.repeat_count > 1)
{
- sprintf (nml_err_msg, "Repeat count too large for namelist object %s" ,
- nl->var_name );
- goto nml_err_ret;
+ snprintf (nml_err_msg, nml_err_msg_size,
+ "Repeat count too large for namelist object %s", nl->var_name);
+ goto nml_err_ret;
}
return SUCCESS;
static try
nml_get_obj_data (st_parameter_dt *dtp, namelist_info **pprev_nl,
- char *nml_err_msg)
+ char *nml_err_msg, size_t nml_err_msg_size)
{
char c;
namelist_info * nl;
namelist_info * root_nl = NULL;
int dim, parsed_rank;
int component_flag;
- char parse_err_msg[30];
index_type clow, chigh;
int non_zero_rank_count;
if (nl == NULL)
{
if (dtp->u.p.nml_read_error && *pprev_nl)
- sprintf (nml_err_msg, "Bad data for namelist object %s",
- (*pprev_nl)->var_name);
+ snprintf (nml_err_msg, nml_err_msg_size,
+ "Bad data for namelist object %s", (*pprev_nl)->var_name);
else
- sprintf (nml_err_msg, "Cannot match namelist object name %s",
- dtp->u.p.saved_string);
+ snprintf (nml_err_msg, nml_err_msg_size,
+ "Cannot match namelist object name %s",
+ dtp->u.p.saved_string);
goto nml_err_ret;
}
{
parsed_rank = 0;
if (nml_parse_qualifier (dtp, nl->dim, nl->ls, nl->var_rank,
- parse_err_msg, &parsed_rank) == FAILURE)
+ nml_err_msg, &parsed_rank) == FAILURE)
{
- sprintf (nml_err_msg, "%s for namelist variable %s",
- parse_err_msg, nl->var_name);
+ char *nml_err_msg_end = strchr (nml_err_msg, '\0');
+ snprintf (nml_err_msg_end,
+ nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
+ " for namelist variable %s", nl->var_name);
goto nml_err_ret;
}
{
if (nl->type != GFC_DTYPE_DERIVED)
{
- sprintf (nml_err_msg, "Attempt to get derived component for %s",
- nl->var_name);
+ snprintf (nml_err_msg, nml_err_msg_size,
+ "Attempt to get derived component for %s", nl->var_name);
goto nml_err_ret;
}
descriptor_dimension chd[1] = { {1, clow, nl->string_length} };
array_loop_spec ind[1] = { {1, clow, nl->string_length, 1} };
- if (nml_parse_qualifier (dtp, chd, ind, -1, parse_err_msg, &parsed_rank)
+ if (nml_parse_qualifier (dtp, chd, ind, -1, nml_err_msg, &parsed_rank)
== FAILURE)
{
- sprintf (nml_err_msg, "%s for namelist variable %s",
- parse_err_msg, nl->var_name);
+ char *nml_err_msg_end = strchr (nml_err_msg, '\0');
+ snprintf (nml_err_msg_end,
+ nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
+ " for namelist variable %s", nl->var_name);
goto nml_err_ret;
}
if (ind[0].step != 1)
{
- sprintf (nml_err_msg,
- "Step not allowed in substring qualifier"
- " for namelist object %s", nl->var_name);
+ snprintf (nml_err_msg, nml_err_msg_size,
+ "Step not allowed in substring qualifier"
+ " for namelist object %s", nl->var_name);
goto nml_err_ret;
}
if (c == '(')
{
- sprintf (nml_err_msg, "Qualifier for a scalar or non-character"
- " namelist object %s", nl->var_name);
+ snprintf (nml_err_msg, nml_err_msg_size,
+ "Qualifier for a scalar or non-character namelist object %s",
+ nl->var_name);
goto nml_err_ret;
}
/* Make sure there is no more than one non-zero rank object. */
if (non_zero_rank_count > 1)
{
- sprintf (nml_err_msg, "Multiple sub-objects with non-zero rank in"
- " namelist object %s", nl->var_name);
+ snprintf (nml_err_msg, nml_err_msg_size,
+ "Multiple sub-objects with non-zero rank in namelist object %s",
+ nl->var_name);
non_zero_rank_count = 0;
goto nml_err_ret;
}
if (c != '=')
{
- sprintf (nml_err_msg, "Equal sign must follow namelist object name %s",
- nl->var_name);
+ snprintf (nml_err_msg, nml_err_msg_size,
+ "Equal sign must follow namelist object name %s",
+ nl->var_name);
goto nml_err_ret;
}
- if (nml_read_obj (dtp, nl, 0, pprev_nl, nml_err_msg, clow, chigh) == FAILURE)
+ if (nml_read_obj (dtp, nl, 0, pprev_nl, nml_err_msg, nml_err_msg_size,
+ clow, chigh) == FAILURE)
goto nml_err_ret;
return SUCCESS;
{
char c;
jmp_buf eof_jump;
- char nml_err_msg[100];
+ char nml_err_msg[200];
/* Pointer to the previously read object, in case attempt is made to read
new object name. Should this fail, error message can give previous
name. */
while (!dtp->u.p.input_complete)
{
- if (nml_get_obj_data (dtp, &prev_nl, nml_err_msg) == FAILURE)
+ if (nml_get_obj_data (dtp, &prev_nl, nml_err_msg, sizeof nml_err_msg)
+ == FAILURE)
{
gfc_unit *u;