]> granicus.if.org Git - python/commitdiff
SF bug 515943: searching for data with \0 in mmap.
authorTim Peters <tim.peters@gmail.com>
Fri, 8 Mar 2002 05:43:32 +0000 (05:43 +0000)
committerTim Peters <tim.peters@gmail.com>
Fri, 8 Mar 2002 05:43:32 +0000 (05:43 +0000)
mmap_find_method():  this obtained the string to find via s#, but it
ignored its length, acting as if it were \0-terminated instead.

Someone please run on Linux too (the extended test_mmap works on Windows).

Bugfix candidate.

Lib/test/test_mmap.py
Modules/mmapmodule.c

index f3d1538f7efdb8565ef33e988ea039db42de894d..0f34758925e570c5714931e7ae5f43317e6fff57 100644 (file)
@@ -244,6 +244,31 @@ def test_both():
         except OSError:
             pass
 
+    # Do a tougher .find() test.  SF bug 515943 pointed out that, in 2.2,
+    # searching for data with embedded \0 bytes didn't work.
+    f = open(TESTFN, 'w+')
+
+    try:    # unlink TESTFN no matter what
+        data = 'aabaac\x00deef\x00\x00aa\x00'
+        n = len(data)
+        f.write(data)
+        m = mmap.mmap(f.fileno(), n)
+        f.close()
+
+        for start in range(n+1):
+            for finish in range(start, n+1):
+                slice = data[start : finish]
+                vereq(m.find(slice), data.find(slice))
+                vereq(m.find(slice + 'x'), -1)
+
+    finally:
+        try:
+            os.unlink(TESTFN)
+        except OSError:
+            pass
+
+
+
     print ' Test passed'
 
 test_both()
index cd1391453ce78e057898f100d9dd8a8afeea7a7f..3164a274076a71310c3079a11c298f2a2461bb87 100644 (file)
@@ -251,20 +251,16 @@ mmap_find_method(mmap_object *self,
                        start = 0;
                 else if ((size_t)start > self->size)
                        start = self->size;
-                p = self->data + start;
 
-               while (p < e) {
-                       char *s = p;
-                       char *n = needle;
-                       while ((s<e) && (*n) && !(*s-*n)) {
-                               s++, n++;
-                       }
-                       if (!*n) {
+               for (p = self->data + start; p + len <= e; ++p) {
+                       int i;
+                       for (i = 0; i < len && needle[i] == p[i]; ++i)
+                               /* nothing */;
+                       if (i == len) {
                                return Py_BuildValue (
                                        "l",
                                        (long) (p - self->data));
                        }
-                       p++;
                }
                return Py_BuildValue ("l", (long) -1);
        }