On 7/27/2025 5:19 PM, Mikael Morin wrote:
+gfc_charlen_type
+string_split (gfc_charlen_type stringlen, const CHARTYPE *string,
+          gfc_charlen_type setlen, const CHARTYPE *set,
+          gfc_charlen_type pos, GFC_LOGICAL_4 back)
+{
+  gfc_charlen_type i, j;
+
+  if (!back)
+    {
+      if (pos > stringlen)
+    runtime_error ("If BACK is present with the value false, the value of "
+               "POS shall be in the range [0, LEN (STRING)]");
+
The condition doesn't check pos >= 0; I think the case pos < 0 doesn't work.

The variable pos is an unsigned type, so checking for < 0 is unnecessary. (I was not initially aware of this, but the compiler warning alerted me.)

Mmmh, good point.  But POS is a user variable, so a signed type originally.
Can you check the following:

   integer(kind=1) :: my_pos = -1
   character(len=300) :: my_str = ""
   call split(my_str, " ", my_pos)

Is it rejected at runtime?
I expect the unsigned conversion to convert -1 to 255 and then the call would be (wrongly) accepted.


Yes, it's rejected at runtime. The trick lies in how the pos is handled: it's first converted to gfc_charlen_type (an unsigned long long). So, even if kind=1 and the input is -1, it wraps around to a very large positive value (specifically, ULLONG_MAX - 1). This value far exceeds 300, leading to a runtime error.

+  pos_for_call = fold_convert (gfc_charlen_type_node, pos);

Thanks,
Yuao

Reply via email to