Skip to content

Commit 83d6baf

Browse files
committed
protection against zero-length argument for nthread
1 parent 6688f1a commit 83d6baf

File tree

10 files changed

+58
-5
lines changed

10 files changed

+58
-5
lines changed

pkg/DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Description: Implements an approximate string matching version of R's native
2323
vectors representing generic sequences. This package is built for speed and
2424
runs in parallel by using 'openMP'. An API for C or C++ is exposed as well.
2525
Reference: MPJ van der Loo (2014) <doi:10.32614/RJ-2014-011>.
26-
Version: 0.9.13
26+
Version: 0.9.15
2727
Depends:
2828
R (>= 2.15.3)
2929
URL: https://github.com/markvanderloo/stringdist

pkg/NEWS

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
version 0.9.13
1+
version 0.9.15
2+
- Fixe issue with zero-length 'nthreads' argument in all exported functions
3+
with this parameter. (Thanks to Brian Ripley for the notification and pointer
4+
to the problem)
5+
6+
version 0.9.14
27
- Fixed issue with zero-length strings in 'qgrams' (Thanks to Brian Ripley
38
for the notification and pointer to the origin of the problem)
49

pkg/R/afind.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ afind <- function(x, pattern, window=NULL
8787
, is.logical(value) && !is.na(value)
8888
, ifelse(method %in% c('osa','dl'), length(weight) >= 4, TRUE)
8989
, ifelse(method %in% c('lv','jw') , length(weight) >= 3, TRUE)
90+
, length(nthread) == 1
91+
, is.numeric(nthread)
9092
, nthread > 0
9193
)
9294

pkg/R/amatch.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ amatch <- function(x, table, nomatch=NA_integer_, matchNA=TRUE
104104
, is.logical(useBytes)
105105
, ifelse(method %in% c('osa','dl'), length(weight) >= 4, TRUE)
106106
, ifelse(method %in% c('lv','jw') , length(weight) >= 3, TRUE)
107+
, length(nthread) == 1
108+
, is.numeric(nthread)
107109
, nthread > 0
108110
)
109111
if (method == 'jw') weight <- weight[c(2,1,3)]
@@ -212,6 +214,8 @@ seq_amatch <- function(x, table, nomatch=NA_integer_, matchNA=TRUE
212214
, matchNA %in% c(TRUE,FALSE)
213215
, ifelse(method %in% c('osa','dl'), length(weight) >= 4, TRUE)
214216
, ifelse(method %in% c('lv','jw') , length(weight) >= 3, TRUE)
217+
, length(nthread) == 1
218+
, is.numeric(nthread)
215219
, nthread > 0
216220
)
217221
if (method == 'jw') weight <- weight[c(2,1,3)]

pkg/R/seqdist.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ seq_dist <- function(a, b
6969
, p >= 0
7070
, ifelse(method %in% c('osa','dl'), length(weight) >= 4, TRUE)
7171
, ifelse(method %in% c('lv','jw') , length(weight) >= 3, TRUE)
72+
, length(nthread) == 1
73+
, is.numeric(nthread)
7274
, nthread > 0
7375
)
7476

pkg/R/stringdist.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@
6363
#' }
6464
#' Or use \code{citation('stringdist')} to get a bibtex item.
6565
#'
66-
#' @aliases stringdist-package
66+
#' @name stringdist-package
6767
#' @docType package
6868
#' @useDynLib stringdist, .registration=TRUE
6969
#' @importFrom parallel detectCores
7070
#'
7171
#'
7272
#'
73-
{}
73+
"_PACKAGE"
7474

7575
listwarning <- function(x,y){
7676
sprintf("
@@ -156,6 +156,8 @@ stringdist <- function(a, b
156156
, is.logical(useBytes)
157157
, ifelse(method %in% c('osa','dl'), length(weight) >= 4, TRUE)
158158
, ifelse(method %in% c('lv','jw') , length(weight) >= 3, TRUE)
159+
, length(nthread) == 1
160+
, is.numeric(nthread)
159161
, nthread > 0
160162
)
161163

@@ -224,6 +226,8 @@ stringdistmatrix <- function(a, b
224226
, is.logical(useBytes)
225227
, ifelse(method %in% c('osa','dl'), length(weight) >= 4, TRUE)
226228
, ifelse(method %in% c('lv','jw') , length(weight) >= 3, TRUE)
229+
, length(nthread) == 1
230+
, is.numeric(nthread)
227231
, nthread > 0
228232
)
229233

@@ -342,7 +346,7 @@ lower_tri <- function(a
342346
if (is.na(method)){
343347
stop(sprintf("method '%s' is not defined",method))
344348
}
345-
349+
346350
x <- .Call("R_lower_tri", a, methnr
347351
, as.double(weight), as.double(p), as.double(bt)
348352
, as.integer(q), as.integer(useBytes), as.integer(nthread)

pkg/inst/tinytest/test_afind.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
options(sd_num_thread=1L)
22

3+
# tests against cases that used to segfault when we did not check
4+
# NULL cases.
5+
expect_error(afind("a","b",nthread=1:4))
6+
expect_error(afind("a","b",nthread="foo"))
7+
expect_error(afind("a","b",nthread=integer(0)))
8+
expect_error(afind("a","b",nthread=NULL))
9+
10+
11+
12+
13+
314
texts = c("When I grow up, I want to be"
415
, "one of the harversters of the sea"
516
, "I think before my days are gone"

pkg/inst/tinytest/test_amatch.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
options(sd_num_thread=2)
22
## amatch: Optimal String Alignment
3+
# tests against cases that used to segfault when we did not check
4+
# NULL cases.
5+
expect_error(amatch("a","b",nthread=1:4))
6+
expect_error(amatch("a","b",nthread="foo"))
7+
expect_error(amatch("a","b",nthread=integer(0)))
8+
expect_error(amatch("a","b",nthread=NULL))
9+
310

411
## simple test and multiple edge cases
512
expect_equal(amatch("aa",c("ba","bb"), method="osa",maxDist=1L), 1L)
@@ -187,5 +194,9 @@ options(sd_num_thread=2)
187194
expect_false(seq_ain(x,table))
188195

189196

197+
expect_error(seq_amatch(x,table,nthread=1:4))
198+
expect_error(seq_amatch(x,table,nthread="foo"))
199+
expect_error(seq_amatch(x,table,nthread=integer(0)))
200+
expect_error(seq_amatch(x,table,nthread=NULL))
190201

191202

pkg/inst/tinytest/test_seq_dist.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
options(sd_num_thread=2)
22
## seq_dist
3+
# tests against cases that used to segfault when we did not check
4+
# NULL cases.
5+
expect_error(seq_dist(a=list(c(1L,2L,3L)), b=list(c(2L,1L,3L)),nthread=1:4))
6+
expect_error(seq_dist(a=list(c(1L,2L,3L)), b=list(c(2L,1L,3L)),nthread="foo"))
7+
expect_error(seq_dist(a=list(c(1L,2L,3L)), b=list(c(2L,1L,3L)),nthread=integer(0)))
8+
expect_error(seq_dist(a=list(c(1L,2L,3L)), b=list(c(2L,1L,3L)),nthread=NULL))
39

410
# A simple test to see that everything is passed on to the correct
511
# algorithm

pkg/inst/tinytest/test_stringdist.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ options(sd_num_thread=2)
88
expect_error(stringdist("a","b",weight=c(-1,1,1,1)))
99
expect_error(stringdist("a","b",weight=c(1,0,1,1)))
1010
expect_error(stringdist("a","b",weight=c(1,1,1,4)))
11+
expect_error(stringdist("a","b",nthread=1:4))
12+
expect_error(stringdist("a","b",nthread="foo"))
13+
expect_error(stringdist("a","b",nthread=integer(0)))
14+
expect_error(stringdist("a","b",nthread=NULL))
1115
expect_warning(stringdist(letters[1:3],letters[1:2]))
1216
expect_warning(stringdist(list('a'),'a'))
1317
expect_warning(stringdist('a',list('a')))
1418
expect_warning(stringdistmatrix(list('a')))
1519
expect_warning(stringdistmatrix(list('a'),list('b')))
20+
expect_error(stringdistmatrix("a","b",nthread=1:4))
21+
expect_error(stringdistmatrit("a","b",nthread="foo"))
22+
expect_error(stringdistmatrit("a","b",nthread=integer(0)))
23+
expect_error(stringdistmatrit("a","b",nthread=NULL))
1624

1725

1826

0 commit comments

Comments
 (0)