[flang-commits] [PATCH] D133693: [flang][runtime] ensure character compares to blank are unsigned

Jean Perier via Phabricator via flang-commits flang-commits at lists.llvm.org
Tue Sep 13 01:43:21 PDT 2022


This revision was automatically updated to reflect the committed changes.
Closed by commit rG2694234c2982: [flang][runtime] ensure character compares to blank are unsigned (authored by jeanPerier).

Changed prior to commit:
  https://reviews.llvm.org/D133693?vs=459440&id=459682#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133693/new/

https://reviews.llvm.org/D133693

Files:
  flang/runtime/character.cpp
  flang/unittests/Runtime/CharacterTest.cpp


Index: flang/unittests/Runtime/CharacterTest.cpp
===================================================================
--- flang/unittests/Runtime/CharacterTest.cpp
+++ flang/unittests/Runtime/CharacterTest.cpp
@@ -171,6 +171,8 @@
         std::make_tuple("abc", "def", 3, 3, -1),
         std::make_tuple("ab ", "abc", 3, 2, 0),
         std::make_tuple("abc", "abc", 2, 3, -1),
+        std::make_tuple("ab\xff", "ab ", 3, 2, 1),
+        std::make_tuple("ab ", "ab\xff", 2, 3, -1),
     },
     {
         std::make_tuple(u"abc", u"abc", 3, 3, 0),
Index: flang/runtime/character.cpp
===================================================================
--- flang/runtime/character.cpp
+++ flang/runtime/character.cpp
@@ -20,11 +20,14 @@
 
 template <typename CHAR>
 inline int CompareToBlankPadding(const CHAR *x, std::size_t chars) {
+  using UNSIGNED_CHAR = std::make_unsigned_t<CHAR>;
+  const auto blank{static_cast<UNSIGNED_CHAR>(' ')};
   for (; chars-- > 0; ++x) {
-    if (*x < ' ') {
+    const UNSIGNED_CHAR ux{*reinterpret_cast<const UNSIGNED_CHAR *>(x)};
+    if (ux < blank) {
       return -1;
     }
-    if (*x > ' ') {
+    if (ux > blank) {
       return 1;
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133693.459682.patch
Type: text/x-patch
Size: 1187 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220913/8f75beed/attachment.bin>


More information about the flang-commits mailing list