[Rd] unique.default problem (PR#12551)

From: <prokaj_at_cs.elte.hu>
Date: Sat, 16 Aug 2008 14:20:17 +0200 (CEST)

Full_Name: Vilmos Prokaj
Version: R 2.7.1
OS: windows
Submission from: (NULL) (213.181.195.84)

Dear developers,

The following line of code (produced by a mistake) caused an infinite loop

unique("a",c("a","b"))

or also

unique(1,1:2)

I made a little investigation, and it seems to be that the following function from unique.c is looping infinitely

static int isDuplicated(SEXP x, int indx, HashData *d)
{

int i, *h;

h = INTEGER(d->HashTable);
i = d->hash(x, indx, d);
while (h[i] != NIL) {

```	if (d->equal(x, h[i], x, indx))
return h[i] >= 0 ? 1 : 0;
i = (i + 1) % d->M;
}
h[i] = indx;
```

return 0;
}
In this case h contains only one negative value, which causes d->equal(=requal) to return 0.

static int requal(SEXP x, int i, SEXP y, int j)
{

if (i < 0 || j < 0) return 0;
if (!ISNAN(REAL(x)[i]) && !ISNAN(REAL(y)[j]))

return (REAL(x)[i] == REAL(y)[j]);
else if (R_IsNA(REAL(x)[i]) && R_IsNA(REAL(y)[j])) return 1;     else if (R_IsNaN(REAL(x)[i]) && R_IsNaN(REAL(y)[j])) return 1;     else return 0;
}

I do not claim that the situation above is frequent or even meaningful, however it should not cause a crash of R.

Sincerely yours
Vilmos Prokaj

R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sat 16 Aug 2008 - 17:29:21 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 17 Aug 2008 - 12:36:57 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.