[flang] Fix negative unit number hashing
authorpeter klausler <pklausler@nvidia.com>
Wed, 8 Jul 2020 20:09:53 +0000 (13:09 -0700)
committerpeter klausler <pklausler@nvidia.com>
Thu, 9 Jul 2020 00:59:00 +0000 (17:59 -0700)
Ensure that external unit number hashing produces a valid
index for a negative unit number, viz. a NEWUNIT=.

Reviewed By: sscalpone

Differential Revision: https://reviews.llvm.org/D83428

flang/runtime/unit-map.h

index 286cb06..9efb269 100644 (file)
@@ -15,6 +15,7 @@
 #include "lock.h"
 #include "memory.h"
 #include "unit.h"
+#include <cstdlib>
 
 namespace Fortran::runtime::io {
 
@@ -59,7 +60,7 @@ private:
   };
 
   static constexpr int buckets_{1031}; // must be prime
-  int Hash(int n) { return n % buckets_; }
+  int Hash(int n) { return std::abs(n) % buckets_; }
 
   ExternalFileUnit *Find(int n) {
     Chain *previous{nullptr};