Avoid memcpy() with same source and destination address.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 7 Mar 2014 11:13:33 +0000 (13:13 +0200)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 7 Mar 2014 11:43:28 +0000 (13:43 +0200)
The behavior of that is undefined, although unlikely to lead to problems in
practice.

Found by running regression tests with Valgrind.

src/backend/tsearch/dict_ispell.c
src/backend/utils/adt/tsvector.c

index b6959e90e3021eb46dd0592b79a9325a54456117..1cda70d2c43da22e167ad3827a389a369e56a1c3 100644 (file)
@@ -127,20 +127,19 @@ dispell_lexize(PG_FUNCTION_ARGS)
    if (res == NULL)
        PG_RETURN_POINTER(NULL);
 
-   ptr = cptr = res;
-   while (ptr->lexeme)
+   cptr = res;
+   for (ptr = cptr; ptr->lexeme; ptr++)
    {
        if (searchstoplist(&(d->stoplist), ptr->lexeme))
        {
            pfree(ptr->lexeme);
            ptr->lexeme = NULL;
-           ptr++;
        }
        else
        {
-           memcpy(cptr, ptr, sizeof(TSLexeme));
+           if (cptr != ptr)
+               memcpy(cptr, ptr, sizeof(TSLexeme));
            cptr++;
-           ptr++;
        }
    }
    cptr->lexeme = NULL;
index 397e6c780bf51d2f5dfe9b3e8734de4a544bf863..0dbbc9368bfdd4bb4aa71853829bfea8f59e5fe4 100644 (file)
@@ -125,7 +125,8 @@ uniqueentry(WordEntryIN *a, int l, char *buf, int *outbuflen)
                buflen += res->poslen * sizeof(WordEntryPos) + sizeof(uint16);
            }
            res++;
-           memcpy(res, ptr, sizeof(WordEntryIN));
+           if (res != ptr)
+               memcpy(res, ptr, sizeof(WordEntryIN));
        }
        else if (ptr->entry.haspos)
        {