]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libgfortran/intrinsics/string_intrinsics_inc.c
Update copyright years.
[thirdparty/gcc.git] / libgfortran / intrinsics / string_intrinsics_inc.c
index f514f4c6a3ee40f45d6ff900a314c7833c18c264..61492fa0f1eb0e0835ba1bde456d102741605436 100644 (file)
@@ -1,5 +1,5 @@
 /* String intrinsics helper functions.
-   Copyright (C) 2002-2017 Free Software Foundation, Inc.
+   Copyright (C) 2002-2020 Free Software Foundation, Inc.
 
 This file is part of the GNU Fortran runtime library (libgfortran).
 
@@ -87,6 +87,14 @@ compare_string (gfc_charlen_type len1, const CHARTYPE *s1,
   gfc_charlen_type len;
   int res;
 
+  /* Placate the sanitizer.  */
+  if (!s1 && !s2)
+    return 0;
+  if (!s1)
+    return -1;
+  if (!s2)
+    return 1;
+
   res = MEMCMP (s1, s2, ((len1 < len2) ? len1 : len2));
   if (res != 0)
     return res;
@@ -177,23 +185,25 @@ string_trim (gfc_charlen_type *len, CHARTYPE **dest, gfc_charlen_type slen,
 gfc_charlen_type
 string_len_trim (gfc_charlen_type len, const CHARTYPE *s)
 {
-  const gfc_charlen_type long_len = (gfc_charlen_type) sizeof (unsigned long);
-  gfc_charlen_type i;
+  if (len <= 0)
+    return 0;
+
+  const size_t long_len = sizeof (unsigned long);
 
-  i = len - 1;
+  size_t i = len - 1;
 
   /* If we've got the standard (KIND=1) character type, we scan the string in
      long word chunks to speed it up (until a long word is hit that does not
      consist of ' 's).  */
   if (sizeof (CHARTYPE) == 1 && i >= long_len)
     {
-      int starting;
+      size_t starting;
       unsigned long blank_longword;
 
       /* Handle the first characters until we're aligned on a long word
         boundary.  Actually, s + i + 1 must be properly aligned, because
         s + i will be the last byte of a long word read.  */
-      starting = ((unsigned long)
+      starting = (
 #ifdef __INTPTR_TYPE__
                  (__INTPTR_TYPE__)
 #endif
@@ -224,14 +234,15 @@ string_len_trim (gfc_charlen_type len, const CHARTYPE *s)
              break;
            }
        }
-
-      /* Now continue for the last characters with naive approach below.  */
-      assert (i >= 0);
     }
 
   /* Simply look for the first non-blank character.  */
-  while (i >= 0 && s[i] == ' ')
-    --i;
+  while (s[i] == ' ')
+    {
+      if (i == 0)
+       return 0;
+      --i;
+    }
   return i + 1;
 }
 
@@ -327,12 +338,12 @@ string_scan (gfc_charlen_type slen, const CHARTYPE *str,
 
   if (back)
     {
-      for (i = slen - 1; i >= 0; i--)
+      for (i = slen; i != 0; i--)
        {
          for (j = 0; j < setlen; j++)
            {
-             if (str[i] == set[j])
-               return (i + 1);
+             if (str[i - 1] == set[j])
+               return i;
            }
        }
     }