[Jython-checkins] jython: Fix race condition in PyStringMap keys method [GH-156]

jeff.allen jython-checkins at python.org
Sun Feb 23 16:36:23 EST 2020


https://hg.python.org/jython/rev/57e47c817d26
changeset:   8331:57e47c817d26
user:        Peter Holloway <holloway.p.r at gmail.com>
date:        Sun Feb 23 16:09:30 2020 +0000
summary:
  Fix race condition in PyStringMap keys method [GH-156]

If the map was modified during a call to keys(), the keyset would change
size and cause an ArrayIndexOutOfBoundsException. This creates a copy of
the keyset and uses that throughout the method.

files:
  NEWS                                 |  1 +
  src/org/python/core/PyStringMap.java |  5 +++--
  2 files changed, 4 insertions(+), 2 deletions(-)


diff --git a/NEWS b/NEWS
--- a/NEWS
+++ b/NEWS
@@ -8,6 +8,7 @@
 
 Jython 2.7.2b4
   Bugs fixed
+    - [ GH-156 ] Race condition in PyStringMap keys method
     - [ 2862 ] Jython fails on Linux for normal user when installed by root
 
 Jython 2.7.2b3
diff --git a/src/org/python/core/PyStringMap.java b/src/org/python/core/PyStringMap.java
--- a/src/org/python/core/PyStringMap.java
+++ b/src/org/python/core/PyStringMap.java
@@ -637,9 +637,10 @@
 
     @ExposedMethod(doc = BuiltinDocs.dict_keys_doc)
     final PyList stringmap_keys() {
-        PyObject[] keyArray = new PyObject[table.size()];
+        Object[] keys = table.keySet().toArray();
+        PyObject[] keyArray = new PyObject[keys.length];
         int i = 0;
-        for (Object key : table.keySet()) {
+        for (Object key : keys) {
             keyArray[i++] = keyToPy(key);
         }
         return new PyList(keyArray);

-- 
Repository URL: https://hg.python.org/jython


More information about the Jython-checkins mailing list