]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix the .find() method for memory maps.
authorGreg Stein <gstein@lyra.org>
Mon, 14 May 2001 09:32:26 +0000 (09:32 +0000)
committerGreg Stein <gstein@lyra.org>
Mon, 14 May 2001 09:32:26 +0000 (09:32 +0000)
1) it didn't obey the "start" parameter (and when it does, we must validate
   the value)
2) the return value needs to be an absolute index, rather than relative to
   some arbitrary point in the file

(checking CVS, it appears this method never worked; these changes bring it
 into line with typical .find() behavior)

Modules/mmapmodule.c

index dd7ff6dcf66ed9ae2df23ed64feb5e8b7980ab0f..acf85a40388f6abe36a01be20fc044d648c63de8 100644 (file)
@@ -232,8 +232,17 @@ mmap_find_method(mmap_object *self,
        if (!PyArg_ParseTuple (args, "s#|i:find", &needle, &len, &start)) {
                return NULL;
        } else {
-               char *p = self->data+self->pos;
-               char *e = self->data+self->size;
+               char *p;
+               char *e = self->data + self->size;
+
+                if (start < 0)
+                    start += self->size;
+                if (start < 0)
+                    start = 0;
+                else if (start > self->size)
+                    start = self->size;
+                p = self->data + start;
+
                while (p < e) {
                        char *s = p;
                        char *n = needle;
@@ -243,7 +252,7 @@ mmap_find_method(mmap_object *self,
                        if (!*n) {
                                return Py_BuildValue (
                                        "i",
-                                       (int) (p - (self->data + start)));
+                                       (int) (p - self->data));
                        }
                        p++;
                }